GATE / CS ARCHIVE

GATE CSE 2024 SET 2 / QUESTION 21

Match compiler phases to their products

Compiler Design · Compiler Phases, Linking & Loading · Full GATE CSE 2024 Set 2 paper

GATE 2024 Set 2, Q21 1 mark · MCQ
Report issue

Compiler Phases, Linking & Loading

Question 21, GATE 2024: Match P lexical analyzer, Q syntax analyzer, R intermediate code generator, S code optimizer to 1 abstract syntax tree, 2 token, 3 parse tree, 4 constant folding. A 4,1,3,2; B 2,3,1,4; C 2,1,3,4; D 4,3,2,1 (listed in P,Q,R,S order).
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Approach: Follow the conventional progression from source characters to structure to intermediate form to optimization.

1. Derivation. Lexical analysis produces tokens(P–2). Syntax analysis constructs a parse tree(Q–3). An abstract syntax tree is a common intermediate representation associated with intermediate-code generation(R–1). Constant folding is a code optimization(S–4). This gives B. Practical compilers can construct ASTs during parsing too; these are the conventional matches among the listed choices, not exclusive ownership of representations.

2. Explanation and checks. A lexer recognizes lexemes such as identifiers and produces tokens; a parser groups tokens according to grammar productions and yields a parse tree. An AST removes grammar-only detail to represent the essential operators and operands, so among these choices it is the intermediate representation. Constant folding replaces an expression such as 3×4 with 12. Real implementations may combine phase boundaries, but B is the intended one-to-one conceptual match.

Original paper, page 15 · Answer key