Compiler
Compiler
Q1. Explain the phases of a compiler. What role does each phase play in translating a high-level
program into machine code?
Answer:
A compiler translates high-level programming code into machine-readable code through several
phases:
1. Lexical Analysis:
The compiler scans the source code to identify tokens (basic language components like
keywords, identifiers, and operators).
Ensures the program's structure follows the grammar rules of the language.
3. Semantic Analysis:
Checks for semantic errors like type mismatches (e.g., adding a number and a string).
Converts the source code into an intermediate representation (IR) for easier optimization and
portability.
5. Code Optimization:
Improves the intermediate code to make it more efficient.
6. Code Generation:
Produces the final machine code (assembly or binary) from the optimized intermediate code.
Links external libraries and loads the executable into memory for execution.
---
Q2. What are Finite Automata and Regular Expressions? How are they used in lexical analysis?
Answer:
Deterministic FA (DFA): Each state has one transition per input symbol.
Non-Deterministic FA (NFA): States may have multiple transitions for the same symbol.
Used to define patterns for lexical elements like keywords and operators in a programming
language.
1. Lexical analyzers use REs to describe tokens (e.g., int for integers).
3. The automaton scans the source code, recognizing valid tokens based on transitions.
---
Q3. Compare top-down and bottom-up parsing techniques. How are they implemented in syntax
analysis?
Answer:
Top-Down Parsing:
Starts with the root of the parse tree and works downward to match the input.
Bottom-Up Parsing:
Starts with the input tokens and works upward to construct the parse tree.
Shift-Reduce Parsing: A token is shifted onto the stack, and reductions are applied to match
grammar rules.
LR Parsers (Left-to-right, Rightmost derivation): Includes variants like SLR, CLR, and LALR.
Comparison:
1. Top-Down Parsing is simpler but struggles with left-recursive grammars.
2. Bottom-Up Parsing handles a wider range of grammars, making it suitable for complex
programming languages.
---
Q4. What is Syntax-Directed Translation (SDT)? How are syntax trees and intermediate code
generated?
Answer:
Integrates semantic actions with grammar rules to guide translation during parsing.
Syntax Trees:
Example: For a + b * c, the syntax tree has + at the root, with a and * as children, and b and c
as leaves.
Converts the syntax tree into a linear representation (e.g., three-address code).
t1 = b * c
t2 = a + t1
---
Q5. Explain code optimization in compilers. What are loop optimization and DAG
representation?
Answer:
Code Optimization:
Enhances the intermediate code to improve performance without altering its functionality.
Goals: Reduce execution time, minimize memory usage, and improve hardware utilization.
Loop Optimization:
1. Loop Unrolling: Reduces the overhead of loop control by executing multiple iterations in a
single iteration.
2. Invariant Code Motion: Moves calculations outside the loop if they don’t depend on loop
variables.
DAG Representation: