Currently, I'm working on a simple compiler project.
Suppose having the following grammar:
file_input : file_item*
;
file_item : class_def
| variable_decl
;
class_def : 'class' NAME scope
;
variable_decl : 'dim' NAME 'as' NAME
;
now, while building our symbol table
if we declared a variable before the class definition we will get semantic error, because it won't find the class required in the symbol table
simply, we need let the compiler wait till the class name is defined, so declaring a variable of type foo
and defining the class foo
later won't disturb the compiler.
any suggestion on how to achieve that ?
thanks for your time.
You'll require a multi-pass approach:
First walk over the AST once to build the table mapping class names to the class definition without doing anything else that would require performing lookups on the table. Then walk it a second time, with the table already built and you'll be able to loop up any class you want when encountering a variable definition.