Consider the following program:
int main () {
Integer x;
return 0;
}
Which one of the following phases in a seven-phase compiler will throw an error?
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;:
- Lexical Analyzer: The lexical analyzer groups characters into valid tokens. Here,
Integerandxare 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. - 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 (whereIntegeris parsed as a potential user-defined type, such as atypedef). Since the structure is grammatically correct, no syntax error is thrown. - Semantic Analyzer: The semantic analyzer performs type checking and verifies identifier declarations using the symbol table. It looks up the identifier
Integerto verify its type definition. SinceIntegeris neither a built-in type nor has it been declared (e.g., viatypedef), the semantic analyzer detects thatIntegeris 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.
