12 TMT 2.atcd At2 Answer Key
12 TMT 2.atcd At2 Answer Key
PART – A (7 × 2 = 14 Marks)
A preprocessor processes source code before the main compilation. Its functions are:
1. Macro processing — expands macros defined with #define.
2. File inclusion — inserts content of header files via #include.
3. Rational preprocessor — extends older language features (e.g., adds structs to older C).
4. Language extension — supports conditional compilation (#ifdef, #ifndef, #endif).
5. Stripping comments — removes all comments from the source.
A symbol table is a data structure maintained by the compiler to store information about every
identifier (variable, function, class, etc.) in the source program.
It stores: identifier name, data type, scope, memory location, size, and parameter info for
functions.
Operations: insert (when declared), lookup (when referenced), update (when type info is
added).
Used across all phases — from lexical analysis to code generation.
A CFG is ambiguous if there exists at least one string in the language that has more than one
parse tree (equivalently, more than one leftmost or rightmost derivation).
Example: E → E+E | E*E | id is ambiguous — the string id+id*id has two parse trees
depending on whether + or * is applied first.
Ambiguity is a property of the grammar, not the language. It can be eliminated by rewriting the
grammar to enforce precedence and associativity.
A predictive parser is a top-down parser that determines which production to apply by looking
at the current input token (lookahead) without backtracking.
• It works on LL(1) grammars — no left recursion, no ambiguity, no common prefixes.
• Uses a parsing table (rows = non-terminals, columns = terminals) built from FIRST and
FOLLOW sets.
• Maintains a stack and replaces top non-terminal based on table entry for current input.
• Also called LL(1) parser or non-recursive predictive parser.
5 What are the notations used to represent an intermediate languages? CO4 PO1 1
7 Draw a annotated parse tree diagram for the input string 3*5+4$. CO4 PO2 2
The compiler transforms the statement through each phase. Here's the progression:
1. Source program (input):
A := B + C * 50
6. Code optimization:
t1 = C * 50
A = B + t1
(temp folded, constant not converted if same type)
OR
State Items
I₁ S'→S·
I₄ A→d·
I₅ S→AA·
I₆ A→aA·
State a d $ S A
0 s3 s4 1 2
1 acc
2 s3 s4 5
3 s3 s4 6
4 r3 r3 r3
5 r1
6 r2 r2 r2
s = shift, r = reduce (by rule number), acc = accept. LALR merges states with same LR(0) core
— here I₃ and the second A→·aA,A→·d state are merged. No conflicts → grammar is
LALR(1).
Syntax Directed Translation (SDT) is a method that associates semantic rules or actions with
grammar productions to perform translation during parsing.
Two types:
• S-attributed SDT — uses only synthesised attributes (computed bottom-up from children to
parent). Compatible with LR parsing.
• L-attributed SDT — uses both synthesised and inherited attributes (passed top-down).
Compatible with LL parsing.
F → id [Link] = [Link]
For 13 marks: explain S-attributed vs L-attributed, write the full SDD table, show an example
evaluation tree (e.g. for a+b*c), and show the generated 3-address code.
OR
Revised grammar:
S → (L) | a
L → S L'
L' → ,SL' | ε
Non-term ( ) a , $
S S→(L) S→a
L L→SL' L→SL'
L' L'→ε L'→,SL' L'→ε
Blank entries = error. L'→ε is placed in FOLLOW(L') = {)}. The grammar is LL(1) after left-
recursion elimination — no cell has more than one entry.
PART – C (1 × 10 = 10 Marks)
Non-term id + * ( ) $
E E→TE' E→TE'
E' E'→+TE' E'→ε E'→ε
T T→FT' T→FT'
T' T'→ε T'→*FT' T'→ε T'→ε
F F→id F→(E)
This is a standard result — memorise it. ε-productions go into FOLLOW sets. No cell conflict
confirms this is LL(1) after transformation.
COURSE OUTCOMES:
Upon successful completion of this course, students will be able to:
CO1: Construct automata theory using Finite Automata and Regular Expressions
CO2: Design context free grammar, Pushdown Automata and Turing Machine
CO3: Utilize the knowledge of patterns and tokens for developing a Lexical and Syntax
analyzer.
CO4: Understand semantics rules (SDT) Schemes to map syntactic structures to semantic
actions.
CO5: Implement code generation and apply code optimization techniques