Arrange the following stages of parsing in the correct order as they typically…
2024
Arrange the following stages of parsing in the correct order as they typically occur in the compilation process.
(A) Lexical Analysis
(B) Semantic Analysis
(C) Syntax Analysis
(D) Intermediate Code Generation
(E) Code Optimization
Choose the correct answer from the options given below:
- A.
(A), (B), (C), (D), (E)
- B.
(A), (C), (B), (D), (E)
- C.
(A), (D), (B), (C), (E)
- D.
(A), (C), (D), (B), (E)
Attempted by 305 students.
Show answer & explanation
Correct answer: B
Correct order: Lexical Analysis → Syntax Analysis → Semantic Analysis → Intermediate Code Generation → Code Optimization.
Lexical Analysis: Convert source characters into tokens (identifiers, keywords, operators, etc.).
Syntax Analysis: Parse tokens to create a parse tree or abstract syntax tree (AST) according to the grammar.
Semantic Analysis: Perform type checking, scope resolution and other semantic checks on the AST.
Intermediate Code Generation: Generate an intermediate representation (IR) from the semantically-checked AST.
Code Optimization: Optimize the intermediate code to improve performance or reduce resource usage before final code generation.
Why other orders are incorrect: Syntax analysis must follow lexical analysis because parsing needs tokens; semantic analysis must follow parsing because it requires the AST; intermediate code generation and optimization must come after semantic checks so that generated code is correct and can be safely optimized.
A video solution is available for this question — log in and enroll to watch it.