CD End Sem QP Answers
CD End Sem QP Answers
Q1 a) [2]
Determine which of the following lexical entries can be derived from <word>?
• Pick
• Picks
• C44
b) [2]
1|Page
Answer: Option A
c) SEMANTIC [2]
What are the problems you may occur while evaluating the following translation scheme?
𝑆 → 𝐴1 𝐴2 {𝐴1. 𝑖𝑛 = 1, 𝐴2. 𝑖𝑛 = 2}
𝐴 → 𝑎 {𝑝𝑟𝑖𝑛𝑡(𝐴. 𝑖𝑛)}
Where A1 and A2 are inherited attributes.
Answer:
An inherited attribute for a symbol on RHS of a production must be computed in an action
before that symbol.
The depth first order traversal of above will gives error (undef) When designing translation
scheme, ensure attribute value is available when referred to.
d) Write an Intermediate code (three address code) to set a 10 × 10 matrix to an identity matrix. [2]
e) Give four examples of program transformations that holds the characteristic of peephole [2]
optimizations.
• Eliminating Redundant Loads and Stores
• Eliminating Unreachable Code
• Flow-of-Control Optimizations
• Algebraic Simplication and Reduction in Strength
• Use of Machine Idioms
1. 𝑎(𝑎|𝑏) ∗ 𝑎
2. ((𝜀|𝑎)𝑏 ∗) ∗
3. (𝑎|𝑏) ∗ 𝑎(𝑎|𝑏)(𝑎|𝑏)
4. 𝑎 ∗ 𝑏𝑎 ∗ 𝑏𝑎 ∗ 𝑏𝑎 ∗
5. !! (𝑎𝑎|𝑏𝑏) ∗ ((𝑎𝑏|𝑏𝑎)(𝑎𝑎|𝑏𝑏) ∗ (𝑎𝑏|𝑏𝑎)(𝑎𝑎|𝑏𝑏) ∗) ∗
Answer
2|Page
Sq
• S =lm=> SS* => SS+S* => aS+S* => aa+S* => aa+a*
• S =rm=> SS* => Sa* => SS+a* => Sa+a* => aa+a* 3.
• Unambiguous
d) Construct the
• Canonical LR
• LALR
3|Page
b) Determine the types and relative addresses for the identifiers in the following sequence of [5]
declarations:
𝑓𝑙𝑜𝑎𝑡 𝑥;
𝑟𝑒𝑐𝑜𝑟𝑑 { 𝑓𝑙𝑜𝑎𝑡 𝑥; 𝑓𝑙𝑜𝑎𝑡 𝑦; } 𝑝;
𝑟𝑒𝑐𝑜𝑟𝑑 { 𝑖𝑛𝑡 𝑡𝑎𝑔; 𝑓𝑙𝑜𝑎𝑡 𝑥; 𝑓𝑙𝑜𝑎𝑡 𝑦; } 𝑞;
Answer:
SDT
1) x float 0 1
2) x float 0 2
2) y float 8 2
2) p record() 8 1
3) tag int 0 3
3) x float 4 3
3) y float 12 3
4|Page
3) q record() 24 1
c) Write atleast two Syntax-Directed Translation (SDT) of Switch-Statements. Then, generate [5]
three-address-code instructions to translate a switch statement.
d) Below is the code to count the number of primes from 2 to 𝑛, using the sieve method on [5]
a suitably large array 𝑎. That is, 𝑎[𝑖] is TRUE at the end only if there is no prime 𝑝𝑖 or
less that evenly divides 𝑖. We initialize all 𝑎[𝑖] to TRUE and then set 𝑎[𝑗] to FALSE if
we find a divisor of 𝑗.
5|Page
• Translate the program into three-address statements. Assume integers require 4 bytes.
• Construct the flow graph for your code.
• Identify the loops in your flow graph.
Answer:
B1 1) I = 2
B2 2) if I > n goto(7)
B3 3) t1 = I * 4
4) a[t1] = TRUE
5) I = I + 1
6) goto(2)
B4 7) count = 0
8) s = sqrt(n)
9) I = 2
B6 11) t2 = I * 4
12) ifFalse a[t2] goto(20)
B9 16) t3 = j * 4
17) a[t3] = FALSE
18) j = j + i
19) goto(15)
B10 20) I = I + 1
21) goto(10)
6|Page
loops
o {B2, B3}
o {B5, B6, B10}
o {B5, B6, B7, B8, B10}
o {B8, B9}
7|Page