# Symbol table usage

In which phases of the compiler, is symbol table used and updated ?

According to me,

In lexical analysis, new entry for each variable is created in symbol table so symbol table is only updated

In syntax and semantic analysis, symbol table is just used to check the information about attributes during parse tree creation and type checking. So in both of these phases , symbol table is only used and not updated

In intermediate code generation and code optimization, entry in symbol table is added in case any temporary variables are created and to check the type of temp variable, symbol table is used.So symbol table is both used and updated here.

I am not sure about Target code generation.

Can someone please tell me if I am correct?

• I doubt that the symbol table is updated after parsing. After all, its purpose is to associate names of variables to internal IDs. If you're using variables internally, they already have no name. – Yuval Filmus Nov 26 '17 at 8:12
• Is symbol table updated during parsing ? – Sagar P Nov 26 '17 at 8:40
• Can someone please answer this question? – Sagar P Nov 26 '17 at 10:29
• Don't be so impatient! – Gilles Nov 26 '17 at 11:13
• Which symbol table are you referring to, and in what kind of language/compiler architecture? A symbol table is a mapping from names to something, and in a typical build tool chain there is more than one something involved. – Gilles Nov 26 '17 at 11:14

In which phases of the compiler, is symbol table used and updated?

Symbol table are interacted during all phases of a compiler.

Basically, a compiler operates in phases which are grouped as backend and frontend. The front end deals with those part of code which is independent of the target platform and mainly depends on the language structure, while backend does not depend on the structure of the language but depend on the target platform.

Essentially, a symbol table is a data structure (usually a hash table) containing information about identifiers. Identifiers are detected and stored in the symbol table by the lexical analyzer. During the analysis phase (frontend) the compiler collects various information about identifiers (scope, type, size, etc.), stores it into the symbol table, and later this information are used in various ways in remaining phases, for example by code generator and optimizer. The symbol table are used and updated as necessary and for the most part depends on what kind of compiler you are writing (target platform, architecture, structure of language, etc.).

For example, initially the symbol table may store keywords. The syntax analyzer may store information about types of identifiers, and semantic analyzer may use this information to check semantics of expressions. The code generator uses types of identifiers and store the information about the storage assigned to identifiers.

Using of Symbols Tables are discussed in Chapters 2 and 7 of the Dragon book.

• Is symbol table updated during syntax analysis and semantic analysis ? – Sagar P Nov 29 '17 at 14:53
• As I said "as necessary". There is no strict rule as "when and how" to use the symbol table. In fact, you could design a "useless" compiler which would not use the symbol table at all. So there is no a single and definite answer to your question. That said, during syntax analysis a compiler (for static typed languages such as C/C++) hove to store information about type of identifiers, for exampleint my_var;. – fade2black Nov 29 '17 at 15:15
• But semantic analysis mainly concerns about type checking. I cannot think about a scenario where semantic analyzer would change information about identifiers, except it is used with interpreters for a dynamic typed language where you would change the type of the variable on type conversion. – fade2black Nov 29 '17 at 15:16