tags:

views:

78

answers:

2

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.

+1  A: 

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.

Richard Pennington
Sounds like a `Stack` of symbol tables instead of a `List`
Jason Punyon
Right, it is a stack of scopes. Or a FIFO of scopes. Or a list of active scopes in order of relevance. I choose to look at at like the latter because I would traverse the list from the head to the tail to find a scope containing a symbol I'm interested in.
Richard Pennington
Or a tree, where to traverse from the edge to the root.
Simeon Pilgrim
+1  A: 

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.

DigitalRoss