CD End Sem QP Answer
CD End Sem QP Answer
: _____________
Instructions: All questions are compulsory. All the subparts of a question are to be attempted together.
Marks
Q1 a) What are the three components of Lexical definitions? [2]
Lexical definitions consist of regular definitions, priority rules and maximal munch
principle
b) Explain briefly the difference between a Syntax directed definition and translation [2]
scheme.
c) Divide the following C++ program into appropriate lexemes. Which lexemes should [2]
get associated lexical values? What should those values be?
float limitedSquare(x){float x;
/* returns x-squared, nut never more than 100 */
return (x <= -10.0 || x >= 10.0) ? 100 : x*x;
}
1|Page
d) Compute the First and Follow set of following grammar: [2]
𝑆 −> 𝑎𝐵𝐷ℎ
𝐵 −> 𝑐𝐶
𝐶 −> 𝑏𝐶 | Є
𝐷 −> 𝐸𝐹
𝐸 −> 𝑔|Є
𝐹 −> 𝑓|Є
Answer:
FIRST set
FIRST(S) = { a }
FIRST(B) = { c }
FIRST(C) = { b , Є }
FIRST(D) = FIRST(E) U FIRST(F) = { g, f, Є }
FIRST(E) = { g , Є }
FIRST(F) = { f , Є }
FOLLOW Set
FOLLOW(S) = { $ }
FOLLOW(B) = { FIRST(D) – Є } U FIRST(h) = { g , f , h }
FOLLOW(C) = FOLLOW(B) = { g , f , h }
FOLLOW(D) = FIRST(h) = { h }
FOLLOW(E) = { FIRST(F) – Є } U FOLLOW(D) = { f , h }
FOLLOW(F) = FOLLOW(D) = { h }
Answer: First(aSa) = a
First(bS) = b
First(c) = c
All are mutually disjoint i.e no common terminal between them, the given grammar
is LL(1). As the grammar is LL(1) so it will also be LR(1) as LR parsers are more
powerful then LL(1) parsers. and all LL(1) grammar are also LR(1)
Note: There is no need to generate the LR parse table for the question part (ii)
2|Page
Q3 Consider the following Grammar and answer the following sub-questions. [2+2+4]
𝑆 →𝐿 =𝑅|𝑅
𝐿 → ∗ 𝑅 | 𝑖𝑑
𝑅→𝐿
i. State whether the statement “Every SLR(1) is unambiguous, but every unambiguous
grammar is not SLR(1)” is true or false? Justify your answer.
ii. State whether the given grammar is ambiguous or unambiguous? Justify your
answer.
iii. State whether the given grammar is SLR(1) or not? Justify your answer.
Answers
i. The statement is true. There are some unambiguous grammars which are not
SLR(1). This is due to shift-reduce conflicts. These conflicts arises from the fact
that SLR parser construction method is not powerful enough to remember the
left context to decide what action the parser should take on some input.
ii. Grammar is unambiguous. A grammar is said to be ambiguous if there exists
more than one leftmost derivation or more than one rightmost derivation or more
than one parse tree for the given input string. If the grammar is not ambiguous,
then it is called unambiguous.
iii.
3|Page
Q4 Define the following terms with atleast one example for each: [8]
Synthesized attributes vs Inherited attributes
S-attribute vs L-attribute definitions
Peephole Optimization
Abstract Syntax tree vs Directed Acyclic Graph
Q5 Explain the Syntax directed translation of expression into 3-address code. Write the [4+4]
equivalent grammar and associated rules for translation. Then, show the process of
generating the 3-address code for expression 𝑎 = 𝑏 ∗ −𝑐 + 𝑏 ∗ −𝑐 using the same syntax
directed translation.
4|Page
Q6 The below figure is a simple matrix-multiplication program. [4+2+2]
5|Page
a) Translate the program into three-address statements of the type we have been using in
this section. Assume the matrix entries are numbers that require 8 bytes, and that
matrices are stored in row-major order.
b) Construct the flow graph for your code from (a).
c) Identify the loops in your flow graph from (b).
loops
{B4, B5}
{B12, B13}
6|Page