Question:medium

In a compiler, type checking is normally done during which phase?

Show Hint

Type checking belongs to semantic analysis, not syntax or lexical analysis.
Updated On: Jul 6, 2026
  • Lexical analysis
  • Syntax analysis
  • Syntax directed translation
  • Code optimization
Show Solution

The Correct Option is C

Approach Solution - 1

Type checking needs to know the meaning of the code, not just its spelling (lexical analysis) or its grammatical shape (syntax analysis), both of those happen too early to know anything about declared types.
Code optimization happens too late, after the program has already been translated into intermediate form, it isn't concerned with validating types at that point.
Syntax directed translation is the phase where the compiler attaches computed attributes, like a variable's type, to nodes of the grammar as it processes the input, and it's exactly this mechanism that carries out type checking.
So the correct answer is syntax directed translation.
Was this answer helpful?
0
Show Solution

Approach Solution -2

Placing type checking correctly means asking, at each compiler stage, "does the compiler know enough about meaning yet to check this?":

  1. Lexical analysis: At this point the compiler only knows "this is an identifier" or "this is a number", it hasn't even looked up what type a given identifier was declared with, so it's far too early.
  2. Syntax analysis: Here the compiler confirms the code follows valid grammar rules, e.g. that an assignment has the form "identifier = expression", but it still has no notion of what type that identifier or expression evaluates to.
  3. Syntax directed translation: By attaching semantic rules to each grammar production, this is the first point where the compiler actually computes and compares types, for instance checking that both operands of a "+" are numeric before allowing the operation.
  4. Code optimization: By this stage the program has already been translated into intermediate code assuming it type-checked successfully; optimization only rearranges or simplifies that already-valid code.

Only one stage is positioned where meaning (types) is actually computed and checked for the first time.

Therefore, the correct answer is Syntax directed translation.

Was this answer helpful?
0