Question:

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
collegedunia
Verified By Collegedunia

The Correct Option is C

Approach Solution - 1

Step 1: Understanding type checking.
Type checking ensures that operations in a program are applied to compatible data types, such as preventing addition of an integer and a string.
Step 2: Role of compiler phases.
Lexical analysis identifies tokens, while syntax analysis checks grammatical structure. Neither phase checks semantic correctness like type compatibility.
Step 3: Syntax-directed translation.
Type checking is part of semantic analysis, which is implemented using syntax-directed translation with attributes attached to grammar symbols.
Step 4: Final conclusion.
Hence, type checking is normally performed during syntax-directed translation.
Was this answer helpful?
0
0
Show Solution
collegedunia
Verified By Collegedunia

Approach Solution -2

Type checking verifies that operations in a program are applied to compatible types, this is a matter of meaning (semantics), not just structure or spelling. Let's check each compiler phase:

  1. Lexical analysis: This phase only groups characters into tokens like identifiers, numbers, and keywords; it has no concept of what a variable's type is, so it cannot catch type mismatches.
  2. Syntax analysis: This phase checks that tokens form a grammatically valid structure (matching parentheses, well-formed statements), producing a parse tree. It verifies shape, not meaning, so a syntactically valid statement like adding an integer to a string would still pass through this phase untouched.
  3. Syntax directed translation: This is where semantic actions and attributes are attached to grammar rules, letting the compiler compute and check properties like a symbol's declared type as the parse tree (or its equivalent) is processed. Type checking is implemented precisely through these semantic rules attached to the grammar.
  4. Code optimization: This phase improves already-generated intermediate code for speed or size; by this point the program has already been validated for type correctness, optimization doesn't perform semantic checks.

Only the phase that attaches semantic computation to the grammar structure is equipped to check types.

Therefore, the correct answer is Syntax directed translation.

Was this answer helpful?
0
0