Solved Paper
Q No 1: Short Answers
1 In what situation we use cross compiler?
Ans. A cross-compiler is used when the compiler runs on one machine/OS (host) but
generates executable code for a different machine/architecture (target). Useful for
embedded systems and different-architecture builds.
2. What is the benefit of syntax tree in analysis synthesis model of compilation?
Ans. Syntax trees compactly represent program structure, remove irrelevant grammar
details and provide a convenient intermediate form for semantic checks, transformations
and code generation.
3. Mention some important properties of the intermediate code.
Ans. Important properties: (1) Machine-independent, (2) Simple constructs (three-address
code), (3) Easy to transform to target code and perform analyses.
4. Write the name of phases of compiler in sequence.
Ans. Phases: Lexical analysis → Syntax analysis → Semantic analysis → Intermediate code
generation → Optimization → Target code generation → Assembly & Linking.
5. Define semantic analysis.
Ans. Semantic analysis checks meaning-level correctness (type checking, scope resolution)
and builds/uses the symbol table; it annotates syntax tree with types/attributes.
6. Differentiate between top down parsing and predictive parsing.
Ans. Top-down parsing derives leftmost derivations starting from start symbol. Predictive
parsing (LL(1)) uses lookahead to choose productions without backtracking.
7. Differentiate between NFA and DFA with the help of one example.
Ans. NFA may have multiple or epsilon transitions from a state; DFA has exactly one
transition per input symbol per state.
8. What is the difference between compiler and interpreter?
Ans. Compiler translates source to target machine code before execution. Interpreter
executes source line-by-line at runtime.
9. Differentiate between lexeme and pattern with the help of example.
Ans. Pattern is rule/regex (e.g., digit → [0-9]+). Lexeme is actual instance (e.g., 123).
10. What is the function of static checkers?
Ans. Static checkers analyze code without executing to find errors like type mismatches,
unreachable code, or bugs.
11. What is dead code elimination?
Ans. Dead code elimination removes code whose results are never used or unreachable.
12. Define left recursion.
Ans. Grammar has left recursion if a nonterminal derives a string starting with itself, e.g., A
→ A α | β.
13. Differentiate between lexical analysis and parsing.
Ans. Lexical analysis groups characters into tokens; parsing arranges tokens according to
grammar.
14. Define transition diagram.
Ans. Transition diagram is a directed graph describing finite automata states and
transitions.
15. Define syntax directed translation.
Ans. Syntax-directed translation attaches semantic actions to grammar rules to compute
attributes or generate code.
16. Why we use symbol table in’ compiler?
Ans. Symbol table stores identifiers' information (name, type, scope, memory location) for
fast lookup.
Q No 2: Structure of a Compiler
A compiler is organized into phases:
1. Lexical Analyzer: Produces tokens.
2. Syntax Analyzer: Builds parse tree.
3. Semantic Analyzer: Checks type and scope.
4. Intermediate Code Generator: Produces IR.
5. Optimizer: Improves IR.
6. Code Generator: Produces target code.
7. Assembler & Linker: Produces executable.
Diagram:
Source → Lexical Analyzer → Syntax Analyzer → Semantic Analyzer → IR → Optimizer →
Code Generator → Assembler & Linker → Executable.
Q No 3: Parse Tree Example
A parse tree represents syntactic structure of input according to grammar.
Example grammar:
E→E+T|T
T→T*F|F
F → (E) | id
For input: id + id * id
Parse tree (simplified):
E
/|\
E + T
| /|\
T T*F
| | |
F F id
| |
id id
AST: (+, id, (*, id, id))
Q No 4: Lexical Analysis Phase
Lexical analysis converts characters into tokens.
- Recognizes keywords, identifiers, numbers.
- Uses regex and finite automata.
- Removes whitespace/comments.
- Interacts with symbol table.
- Reports lexical errors.
Example: 'int x=5;' → tokens: keyword(int), id(x), '=', num(5), ';'.
Q No 5: Three Address Code
Three Address Code (TAC) is intermediate representation with at most 3 operands.
Example:
a = b * -c + d
t1 = -c
t2 = b * t1
t3 = t2 + d
a = t3
Forms: quadruples, triples, indirect triples.
Advantages: simple, easy to optimize, close to assembly.
Q No 6: Code Optimization
Original:
x=1
y = a*b + 3
z = a*b + x + z + 2
x=3
Optimized:
t1 = a * b // Common subexpression elimination
y = t1 + 3
z = z + t1 + 3 // Constant propagation & algebraic simplification
x =3
Techniques: CSE, constant propagation, algebraic simplification, temporary introduction.