Skip to content
Official PYQ · 2021

GATE CS 2021 Set 2 Q3 — Lexical Analysis

Explain with AI

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

int main () {
    Integer x;
    return 0;
}

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

Choose one option
View worked solution

Answer: C

To determine which phase of the compiler throws an error, let us analyze how each phase processes the statement Integer x;:

  1. Lexical Analyzer: The lexical analyzer groups characters into valid tokens. Here, Integer and x are recognized as valid identifiers, and ; is recognized as a punctuation token. Since there are no invalid characters or malformed tokens, no lexical error is thrown.
  2. Syntax Analyzer: The syntax analyzer checks if the sequence of tokens conforms to the context-free grammar of the language. In C, a statement of the form Type identifier; is syntactically valid (where Integer is parsed as a potential user-defined type, such as a typedef). Since the structure is grammatically correct, no syntax error is thrown.
  3. Semantic Analyzer: The semantic analyzer performs type checking and verifies identifier declarations using the symbol table. It looks up the identifier Integer to verify its type definition. Since Integer is neither a built-in type nor has it been declared (e.g., via typedef), the semantic analyzer detects that Integer is an undefined type and throws an "unknown type name" semantic error.

Therefore, the error is detected and thrown during the Semantic Analysis phase.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Lexical Analysis questions