Tutorial :How to take scope into account when building a symbol table with yacc?


My yacc parser creates a symbol table, but I need to take scope into account. How would I do that? I heard something about how when you exit a scope, the symbol table gets destroyed. Still not very clear on how to do this.


There are many ways to handle scoping in a symbol table. One very simple way is to have a separate table for each scope and maintain a list of active scopes.

Whenever a new scope is entered, you can create a table for it and add it to the beginning of the active scope list. When you leave the scope, simply remove the head of the active scope list.

I generally find that you don't want to destroy the table when you are done parsing a scope. You may need it later to do semantic analysis, generate debug info, etc.


This is issue is only indirectly related to yacc, as you seem to have properly determined. (All yacc does is match input strings to strings in your grammar.)

So, you get to do all the symbol management and all other semantic processing in code. You have any data structure you can imagine at your disposal.

Some thoughts to get organized with:

  • You could create new symbol tables as you enter nested scopes, and then simply perform multiple lookups in an outward direction until you find a given symbol.

  • You could use a single table and tag each symbol with its originating lexical level. You would then need to handle duplicate symbols that differ only in lexical level and would need a lookup that could return multiple symbols, but you would need only a single table. If you don't plan on keeping all the symbols after exiting a scope then this might be more trouble than its worth. If you do this, you might want to keep a separate stack containing a root pointer to links that thread all the symbols in a given scope together.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »