Question:

Which phase of a compiler checks variable declaration?

Show Hint

A program can be syntactically correct but semantically wrong. For example: int x; x = "hello"; is grammatically correct (Syntax), but semantically incorrect because you cannot assign a string to an integer.
Updated On: Jul 4, 2026
  • Lexical
  • Syntax
  • Semantic
  • Code generation
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is C

Solution and Explanation

Concept: Compilers analyze programs in multiple stages, moving from raw text to logical meaning.
Syntax: Checks if the "structure" is correct (like grammar in English).
Semantics: Checks if the "meaning" is correct (logic, types, declarations).

Step 1:
Defining the scope of Syntax Analysis.
The parser (syntax analyzer) only ensures that statements like x = 10; are valid. It does not care if 'x' was actually defined as an integer earlier.

Step 2:
Defining the scope of Semantic Analysis.
The Semantic analyzer uses a "Symbol Table." It checks if every variable used in an expression has been declared. It also verifies that the type of the variable matches the operation.

Step 3:
Final Identification.
Checking whether a variable is "known" to the program is a task of logic and meaning. Thus, Semantic Analysis is the correct phase for declaration checking.
Was this answer helpful?
0
0