c - Symbol table and semantic analysis for compiler -
i'm working on building compiler (without using tools -like lex or bison) c language (a simpler one) , have gotten past lexer , parser. not sure way doing parser correct or not. because, far parsing, ie check if syntax correct or not , haven't used linked lists @ all. basically, parser looks this: suppose syntax -
<program> ::= <program_header> <program_body> <program_header>::= program <identifier> <program_body> ::= (<declaration>;)* begin (<statement>;)* end program
my program looks 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 have functions non-terminals , call them @ appropriate times , checking keywords "strcmp". method ok?
from point, how go doing semantic analysis? should start building symbol table?
any suggestion or pointer think great! thank much
well common , rather simple way of doing create recursive descent parser i.e. create functions correspond syntax (which sort of seem have started already):
e.g.
<program> ::= <program_header> <program_body> <program_header>::= program <identifier> <program_body> ::= (<declaration>;)*
would correspond like
void program() { program_header(); program_body(); } void program_header() { char* program_token = get_token(); char* identifier = get_token(); if (identifier==null) report_error(); ... } void program_body() { declaration(); ... }
and inside each function put semantic checks. need symbol table, either global construct if don't want handle scopes or have kind of stack of symbol tables.
Comments
Post a Comment