[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Implementing identification on SableCC ASTs
Hi Jens,
The usual approach is to build a tree of symbol tables, e.g.:
- 1 outer scope (global variables) symbol table
- 1 symbol table for each inner scope which maintains a pointer
to its "parent" scope
You usually store a pointer from each block of code to its appropriate
symbol table.
Looking up a symbo goes recursively from the "current" symbol table
to its ancestors until a match is found.
You should look at the JJOOS example compiler (see Prof. Hendren's
cs520 course page http://www.sable.mcgill.ca/~hendren/520/).
Etienne
To make your solution work, it would be necessary to
define a
SymbolTable class that "remembers" everything. I.e.
when the so called
SemanticAnalyser class goes through the AST and
opens scopes, enters
identifiers and closes scopes - everything has to be
stored in the
SymbolTable.
Normally, the symbol table "forgets" about
identifiers declared in a
scope when the scope is closed.
You are totally right. I had forgotten about the local
scope variables as well as nested ones. I haven't
tried the HashMap technique, so I can't really comment
on that.
--
Etienne M. Gagnon, Ph.D. http://www.info.uqam.ca/~egagnon/
SableVM: http://www.sablevm.org/
SableCC: http://www.sablecc.org/