Skip to main content

GATE CSE 2021Compiler Design › Lexical Analysis, Compilation Phases & LinkingVerified

GATE CSE 2021 PYQ: Consider the following ANSI C \text{ANSI C} ANSI C program: int main () { Integer x ; return 0 ; } Which one…

Consider the following ANSI C\text{ANSI C} program:

  1. int main () {
  2. Integer x;
  3. return 0;
  4. }

Which one of the following phases in a seven-phase CC compiler will throw an error?

  1. A
    Lexical analyzer
  2. B
    Syntax analyzer
  3. C
    Semantic analyzer
    Correct answer
  4. D
    Machine dependent optimizer

Answer

(C) Semantic analyzer

Approach

Trace what each phase can and cannot detect for the statement Integer x;. The lexer only forms tokens, and Integer is not a reserved word, so no token error is possible there. The parser can accept Integer x ; because ANSI C grammar allows an identifier to act as a typedef-name (declaration-specifier); the parser cannot know whether Integer was ever typedef'd. The symbol table does know, and the missing type definition is reported as an error during semantic analysis - which matches the GATE official key.

No token is illegal (no lexical error), and the grammar admits Integer as a typedef-name so parsing succeeds; only the semantic phase, which consults the symbol table, can conclude that the type Integer was never defined. Hence option C, consistent with the stateGATE answer key cited by the dominant community answer.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.