Okay, so the issue is that I don't know why I'm getting this error.
For a class, we're writing a compiler piece by piece. This bit of code is supposed to tokenize input symbols. I wrote a series of if/else statements that act like a very simple trie, thinking that it would be able to find all the symbols. It works fine for most of them, but is getting stuck on "<>".
Here's the whole function:
// 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++;
}
But I'm pretty sure the problem is in here:
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++;
}
I just can't see what the issue is, and was hoping that programmers more wise and experienced than myself could point it out. Also, just ignore the printf statements. I was using those to try and help debug.
Here's the entire input text I'm feeding in, if it helps. The error gets thrown at the '<' that comes right after "var".
const==var<>procedureend<=if>=then.else;while(do)call:=read,write+124-jalapeno*/comment//
Aucun commentaire:
Enregistrer un commentaire