How to take into account when creating a character table with yacc?

My yacc parser creates a character table, but I need to consider the scope. How should I do it? I heard something about how the character table is destroyed when leaving the scope. It is still not very clear how to do this.

+4
source share
2 answers

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

Whenever a new area is introduced, you can create a table for it and add it to the top of the list of active areas. When you leave the scope, simply remove the header of the active scope list.

Usually I find that you do not want to destroy the table when you finish parsing the area. You may need this later to perform semantic analysis, create debugging information, etc.

+2
source

This is a problem that is indirectly related to yacc, as you seem to have decided correctly. (Everything yacc really matches the input lines in your grammar.)

So, you get all the character management and all the semantic processing of the code. You have any data structure that you can imagine at your disposal.

Some thoughts for organizing:

  • You can create new symbol tables when entering nested areas, and then simply perform multiple searches in the outbound direction until you find that symbol.

  • You can use one table and mark each character with its original lexical level. Then you will need to handle repeating characters that differ only in the lexical level and need a search that can return multiple characters, but you only need one table. If you do not plan to save all the characters after leaving the area, then this may be more of a problem than its value. If you do this, you might want to keep a separate stack containing the root pointer in order to bind all the streams of all characters in a given area.

+2
source

All Articles