【问题标题】:Weird string new line thing奇怪的字符串换行的东西
【发布时间】:2020-07-15 07:51:05
【问题描述】:

我目前正在尝试纠正 C# 中的 Lexer(可能很愚蠢,但它只是编程练习,它是我最了解的语言),我遇到了一个奇怪的问题。我已将一个单独的文件拆分为令牌并将其显示到控制台中。但是,每次我运行我的程序时,我都会比脚本中的标记多一行。我已经尝试了将近一天来让它工作,但它不会。这是我的代码和控制台本身:

public class Lexer
    {
        public static List<List<Lexer_Token>> GetTokens(string filePath)
        {
            List<List<Lexer_Token>> tokens = new List<List<Lexer_Token>>();
            string[] commands = GetCommands(Reader.ReadFile(filePath));

            for (int c = 0; c < commands.Length; c++)
            {
                List<Lexer_Token> currentTokens = StoreAsTokens(commands[c]);
                tokens.Add(currentTokens);
            }

            return tokens;
        }

        private static List<Lexer_Token> StoreAsTokens(string command)
        {
            List<Lexer_Token> tokenList = new List<Lexer_Token>();
            string[] tokens = SplitUpCommand(command, ' ');

            for (int t = 0; t < tokens.Length; t++)
            {
                string currentToken = tokens[t];
                Lexer_Token token = new Lexer_Token();
                token.symbol = currentToken;
                tokenList.Add(token);
            }

            return tokenList;
        }

        private static string[] SplitUpCommand(string command, char character)
        {
            return command.Split(character);
        }

        private static string[] GetCommands(string contents)
        {
            return contents.Split(';');
        }

        public static string FormatTokens(List<List<Lexer_Token>> list)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < list.Count; i++)
            {
                List<Lexer_Token> currentTokenList = list[i];

                for (int j = 0; j < currentTokenList.Count; j++)
                {
                    Lexer_Token currentToken = currentTokenList[j];
                    sb.Append("ID: " + currentToken.id.ToString());
                    sb.Append(" Type: " + currentToken.type);
                    sb.Append(" Line: " + currentToken.lineNumber.ToString()
                        + "[" + currentToken.startingCharacterIndex.ToString() + "]");
                    sb.Append(" Symbol: " + currentToken.symbol);
                    if (i != list.Count - 1)
                    {
                        sb.Append("\n");
                    }
                }
            }

            return sb.ToString();
        }
    }

    public class Lexer_Token
    {
        public int id;
        public int lineNumber;
        public int startingCharacterIndex;
        public string type;
        public string symbol;
    }

The console output

【问题讨论】:

  • 文件末尾可能有换行符,所以最后一个“命令”为空。尝试修剪文件以删除字符串末尾的换行符
  • 你有没有调试过调试器,我会说你的split末尾有一个额外的空间或其他东西

标签: c# string lexer


【解决方案1】:

如果您不关心分配,您可以使用 Join 和 linq 执行此操作,也许更容易阅读

static string Output(Lexer_Token currentToken)
{
   var sb = new StringBuilder();
   sb.Append($"ID: {currentToken.id}");
   sb.Append($" Type: {currentToken.type}");
   sb.Append($" Line: {currentToken.lineNumber}[{currentToken.startingCharacterIndex}]");
   sb.Append($" Symbol: {currentToken.symbol}");
   return sb.ToString();
}
public static string FormatTokens(List<List<Lexer_Token>> list)
{
   var lines = list.SelectMany(currentTokenList => currentTokenList).Select(Output);
   return string.Join(Environment.NewLine, lines);
}

【讨论】:

    猜你喜欢
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2020-11-02
    相关资源
    最近更新 更多