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.
Scope defines the “lifetime” of a program symbol. ● If a symbol is no longer accessible then we say that. it is “out of scope.” ●
A symbol table contains the environmental information concerning the attributes of various programming language constructs. In particular, the type and scope information for each variable. The symbol table will be developed as a module to be included in the yacc/bison file.
Symbol Tables. The table of words is a simple symbol table, a common structure in lex and yacc applications. A C compiler, for example, stores the variable and structure names, labels, enumeration tags, and all other names used in the program in its symbol table.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With