Unit-3
Question-4
Goto from I₀ (Shift actions):
• Goto(I₀, x) = I₁
Items in I₁:
o S → x⋅Ay
o S → x⋅Bz
o A → ⋅w
o B → ⋅w
• Goto(I₀, y) = I₂
Items in I₂:
o S → y⋅Bx
o S → y⋅Az
o A → ⋅w
o B → ⋅w
• Goto(I₀, S) = I₃
Items in I₃:
o S′ → S⋅
Question-5
All the given FOLLOW sets are correct.
• FOLLOW(S) = { $ }
• FOLLOW(A) = { e, f }
• FOLLOW(B) = { $ }
Question-6
CLR(1) Item Sets:
I₀
S′ → .S , $
S → .CC , $
C → .cC , c/d
C → .d , c/d
I₁
S′ → S. , $
I₂
S → C.C , $
C → .cC , $
C → .d , $
I₃
S → CC. , $
I₄
C → c.C , $
C → .cC , $
C → .d , $
I₅
C → d. , $
I₆
C → cC. , $
UNIT-4
Question 1:
FlowML and Chomsky Hierarchy
FlowML is a Type-2 language (Context-Free Language).
Why?
• It has nested structures like decisions (YesTask | NoTask).
• It uses expressions like AND, OR, NOT.
• These can be written using context-free grammar.
• No need for context-sensitive rules.
Question 2:
Strings of Fixed Length
a) ∑ = {a, b}, ∑⁴ (length 4)
Answer:
There are 16 strings of length 4:
aaaa, aaab, aaba, aabb, abaa, abab, abba, abbb, baaa, baab, baba, babb, bbaa, bbab, bbba,
bbbb
b) ∑ = {0, 1, 2}
• ∑² → 9 strings (like 00, 01, ..., 22)
• ∑⁴ → 81 strings
Question-3
Grammar Type Language Accepted Automaton
Type 0 Recursively enumerable language (A) Turing Machine
Type 1 Context-sensitive language Linear Bounded Automaton (B)
Type 2 Context-free language (C) Pushdown Automaton
Type 3 Regular language Finite Automaton (D)
Question-4
Question-5
Symbol Type Value Scope
x int
y int
z int 10
Question-6
For executing a custom scripting language, the most suitable language processor is an
interpreter because it executes code line-by-line, which is perfect for scripting languages
that require flexibility and immediate execution.
(OR)
For executing a custom scripting language, the best processor is an interpreter. It executes
code directly, allowing for quick testing and flexibility.
Question-7
(I) The output of a lexical analyzer is tokens, not just groups of characters.
(II) The total number of tokens in printf(“i=%d, &i=%x”, i, &i); is 11 (including
punctuation, operators, and identifiers).
Question-8
((c + d) + (a * b)) - ((a + b) + (c + d))
Question-9
Op Arg1 Arg2 Arg3
minus c t1
mul b t1 t2
minus c t3
mul b t3 t4
add t2 t4 t5
assign t5 a
Explanation:
• Op: The operation performed (e.g., minus, mul, add, assign).
• Arg1, Arg2: The operands for the operation (e.g., variables or temporary variables).
• Arg3: The result of the operation (stored in temporary variables or the final variable).
Question-10
Language Construct of Statement Intermediate Code
Examples (C)
(A) (B)
t1 = 500
Assignment Statement A t2 = 3
t3 = t1 * t2
Copy Statement x=y total = t3
Language Construct of Statement Intermediate Code
Examples (C)
(A) (B)
if t3 > 1000 goto
Conditional Jump C apply_discount
goto no_discount
Unconditional Jump goto label goto apply_discount
Explanation:
• Copy Statement: After calculating t3 = t1 * t2 which results in 1500, this value can
be copied to another variable like total → total = t3.
• Unconditional Jump: An unconditional jump might be something like jumping to the
discount application logic → goto apply_discount.
Let me know if you'd like the intermediate code for applying the 10% discount as well!
Question-11
w = 1000
d = 500
call areaofrectangle, w, d
t4 = w * d
return t4
t1 = return value
area = t1
print area
Question-12
Op Arg1 Arg2 Result
- b c t1
* a t1 t2
+ a t2 t3
* t1 d t4
+ t3 t4 t5
= t5 x
Question-13
(+) ← t5
/ \
(+) (*) ← t3 and t4
/ \ /\
a (*) t1 d ← t2 and t1
/\
b c
Question-14
Answer: C. A is true, but R is false
Simple Justification:
• A is true: Three Address Code uses at most 3 parts (like x = y + z).
• R is false: TAC does not directly show complex things like nested functions or
control structures. It breaks them into simple steps.
Question-15
Column A (TAC Column B (Type of
Column C (Explanation)
Instruction) Operation)
C3. Adds two variables and stores
A1. t1 = a + b B2. Arithmetic Operation
result
C1. Applies negation to a single
A2. t2 = -a B4. Unary Operation
operand
C2. Transfers control based on a
A3. if a < b goto L1 B3. Conditional Jump
condition
– (Explanation not provided in
A4. goto L2 B1. Unconditional Jump
Column C)
UNIT-5
Question-1
(+) ← t3
/ \
t1 (*) ← t1 and t2
/ \ /\
x y t1 z
question-2
t1 = 1
L1: if t1 > 10 goto L2
if t1 % 2 != 0 goto L3
param t1
call print
L3: t1 = t1 + 1
goto L1
L2:
Question-3
i=0
L1:
t2 = a + b
t4 = 4 * i
t5 = t2 + t4
i=i+1
if i < 10 goto L1
question-4
t1 = a + a
t2 = t1 + t1
DAG Representation:
(+)
/ \
t1 t1
\ /
(+)
/ \
a a
question-5
Correct Answer: C. A is true, but R is false
Justification:
Assertion (A): TRUE
• Peephole optimization is a local optimization technique.
• It looks at a small window (peephole) of consecutive instructions in the generated
code.
• It applies simple transformations to improve performance, like:
o Removing redundant instructions
o Replacing inefficient instructions
o Eliminating unreachable code
• So, the assertion is correct.
Question-6
A1 – B1 – C1
A2 – B2 – C2
A3 – B3 – C3
A4 – B4 – C4
A5 – B5 – C5
Question-7
a) Flow Graph Basic Blocks:
• B1: (1)–(4)
• B2: (5)–(8)
• B3: (9)–(12)
• B4: (13)–(15)
• B5: (16)–(22)
• B6: (23)–(30)
b) After Copy Propagation:
cpp
CopyEdit
a[t2] = t5
a[t4] = t3
goto B2
4o
Question 8: Local Common Subexpression Elimination
Given Code:
makefile
CopyEdit
t6 = 4 * i
x = a[t6]
t7 = 4 * i
t8 = 4 * j
t9 = a[t8]
a[t7] = t9
t10 = 4 * j
a[t10] = x
Optimized Code:
java
CopyEdit
t6 = 4 * i
x = a[t6]
t8 = 4 * j
t9 = a[t8]
a[t6] = t9 // used t6 instead of recomputing t7
a[t8] = x // used t8 instead of recomputing t10
Common Subexpressions Eliminated:
• 4 * i reused (t6 and t7)
• 4 * j reused (t8 and t10)
Question 9: Expression from Three Address Code
TAC:
ini
CopyEdit
t1 = a * b
t2 = minus t1
t3 = c + d
t4 = t3 + t1
t5 = a + b
t6 = t5 + t3
t7 = t4 - t6
Step-by-step Reconstruction:
• t1 = a * b
• t3 = c + d
• t4 = t3 + t1 = (c + d) + (a * b)
• t5 = a + b
• t6 = t5 + t3 = (a + b) + (c + d)
• t7 = t4 - t6 = [(a * b) + (c + d)] - [(a + b) + (c + d)]
Final Expression:
css
CopyEdit
(a * b + c + d) - (a + b + c + d)
Question 10:
a) Rearranged TAC (for clarity & order):
csharp
CopyEdit
(5) t3 = minus c
(6) t1 = minus c
(1) t4 = b * t3
(3) t2 = b * t1
(2) t5 = t2 + t4
(4) a = t5
b) Code Optimization Opportunity:
• t3 = minus c and t1 = minus c are same, so reuse one.
• b * t3 and b * t1 are same, so compute once.
Optimized Code:
ini
CopyEdit
t1 = minus c
t2 = b * t1
t5 = t2 + t2
a = t5
OR even shorter:
ini
CopyEdit
t1 = minus c
t2 = b * t1
a = t2 + t2