Assignment Solution Chapter 3 -...
Transcript of Assignment Solution Chapter 3 -...
Assignment Solution
Chapter 3
Q 1) Write a regular definitions for the following languages:
a) all strings beginning and ending with b, let Σ = {a, b}.
L- > b | b(a|b)*b
b) all strings containing exactly three 1s, let Σ = {0, 1}.
L –> 0* 1 0* 1 0*1 0*
c) All strings of a’s and b’s that do not contain the substring abb, let Σ = {a, b}.
L –> b* (a (|b))*
d) String of a`s and b`s that contains odd number of b’s , let Σ = {a, b}.
L –> a* b (a* b a* b)* a*
e) All strings of a's and b's with an even number of a's and an odd number of b's , let Σ = {a, b}.
L –> (aa)* b (bb)* (aa)*
f) All strings of digits that contain no leading zeroes
num –> 1 | 2 | … | 9
digit –> 0 | num
digits –> 0 | num digit*
g) All strings of digits that represent even numbers
even –> 0 | 2 | 4 | 6 | 8
digits –> even | [1-9] [0-9]* even
h) All strings of digits such that all the 5’s occur before all the 8’s
dignot8 –> 0 | 1 |2 | 3 | 4 | 5| 6 | 7 | 9
dignot5 –> 0 | 1 |2 | 3 | 4 | 6 | 7 | 8| 9
digits –> dignot8* dignot5*
The Islamic University of Gaza
Faculty of Engineering
Dept. of Computer Engineering
ECOM5060:Compiler Design
Eng. Ibraheem Lubbad
Q 2) Construct an NFA that recognizes the regular expressions a* (a|b) aa , and find the DFA:
A = ε-closure({0}) = {0,1,2,3,4}
move(A,a) ={0 , 5} - > ε-closure({0,5}) ={0,5,1,2,3,4} =B move(A,b) ={ 5} - > ε-closure({5}) ={5} =C
move(B,a) = {0,5,6 } -> ε-closure( {0,5,6 } ) = {0,5,6,1,2,3,4} = D move(B,b) = {5 } -> ε-closure({5}) = {5} = C move(C,a) = {6 } -> ε-closure({6 } ) = {6} = E move(C,b) ={ } -> ε-closure({}) = {}
move(D,a) = { 0 , 5, 6, 7 } -> ε-closure({ 0 , 5, 6, 7 } ) = { 0 , 5, 6, 7 ,1 ,2,3,4} = F move(D,b) = {5} -> ε-closure({5}) = {5} = C move(E,a) = { 7 } -> ε-closure({ 7 } ) = { 7} = G move(E,b) = {} move(F,a) = { 0,5,6,7 } -> ε-closure({ 0 , 5, 6, 7 } ) = F move(F,b) = {5} -> ε-closure({5}) = C move(G,a) = { } move(G,b) = { }
7 0 2
4
5 6
1
3
a
b
a a
a
A B D F
G E C
a a a
b b b b a
a a
a|b b
b
a|b
b b
Q 3) Construct the DFA from the following NFA:
A = ε-closure({0}) = {0 }
move(A,a) = {1,2,3} - > ε-closure( {1,2,3} ) ={1,2,3} =B move(A,b) = { 2 ,3} - > ε-closure( {2,3} ) ={2,3} =C
move(B,a) ={1,2,4} - > ε-closure( {1,2,4} ) ={1,2,4} =D move(B,b) ={ 2,3,4} - > ε-closure( {2,3,4} ) ={2,3,4} =E
move(C,a) ={4} - > ε-closure( {4} ) ={4} = F move(C,b) ={ 2 , 3, 4} - > E
move(D,a) ={ 1 , 2 } - > ε-closure( {1,2} ) ={1,2} =G move(D,b) ={ 2 , 3 , 4} - > E
move(E,a) ={4} - > F move(E,b) ={ 2,3,4} - > E move(F,a) ={} move(F,b) ={ }
move(G,a) ={1,2} - > G move(G,b) ={ 2,3, 4} - > E
0
1
2
3
4
1
a
b
b
b
b
a
a b
A ={0 } B C
B ={1,2,3} D E
C={2,3} F E
D= {1,2,4} G E
E={2,3,4} F E
F={4} G={1,2} G E
Chapter 4
Q 1) Eliminate left recursion from the following grammars:
a) S -> A A | 0
A -> S S | 1
There is no immediate left recursion but we considering the ordering S, A, so substitute S in A
S -> A A | 0
A -> A A S | 0 S | 1
We don’t need to substitute the second S since first (A) become terminal
S -> A A | 0
A -> 0 S A’ | 1 A’
A’ -> A S A’ |
b) S - > B r | a
B- > C s | b
C - > S t | c
There is two level derivation
Thus, C -> B r t | a t | c => C -> C s r t | b r t | a t| c. there is left recursion.
Now eliminate left recursion from C -> C s r t | b r t | a t| c
S - > B r | a
B- > C s | b
C -> b r t C' | a t C' | c C'
C' -> s r t C' | ϵ
Q 2) Eliminate Left factoring from the following grammars:
a) S -> a SS b S | a S a S b | a b b | b
S -> a S’ | b
S’ -> SSbS | SaSb | bb
A ‘ -> S S’’ |bb
S’’ -> S b S | a S b
b) S -> aAb |aB
A-> a|ab
B -> ccd |ddc
S - > a S’
S’ - > Ab | B
A - > a A’
A’ - > b |
B -> ccd |ddc
c) Check the following grammar is ambiguous if yes make it unambiguous?
E -> int | int + E | int – E | E – (E)
Derive: int – (int+int)
E -> E – ( E ) -> int – ( E ) - > int – ( int + E ) - > int - (int +int)
E - > Int – ( E ) - > int – (int + E) - > int – (int + int)
two leftmost derivations for the sentence: int – (int+int)
Yse , ambiguous
E -> int E’ | E – ( E )
E ‘ - > +E | -E |
Q3) Compute first and follow for following grammar:
a) S -> S r A | A A -> A a B | B B -> n B | ( S ) | t | d
Note first (S) - > first (A) - > first (B) –> {n , ( , t , d}
First Follow
S -> S r A | A
{ n , ( , t , d } { $ , r ) }
A -> A a B | B
{ n, ( ,t ,d } { a, $ , r , ) }
B -> n B | ( S ) | t | d
{ n, ( ,t ,d } { a, $ , r , ) }
b) A -> BCm | gDB B -> bCDE | ϵ C -> DaB | ma | DB D -> dD | ϵ E -> gAf | m
First Follow
A -> BCm | gDB
{ g , b , m , d , a } { $ , f }
B -> bCDE | ϵ
{ b , ϵ } { m , d , a , b , $ , f, g }
C -> DaB | ma | DB { m , d , a , b , ϵ } { m , d ,g }
D -> dD | ϵ
{ d , ϵ } { b , a ,g , m , $ , f , d }
E -> gAf | m { g , m } { m , d ,a , b , $ ,f , g }
c) S -> a = A ; A -> a = A | E E -> E + P | P P -> a | ( a ; a ) | ( A )
First Follow
S -> a = A ; { a }
{ $ }
A -> a = A | E { a , ( } { ; , ) }
E -> E + P | P
{ a , ( } { ; , ) , + }
P -> a | ( a ; a ) | ( A ) { a , ( } { ; , ) , + }
Chapter 5:
Q 1) Eliminate left recursion:
E → E1 + T {E.v:= E1.v + T.v}
E → E1 - T {E.v:= E1.v - T.v}
E → T {E.v:= T.v}
T → (E) {T.v:= E.v}
T → num {T.v:= num.v}
E → T {R.i := T.v } R {E.v := R.s }
R → +T {R 1.i := R.i + T.v } R1 { R.s := R1.s }
R → -T {R 1.i := R.i - T.v } R1 { R.s := R1.s }
R → {R.s := R.i }
T → ( E ) { T.v := E.v}
Q 2) Write the SDD for while-statements that will generate the intermediate code for a typical
programming-language construct: a form of while-statement.
S while ( C ) S1
S -> while ( C ) S1 L1 = new(); L2 = new(); S1.next= L1; C.false = S.next; C.true = L2; S.code = label || L1 || C.code || label || L2 || Sl.code
End