Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 695
neuromancer Avatar asked Nov 30 '09 22:11

neuromancer


People also ask

What is scoping in symbol table?

Scope defines the “lifetime” of a program symbol. ● If a symbol is no longer accessible then we say that. it is “out of scope.” ●

What is YACC symbol table?

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.

What is the use of symbol table in Lex?

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.


1 Answers

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.

like image 60
Richard Pennington Avatar answered Sep 27 '22 17:09

Richard Pennington