c-你能帮我找出词典分析器错误的原因吗?符号无效



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

对于一个类,我们正在逐个编写编译器。这段代码应该用于标记输入符号。我写了一系列if/else语句,就像一个非常简单的trie,认为它能够找到所有的符号。这对他们中的一些人来说很好,但却陷入了困境<gt&";。

以下是整个功能:

// Process the symbols
void symbol_processor(char *input)
{
// Initialize symbol_type operator
int symbol_type = -1;
printf("Location1: %dn", input_index);
// A series of if/else that ape a trie
if (input[input_index] == '=')
if (input[input_index + 1] == '=')
{
printf("Location2: %dn", input_index);
// Set the symbol for "=="
symbol_type = eqlsym;
// Move forward two input_index spaces
input_index += 2;
printf("Location4: %dn", input_index);
}
else if (input[input_index] == '<')
if (input[input_index + 1] == '>')
{
printf("Location4: %dn", 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: %dn", 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: %dn", 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: %dn", input_index);
// Set the symbol for "<"
symbol_type = lessym;
// Move forward one input_index space
input_index++;
}

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

如果有帮助的话,这是我输入的全部文本。错误被抛出到"<"紧接在";var";。

const==var<gt;过程结束<如果>则.else;while(do(call:=读取,写入+124墨西哥胡椒*/comment//

您只接受以'='开头的符号,因为没有与第一个if匹配的else语句。您的缩进使其看起来像有;但是,如果您通过indent运行程序,您会发现哪里出了问题。此序列:

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

实际上是:

if (input[input_index] == '=')
if (input[input_index + 1] == '=')
{
printf("Location2: %dn", input_index);
// Set the symbol for "=="
symbol_type = eqlsym;
// Move forward two input_index spaces
input_index += 2;
printf("Location4: %dn", 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] == ...中的每一个是否都使用相同的数组和索引等要容易得多

最新更新