IAE Academy

ISRO CS 2023 Q.10: solution and answer

ISRO 2023 · CSQ.10 · 1 mark · MCQ

Which sequence below best describes the order in which the phases of a compiler typically work?

  1. (A)

    Syntax analysis, lexical analysis, semantic analysis, code optimization, intermediate code generation

  2. (B)

    Lexical analysis, syntax analysis, semantic analysis, code optimization, intermediate code generation

  3. (C)

    Syntax analysis, lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization

  4. (D)

    Lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization

See the question in the paper

Answer and solution

ANSWER

The correct sequence is lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, which is option (d).

Official answer: DSource: ISRO's revised and final answer key of 16 February 2024

  1. The front end of a compiler works on the source in this order: lexical analysis, then syntax analysis, then semantic analysis. The middle and back end then work on the result.

  2. Lexical analysis comes first. It reads the character stream and groups it into tokens. The parser needs tokens as its input, so syntax analysis cannot come before it. Options (a) and (c) begin with syntax analysis, so both are wrong.

  3. Syntax analysis comes second. The parser takes the token stream and checks it against the grammar, building a parse tree.

  4. Semantic analysis comes third. It works on the parse tree and does type checking and scope checking.

  5. Intermediate code generation comes after semantic analysis. It turns the checked tree into an intermediate representation.

  6. Code optimization comes after that. The optimizer improves the intermediate code, so it needs that code to exist first. Option (b) puts code optimization before intermediate code generation, so it is wrong.

  7. Option (c) also repeats syntax analysis and starts with it, so it is wrong in two ways.

  8. Option (d) gives lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization. This is the correct sequence, so the answer is (d).

KEY IDEA

Each compiler phase takes the output of the one before it. Tokens come before the parse tree, the parse tree before semantic checks, and the checked tree before intermediate code. Optimization works on that intermediate code, so it comes after it.

WATCH OUT FOR

Optimizing before intermediate code exists, as in option (b), or starting with the parser, as in options (a) and (c). The parser cannot run until the scanner has produced tokens.

From the ISRO CS 2023 paper. Every question of this paper · The full paper (PDF, ISRO) · All 3 ISRO Lexical analysis questions, with notes

More ISRO Lexical analysis questions