【问题标题】:Would you please help me find the cause for my Lexical Analyzer Error: Invalid Symbol?你能帮我找出我的词法分析器错误的原因:无效符号吗?
【发布时间】:2021-09-03 16:14:36
【问题描述】:

好的,问题是我不知道为什么会出现这个错误。

对于一个类,我们正在逐段编写编译器。这段代码应该对输入符号进行标记。我写了一系列 if/else 语句,它们就像一个非常简单的 trie,认为它能够找到所有符号。它对他们中的一些人来说很好,但卡在“”上。

这是整个函数:

// Process the symbols
void symbol_processor(char *input)
{
    // Initialize symbol_type operator
    int symbol_type = -1;

    printf("Location1: %d\n", input_index);

    // A series of if/else that ape a trie
    if (input[input_index] == '=')
        if (input[input_index + 1] == '=')
        {
            printf("Location2: %d\n", input_index);
            // Set the symbol for "=="
            symbol_type = eqlsym;

            // Move forward two input_index spaces
            input_index += 2;
            printf("Location4: %d\n", input_index);
        }
    else if (input[input_index] == '<')
        if (input[input_index + 1] == '>')
        {
            printf("Location4: %d\n", input_index);
            // Set the symbol for "<>"
            symbol_type = neqsym;

            // Move forward two input_index spaces
            input_index += 2;
        }
        else if (input[input_index + 1] == '=')
        {
            // Set the symbol for "<="
            symbol_type = leqsym;

            // Move forward two input_index spaces
            input_index += 2;
        }
        else
        {
            printf("Location: %d\n", input_index);
            // Set the symbol for "<"
            symbol_type = lessym;

            // Move forward one input_index space
            input_index++;
        }
    else if (input[input_index] == '>')
        if (input[input_index + 1] == '=')
        {
            // Set the symbol for ">="
            symbol_type = geqsym;

            // Move forward two input_index spaces
            input_index += 2;
        }
        else
        {
            // Set the symbol for ">"
            symbol_type = modsym;

            // Move forward one index space
            input_index++;
        }
    else if (input[input_index] == ':')
        if (input[input_index + 1] == '=')
        {
            // Set the symbol for ":="
            symbol_type = becomessym;

            // Move forward two input_index spaces
            input_index += 2;
        }
    // This could cause an issue
    else if (input[input_index] == '/')
        if (input[input_index + 1] == '*')
            comment_error = comment_processor(input);
        else
        {
            
            // Set the symbol for ">"
            symbol_type = slashsym;

            // Move forward one index space
            input_index++;
        }
    else if (input[input_index] == '%')
    {
        // Set the symbol for "%"
        symbol_type = modsym;

        // Move forward one index space
        input_index++;
    }
    else if (input[input_index] == '*')
    {
        // Set the symbol for "*"
        symbol_type = multsym;

        // Move forward one index space
        input_index++;
    }
    else if (input[input_index] == '+')
    {
        // Set the symbol for "+"
        symbol_type = plussym;

        // Move forward one index space
        input_index++;
    }
    else if (input[input_index] == '-')
    {
        // Set the symbol for "-"
        symbol_type = minussym;

        // Move forward one index space
        input_index++;
    }
    else if (input[input_index] == '(')
    {
        // Set the symbol for "("
        symbol_type = lparentsym;

        // Move forward one index space
        input_index++;
    }
    else if (input[input_index] == ')')
    {
        // Set the symbol for ")"
        symbol_type = rparentsym;

        // Move forward one input_index space
        input_index++;
    }
    else if (input[input_index] == ',')
    {
        // Set the symbol for ","
        symbol_type = commasym;

        // Move forward one index space
        input_index++;
    }
    else if (input[input_index] == '.')
    {
        // Set the symbol for "."
        symbol_type = periodsym;

        // Move forward one input_index space
        input_index++;
    }
    else if (input[input_index] == ';')
    {
        // Set the symbol for ";"
        symbol_type = semicolonsym;

        // Move forward one index space
        input_index++;
    }

    // Check to see if an error should be thrown
    if (symbol_type == -1)
        error_processor(1); // Invalid Symbol

    // Append symbol to the list
    list[lex_index].type = symbol_type;
    lex_index++;
}

但我很确定问题出在这里:

else if (input[input_index] == '<')
        if (input[input_index + 1] == '>')
        {
            printf("Location4: %d\n", input_index);
            // Set the symbol for "<>"
            symbol_type = neqsym;

            // Move forward two input_index spaces
            input_index += 2;
        }
        else if (input[input_index + 1] == '=')
        {
            // Set the symbol for "<="
            symbol_type = leqsym;

            // Move forward two input_index spaces
            input_index += 2;
        }
        else
        {
            printf("Location: %d\n", input_index);
            // Set the symbol for "<"
            symbol_type = lessym;

            // Move forward one input_index space
            input_index++;
        }

我只是看不出问题出在哪里,希望比我更有智慧和经验的程序员能指出这一点。另外,只需忽略 printf 语句。我正在使用它们来尝试帮助调试。

这是我输入的整个输入文本,如果有帮助的话。错误在“var”之后的“

const==varprocedureend=then.else;while(do)call:=read,write+124-jalapeno*/comment//

【问题讨论】:

    标签: c if-statement compiler-construction symbols trie


    【解决方案1】:

    您只接受以“=”开头的符号,因为没有与您的第一个 if 匹配的 else 语句。您的缩进使它看起来像有;但是如果你通过缩进运行你的程序,你会发现哪里出错了。 这个顺序:

    if (input[input_index] == '=')
        if (input[input_index + 1] == '=')
        {
            printf("Location2: %d\n", input_index);
            // Set the symbol for "=="
            symbol_type = eqlsym;
    
            // Move forward two input_index spaces
            input_index += 2;
            printf("Location4: %d\n", input_index);
        }
    else if (input[input_index] == '<')
        if (input[input_index + 1] == '>')
    

    其实是:

    if (input[input_index] == '=')
        if (input[input_index + 1] == '=')
        {
            printf("Location2: %d\n", input_index);
            // Set the symbol for "=="
            symbol_type = eqlsym;
    
            // Move forward two input_index spaces
            input_index += 2;
            printf("Location4: %d\n", input_index);
        } else if (input[input_index] == '<')
            if (input[input_index + 1] == '>')
    

    使用 switch 语句通常更习惯 C 语言,特别是对于最外层的条件,所以逻辑看起来更像:

    switch (input[input_index]) {
        case '=':
            if (input[input_index + 1] == '=')
            {
            }
            break;
        case '<':
            if (input[input_index + 1] == '>')
            {
            }
            else if (input[input_index + 1] == '=')
            {
            }
            else
            {
            }
            break;
        case '>':
            if (input[input_index + 1] == '=')
            {
            }
            else
            {
            }
            break;
    

    在读者看来,这比试图弄清楚if (input[input_index] == ... 中的每一个是否都使用相同的数组和索引等要容易得多。

    【讨论】:

    • 是的,我看到了这个问题。我的缩进愚弄了我。我怀疑我是否会注意到这个问题。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多