0% found this document useful (0 votes)
34 views7 pages

CD End Sem QP Answers

5th sem

Uploaded by

Akash Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views7 pages

CD End Sem QP Answers

5th sem

Uploaded by

Akash Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Marks

Q1 a) [2]

A particular BNF definition for a "word is given by the following rules.


<word> :: = <letter> | <letter> <charpair> | <letter> <intpair>
<charpair> :: = <letter> <letter> | <charpair> <letter> <letter>
<intpair> :: = <integer> <integer> I <intpair> <integer> <integer>
<letter> :: = a | b | c | ...... | Y | Z
<integer> :: = 0 | 1 | 2 | ...... | 9

Determine which of the following lexical entries can be derived from <word>?
• Pick
• Picks
• C44

Answer: Picks and 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

Q2 a) Describe the languages denoted by the following regular expressions: [5]

1. 𝑎(𝑎|𝑏) ∗ 𝑎
2. ((𝜀|𝑎)𝑏 ∗) ∗
3. (𝑎|𝑏) ∗ 𝑎(𝑎|𝑏)(𝑎|𝑏)
4. 𝑎 ∗ 𝑏𝑎 ∗ 𝑏𝑎 ∗ 𝑏𝑎 ∗
5. !! (𝑎𝑎|𝑏𝑏) ∗ ((𝑎𝑏|𝑏𝑎)(𝑎𝑎|𝑏𝑏) ∗ (𝑎𝑏|𝑏𝑎)(𝑎𝑎|𝑏𝑏) ∗) ∗

Answer

1. String of a's and b's that start and end with a.


2. String of a's and b's.
3. String of a's and b's that the character third from the last is a.
4. String of a's and b's that only contains three b.
5. String of a's and b's that has a even number of a and b

c) Consider the context-free grammar: [5]


𝑆→𝑆𝑆 + |𝑆𝑆 ∗ |𝑎
and the string 𝑎𝑎 + 𝑎 ∗

• Give a leftmost derivation for the string.


• Give a rightmost derivation for the string.
• Give a parse tree for the string.
• Is the grammar ambiguous or unambiguous? Justify your answer.
• Describe the language generated by this grammar.

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

• The set of all postfix expressions consist of addition and


multiplication

d) Construct the

• Canonical LR
• LALR

Set of items for the grammar 𝑆 → 𝑆 𝑆 + | 𝑆 𝑆 ∗ | 𝑎


Answer: Refer Class Notes

e) Consider a grammar for signed binary numbers [5]


𝑁𝑢𝑚𝑏𝑒𝑟→𝑆𝑖𝑔𝑛 𝐿𝑖𝑠𝑡
𝑆𝑖𝑔𝑛 → + | −
𝐿𝑖𝑠𝑡 → 𝐿𝑖𝑠𝑡 𝐵𝑖𝑡 | 𝐵𝑖𝑡
𝐵𝑖𝑡 → 0 | 1
Write the Syntax Directed Translation (SDT) that annotates number 10101 with the decimal
value that it represents.
Answer: Refer Class Notes
Q3 a) Write the Syntax Directed Translation (SDT) for generating the Directed Acyclic Graph (DAG) [5]
for the following expression:
𝑎 + 𝑎 ∗ (𝑏 − 𝑐) + (𝑏 − 𝑐) ∗ 𝑑
Also state the total number of nodes and edges in the DAG.
Answer: Refer class Notes for SDT

3|Page
b) Determine the types and relative addresses for the identifiers in the following sequence of [5]
declarations:

𝑓𝑙𝑜𝑎𝑡 𝑥;
𝑟𝑒𝑐𝑜𝑟𝑑 { 𝑓𝑙𝑜𝑎𝑡 𝑥; 𝑓𝑙𝑜𝑎𝑡 𝑦; } 𝑝;
𝑟𝑒𝑐𝑜𝑟𝑑 { 𝑖𝑛𝑡 𝑡𝑎𝑔; 𝑓𝑙𝑜𝑎𝑡 𝑥; 𝑓𝑙𝑜𝑎𝑡 𝑦; } 𝑞;
Answer:

SDT

S -> {top = new Evn(); offset = 0;}


D
D -> T id; {top.put(id.lexeme, T.type, offset);
offset += T.width}
D1
D -> ε
T -> int {T.type = interget; T.width = 4;}
T -> float {T.type = float; T.width = 8;}
T -> record ‘{‘
{Evn.push(top), top = new Evn();
Stack.push(offset), offset = 0;}
D ‘}’ {T.type = record(top); T.width = offset;
top = Evn.top(); offset = Stack.pop();}
Identifier types and relative addresses
line id type offset Evn

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

B5 10) if I > s goto(22)

B6 11) t2 = I * 4
12) ifFalse a[t2] goto(20)

B7 13) count = count + 1


14) j = 2 * i

B8 15) if j > n 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy