IAE Academy

GATE 2020 CS Q.9: solution and answer

GATE 2020 · CSQ.9 · 1 mark · MCQ

Consider the following statements.

I. Symbol table is accessed only during lexical analysis and syntax analysis.
II. Compilers for programming languages that support recursion necessarily need heap storage for memory allocation in the run-time environment.
III. Errors violating the condition 'any variable must be declared before its use' are detected during syntax analysis.

Which of the above statements is/are TRUE?

  1. (A)

    I only

  2. (B)

    I and III only

  3. (C)

    II only

  4. (D)

    None of I, II, and III

See the question in the paper

Answer and solution

ANSWER

(D) None of I, II, and III.

Official answer: DSource: Official answer key, GATE 2020 CS (IIT Delhi)

  1. Statement I is false. The symbol table is used in more phases than lexical and syntax analysis. Semantic analysis looks names up in it for type checking and scope checking. Code generation and optimization read it for the type, size and storage of each name. So 'accessed only during lexical analysis and syntax analysis' is wrong.

  2. Statement II is false. Recursion needs a fresh activation record for every call, and these records are allocated on the run-time stack in last-in, first-out order. Heap storage is needed only for data that outlives the call that created it, such as memory from malloc or new. A language with recursion but no dynamic allocation can run on a stack alone, so heap storage is not necessary.

  3. Statement III is false. 'Every variable must be declared before its use' is a context-sensitive condition. A context-free grammar, which is what syntax analysis uses, cannot express it. The check is done in semantic analysis, by looking the identifier up in the symbol table.

  4. All three statements are false, so the answer is (D) None of I, II, and III.

KEY IDEA

The symbol table is used in many phases, including semantic analysis and code generation. Recursion needs the run-time stack and not necessarily a heap. Declaration-before-use is a context-sensitive rule, so it is checked in semantic analysis and not by the context-free syntax analysis.

WATCH OUT FOR

Treating the symbol table as a tool only for the first two phases. Another slip is reading 'declared before use' as a syntax error, when it is a semantic one. A third is assuming recursion forces heap allocation, when stack frames are enough.

From the GATE 2020 CS paper. Every question of this paper · Download the full paper · All 8 Lexical analysis questions, with notes

More Lexical analysis questions