Q.1
14 MarksAlso asked in Summer 2026 Q1a
Definitions:
- Compiler:
- A language translator that converts the entire High-Level Language (HLL) source program (e.g., C, C++) into equivalent machine/target code in a single comprehensive process before execution.
- Produces an independent executable object file (faster execution, errors reported after full scan).
- Interpreter:
- A language translator that takes HLL source code (e.g., Python, JavaScript) and translates and executes it line-by-line in real time.
- Does not generate an object code file; execution stops immediately on the first encountered error (easier debugging, slower execution).
- Assembler:
- A specialized system program that converts low-level Assembly Language mnemonics (e.g.,
MOV,ADD,JMP) into native binary machine code instructions ( and ).
Also asked in Summer 2026 Q3a, Summer 2025 Q4a
Error Recovery Strategies in Compilers
When a syntax or semantic error is detected during parsing, the compiler employs one of the following recovery strategies to continue scanning without abruptly halting:
- Panic Mode Recovery:
- The simplest and most widely used strategy. Upon discovering an error, the parser discards incoming tokens one by one until a designated synchronizing token (such as a semicolon
;, closing brace}, orend) is found. - Advantage: Guarantees no infinite loops; simple to implement.
- Phrase-Level Recovery:
- The parser replaces an erroneous prefix of the remaining input with a valid string that allows parsing to proceed (e.g., inserting a missing comma or semicolon, or replacing a comma with a semicolon).
- Drawback: May lead to infinite loops if repair choices are cyclic.
- Error Productions:
- The grammar is augmented with anticipated common syntactic mistakes (e.g., writing
thenafterelse). - When an error production matches, the parser emits an exact error message and continues normal parsing.
- Global Correction:
- Algorithms (like the Cocke-Younger-Kasami algorithm) that find the minimum number of insertions/deletions needed to transform the incorrect program into a valid program.
- Drawback: Extremely expensive in time and space; primarily of theoretical interest.
Also asked in Summer 2026 Q1c, Summer 2025 Q1c, Summer 2024 Q1c
Phases of a Compiler
A compiler translates source code into machine code across two main logical structures: the Analysis (Front-End) and the Synthesis (Back-End) phases, supported universally by the Symbol Table and Error Handler.
Source Program (High-Level Code)
|
v
+----------------------------+
| 1. Lexical Analysis | ---> Generates Stream of Tokens
+----------------------------+
|
v
+----------------------------+
| 2. Syntax Analysis | ---> Generates Parse Tree / Syntax Tree
+----------------------------+
| +----------------------+
v | |
+----------------------------+ | |
| 3. Semantic Analysis | ---> Annotated Syntax Tree |
+----------------------------+ | |
| | SYMBOL TABLE |
v | & |
+----------------------------+ | ERROR HANDLER |
| 4. Intermediate Code Gen | ---> Three-Address Code (TAC) |
+----------------------------+ | (Active in all) |
| | |
v | |
+----------------------------+ | |
| 5. Code Optimization | ---> Optimized TAC |
+----------------------------+ | |
| +----------------------+
v
+----------------------------+
| 6. Code Generation | ---> Target Machine / Assembly Code
+----------------------------+Detailed Description of Each Phase:
- Lexical Analysis (Scanner):
- Reads the raw input character stream and groups characters into meaningful sequences called lexemes, producing a stream of tokens: .
- Strips out comments and redundant whitespace.
- Example:
position = initial + rate * 60produces:
- Syntax Analysis (Hierarchical Parsing):
- Uses Context-Free Grammars (CFG) to verify whether the token stream conforms to language syntax rules.
- Constructs a hierarchical Parse Tree or abstract Syntax Tree showing operator precedence and associativity.
- Semantic Analysis (Type & Consistency Checking):
- Checks the parse tree for semantic validity, enforcing type compatibility, array bounds, and declaration rules.
- Performs Type Checking and automatic Type Coercion (e.g., converting integer
60to floating-point60.0ifrateis float).
- Intermediate Code Generation (ICG):
- Produces an explicit, machine-independent low-level intermediate representation, commonly Three-Address Code (TAC):
- Code Optimization:
- Transforms intermediate code to make target code run faster and consume less memory without changing program semantics.
- Employs dead-code elimination, common sub-expression elimination, loop unrolling, and constant folding.
- Code Generation:
- Maps optimized intermediate representations into the target machine architecture (registers, memory offsets, assembly instructions):
LDF R2, id3
MULF R2, #60.0
LDF R1, id2
ADDF R1, R2
STF id1, R1- Symbol Table Management & Error Handling:
- Symbol Table: Stores identifier names, types, scopes, sizes, and memory locations used across all phases.
- Error Handler: Identifies, reports, and recovers from errors at each phase without aborting compilation.