Documente Academic
Documente Profesional
Documente Cultură
Good Luck
Solution
to
Sub – Compiler Design
Class Test – I, 2010
1)
a) Compiler and Interpreter both are translators for high level languages. But they
have differences which are given below.
Compiler Interpreter
b) Regular Expression:
Regular expressions over åcan be defined recursively as follows:
1. Any terminal symbol (i.e. an element of å), L and f are regular
expressions.
2. The union of two regular expressions R1 and R 2, written as R1 + R2, is also
a regular expression.
3. The concatenation of two regular expressions R 1 and R 2, written as R 1 R2,
is also a regular expression.
4. The iteration (or closure) of a regular expression R, written as R *, is also a
regular expression.
5. If R is a regular expression, then (R) is also a regular expression.
6. A recursive application of the rules 1 -5 once or several times results into a
regular expression.
c) Non-deterministic Finite Automaton (NFA):
For example:
A given grammar is :
S®iCtS
S®iCtSeS
S®a
C®b
And a string w = i b t i b t a e a can be derived using leftmost deriva tion as
follows:
SÞiCtS
lm
ÞibtS
lm
Þ
lm
ibtiCtSeS
ÞibtibtSeS
lm
Þ
lm
ibtibtaeS
Þibtibtaea
lm
It is in the form of
Þ a1 Þ a2 Þ
S lm lm
× × × Þ an = w. Here all ai, 1≤ i ≤ n, is a left -sentential
lm lm
form of the given string.
Similarly, the rightmost derivation of the string is as follows:
ÞiCtS
S rm
ÞiCtiCtSeS
rm
Þ
rm
iCtiCtSea
ÞiCtiCtaea
rm
ÞiCtibtaea
rm
Þibtibtaea
rm
It is in the form of
Þ a1 Þ a2 rm
S rm Þ × × × rm
Þ an = w. Here all ai, 1≤ i ≤ n, is a right -sentential
rm
form of the given string. The rightmost derivation is also known as canonical
derivation.
LA SA LA
C S
®
C A
® C A
LB
C
LB LB
C L
® A
® C B
while-statement
relation relation
location ¬ exp
id(A)
exp + exp
exp relop exp exp relop exp
id(A) id(B)
const(2) id(B)
The intermediate code for the given string is as follows:
L1: if A ≤ B goto L2
T1 := 2 * B
T2 := T1 – 5
if A > T2 goto L2
A := A + B
goto L1
L2:
3) The given grammar is
E ® E + E | E * E | (E) | id
A given grammar is said to be ambiguous if it produces more than one parse tree for
some sentence. In other words, if a string is derivable in more than one ways using a
given grammar, then the given grammar is said to be ambiguous.
EÞE*E
ÞE+E*E
Þ id + E * E
Þ id + id * E
Þ id + id * id.
For the above two derivations we can have two different parse trees, which are shown
below.
E E
E + E E * E
E * E E + E
id id id id id id
d d d d d d
q2 a q3 Î
Î
Î
q0 Î q1 q6 q7
Î
Î
q4 b q5
a
q4 q5 Î
Î
Î
a b q2 Î q3 q8 q9
q0 q1
Î
Î
q6 b q7
Î
c) The regular expression is (a* | b*)*a
The Î-NFA for the given regular expression is as below:
q12
q11
q10
Î Î
q5 q9
Î
Î
Î q4 q8
Î Î
a Î b Î Î
q3 q7
Î Î
q2 q6
Î Î
q1
Î
q0
d) The regular expression is (a | b)*a (a | b)+
The Î-NFA for the given regular expression is as below:
q14
Î Î
q13
Î Î
q15 q17
q10
q12
a b Î
a
b
q16 q18
q9
q11
Î Î Î
Î
Î q19
q8
Î
a
q7 q20
q6
Î Î
q3 q5
Î
Î a b
q2 q4
Î Î
q1
Î
q0
e) The regular expression is (a | b)+abb.
The Î-NFA for the given regular expression is as below:
b q14
b q15
q13
a
q12
q11
Î Î
q8
q10
a
Î b
Î
q7
q9
Î Î
q6
q5
Î Î
q2 q4
a b
q1 q3
Î Î
q0
5) The given regular expression is (a | b)*abb. The Î-NFA for the given regular
expression is given below:
q2 a q3 Î
Î
Î a q8
Î q6 q7
q0 q1
Î
Î
q4 b q5 b
q9
Î
b
q10
Now let us minimize the above DFA to get an equivalent minimized DFA.
Here the 0-equivalent classes will be given by Q10={E}, Q20={A, B, C, D}, which is a
set of all final states and a set of all non-final states.
Hence the set of 0-equivalent classes is given by
P 0={ Q10, Q20}.
Now for 1-equivalent classes we got,
Q11={E}, Q21={A, B, C}, Q31={D}.
Hence the set of 1-equivalent classes is given by
P 1={ Q11, Q21, Q31}, where Q11, Q21, Q31 are given above.
Now for 2-equivalent classes we got,
Q12={E}, Q22={A, C}, Q32={B}, Q42={D}.
Hence the set of 2-equivalent classes is given by
P 2={ Q12, Q22, Q32, Q42}, where Q12, Q22, Q32, Q42 are given above.
Now for 3-equivalent classes we got,
Q13={E}, Q23={A, C}, Q33={B}, Q43={D}.
Hence the set of 3-equivalent classes is given by
P 3={ Q13, Q23, Q33, Q43}, where Q13, Q23, Q33, Q43 are given above.
We can see that P2=P3. Hence P2 is the set of equivalence classes.
So, now the transition table for minimized DFA is given below.
State Input
a b
® A B A
B B D
D B E
E* B A
a b
A B D
b a
a
b
E