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?

  • $\begingroup$ 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. $\endgroup$ – Yuval Filmus Nov 26 '17 at 8:12
  • $\begingroup$ Is symbol table updated during parsing ? $\endgroup$ – Sagar P Nov 26 '17 at 8:40
  • $\begingroup$ Can someone please answer this question? $\endgroup$ – Sagar P Nov 26 '17 at 10:29
  • $\begingroup$ Don't be so impatient! $\endgroup$ – Gilles 'SO- stop being evil' Nov 26 '17 at 11:13
  • $\begingroup$ 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. $\endgroup$ – Gilles 'SO- stop being evil' 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.

  • $\begingroup$ Is symbol table updated during syntax analysis and semantic analysis ? $\endgroup$ – Sagar P Nov 29 '17 at 14:53
  • 1
    $\begingroup$ 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;. $\endgroup$ – fade2black Nov 29 '17 at 15:15
  • 1
    $\begingroup$ 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. $\endgroup$ – fade2black Nov 29 '17 at 15:16
  • 1
    $\begingroup$ Please read the last paragraph of my post concerning your question "What more information is added by later phases like syntax, semantic and code generation?" $\endgroup$ – fade2black Nov 29 '17 at 15:21
  • 1
    $\begingroup$ It depends on how you implement ICG. If you implement three-address statements using quadruples you will have to store temp variables in the ST. But you can avoid storing temporary names if you use triples. For more details see Dragon book Chapter 8 (ICG). $\endgroup$ – fade2black Nov 29 '17 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.