compiler
compiler
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
Question-4
Question-5
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
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
t1 = 500
Assignment Statement A t2 = 3
t3 = t1 * t2
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
- 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:
• R is false: TAC does not directly show complex things like nested functions or
control structures. It breaks them into simple steps.
Question-15
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
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
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