Question
Consider the following ANSI C program:
int main() {
Integer x;
return 0;
}
Which one of the following phases in a seven-phase C compiler will throw an error?
Official answer and solution
- Official answer
- C
- From
- Official GATE 2021 answer key
Solution
Trace compiler phases for the statement 'Integer x;' when Integer is not defined.
The program contains the statement Integer x; inside main().
- Lexical Analysis:
Integermatches the pattern of a valid identifier token, andxis another valid identifier. Both are recognized without error.
- Syntax Analysis: The grammar of C includes productions of the form
<type_name> <identifier>;(or typedef-name followed by variable name). The syntax analyzer builds a valid parse tree according to the context-free grammar rules.
- Semantic Analysis: The compiler checks type declarations and symbol table entries. Because
Integerhas not been defined viatypedefor as a primitive type, type resolution fails. The semantic analyzer reports an undeclared type error.
Final answer
C
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2021 Computer Science and Information Technology - Session 2 (CS2)
- Question
- 13 of 65, printed as CS Q.3
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.