I'm working on building a compiler (without using any tools -like lex or bison) for a C like language (a simpler one) and have gotten past the lexer and parser. I am not sure the way I am doing the parser is correct or not. Because, so far to do the parsing, ie to check if the syntax is correct or not , I haven't used linked lists at all. Basically, my parser looks like this: Suppose the syntax is -
<program> ::= <program_header> <program_body>
<program_header>::= program <identifier> is
<program_body> ::= (<declaration>;)*
begin
(<statement>;)*
end program
My program looks like this:
parser()
{
char *next_token;
next_token = get_token();
check_for_program(next_token);
}
check_for_program(next_token)
{
check_for_program_header(next_token);
if (header_found)
check_for_program_body();
}...
I basically have functions for all the non-terminals and call them at appropriate times and I am checking for the keywords by "strcmp". Is this method OK?
From this point, how to go about doing semantic analysis? Where should I start building the symbol table?
Any suggestion or pointer to think is great! Thank you very much