Professional Documents
Culture Documents
Contents
2. Right-most Derivation
Since there are two parse trees for a single string "a+a*a", the
grammar G is ambiguous
Solution:
Nullable: A,C
P’:
S →AaC | aC | Aa | a
A→a
C→b
EX: For the exercise on slide 17, find find G’ λ free CFG.
Solution:
1. Elimination of unit production:
U(S) = {S → A } , N(S) = {S → ABA }
U(A)={A → B} , N(A) ={A → aA | a }
U(B)= Ø
S=>+A and S=>+B
S=>+A => aA | a Є N(A)
S=>+B => bB | b Є N(B)
A=>+B => bB | b
June 15, 2022 FLAT Chapter Five 26
◼ Thus, the grammar without unit productions
S → aA | a|bB|b|ABA
A → aA | a | bB|b
B → bB | b
2. Elimination of secondary productions:
The secondary productions are:
S → aA | bB,
A → aA| bB,
B → bB
Introduce Aa and Aa ->a
Ab and Ab ->b
Then, the equivalent grammar without secondary productions is:
S → Aa A | a| Ab B|b|ABA
A → Aa A | a | Ab B|b
B → Ab B | b
Aa ->a
Ab ->b
June 15, 2022 FLAT Chapter Five 27
3. Elimination of tertiary production:
The tertiary production is:
S →ABA
Replace S →ABA with:
S →AB’
B’ →BA
Therefore, the grammar in CNF is :
S → Aa A | a| Ab B|b|AB’
B’ → BA
A → Aa A | a | Ab B|b
B → Ab B | b
Aa ->a
Ab ->b
A3 -> A2A2 | b
A3-> (A3A1|a)A2 | b
A3->A3A1A 2| aA2 | b
G:
A1 -> A2A1|A2A3
A2 ->A3A1|a
A3->A3A1A2| aA2 | b
Removing the left recursive production by using
theorem 2:
A3-> aA2 | b | aA2 A3’| b A3’
A3’->A1A2 | A1A2 A3’
June 15, 2022 FLAT Chapter Five 36
G: A1 -> A2A1|A2A3
A2 ->A3A1|a
A3-> aA2 | b | aA2 A3’| b A3’
A3’->A1A2 | A1A2 A3’
4. Replace Aj in Ai->Ajα or Ai’ → Ajα using theorem 1.
A2 ->(aA2 | b | aA2 A3’| b A3’ ) A1|a
Or
A2 ->aA2 A1 | bA1 | aA2 A3’ A1| b A3’ A1|a
A1 -> A2A1|A2A3
Or
A1 -> A2A1|A2A3
Or A1 -> (aA2 A1 | bA1 | aA2 A3’ A1| b A3’ A1|a )A1|(aA2 A1 | bA1 | aA2
A3’ A1| b A3’ A1|a)A3 **
A3’-> (**)A2 | (**)A2 A3’
June 15, 2022 FLAT Chapter Five 37
So, the grammar in GNF is:
A1 -> (aA2 A1 | bA1 | aA2 A3’ A1| b A3’ A1|a )A1|(aA2 A1 | bA1 | aA2 A3’ A1|
b A3’ A1|a)A3 **
A2 ->aA2 A1 | bA1 | aA2 A3’ A1| b A3’ A1|a
A3-> aA2 | b | aA2 A3’| b A3’
A3’-> (**)A2 | (**)A2 A3’