Professional Documents
Culture Documents
Context-Free Grammars
I. Nullable Variables(-productions)
II. Unit-Productions
I. Normal Forms
1
1. Unit-Productions
Unit Production: A B
(a single variable in both sides)
2
Example Grammar:
S aA
Aa
A B
BA
B bb
3
S aA
S aA | aB
Aa
Substitute Aa
A B A B B A| B
BA
B bb
B bb
4
S aA | aB S aA | aB
Aa Remove Aa
B A| B BB BA
B bb B bb
5
S aA | aB
S aA | aB | aA
Aa Substitute
BA Aa
BA
B bb
B bb
6
Remove repeated productions
Final grammar
S aA | aB | aA S aA | aB
Aa Aa
B bb B bb
7
2. Useless Productions
S aSb
S
SA
A aA Useless Production
S A aA aaA aa aA
8
In general:
if S xAy w
9
Removing Useless Productions
Example Grammar:
S aS | A | C
Aa
B aa
C aCb
10
First: find all variables that can produce
strings with only terminals
S aS | A | C Round 1: { A, B}
Aa SA
B aa
C aCb Round 2: { A, B, S }
11
Keep only the variables
that produce terminal symbols: { A, B, S }
(the rest variables are useless)
S aS | A | C
Aa S aS | A
B aa Aa
C aCb B aa
Remove useless productions
12
Second: Find all variables
reachable from S
S aS | A
Aa S A B
B aa not
reachable
13
Keep only the variables
reachable from S
(the rest variables are useless)
Final Grammar
S aS | A
S aS | A
Aa
Aa
B aa
14
Another grammar:
SA
A aA
A
B bA Useless Production
Not reachable from S
15
A production A x is useless
if any of its variables is useless
S aSb
S Productions
Variables SA useless
useless A aA useless
useless B C useless
production : A
Example Grammar:
S aMb
M aMb
M
Nullable variable
18
Final Grammar
S aMb
S aMb
Substitute S ab
M aMb M
M aMb
M
M ab
19
Removing All
20
Normal Forms
for
Context-free Grammars
21
Chomsky Normal Form
A BC or Aa
S AS S AS
S a S AAS
A SA A SA
Ab A aa
Chomsky Not Chomsky
Normal Form Normal Form
23
Convertion to Chomsky Normal Form
Example: S ABa
A aab
B Ac
Not Chomsky
Normal Form
24
Introduce variables for terminals: Ta , Tb , Tc
S ABTa
S ABa A TaTaTb
A aab B ATc
B Ac Ta a
Tb b
Tc c
25
Introduce intermediate variable: V1
S AV1
S ABTa
V1 BTa
A TaTaTb
A TaTaTb
B ATc
B ATc
Ta a
Ta a
Tb b
Tb b
Tc c
Tc c
26
Introduce intermediate variable: V2
S AV1
S AV1
V1 BTa
V1 BTa
A TaV2
A TaTaTb
V2 TaTb
B ATc
B ATc
Ta a
Ta a
Tb b
Tb b
Tc c
Tc c 27
Final grammar in Chomsky Normal Form:
S AV1
V1 BTa
A TaV2
Initial grammar
V2 TaTb
S ABa B ATc
A aab Ta a
B Ac Tb b
Tc c 28
In general:
29
The Procedure
First remove:
Nullable variables
Unit productions
with A C1V1
V1 C2V2
Vn2 Cn1Cn
31
Theorem:
For any context-free grammar
(which doesn’t produce )
there is an equivalent grammar
in Chomsky Normal Form
Observations
• Chomsky normal forms are good
for parsing and proving theorems
• It is very easy to find the Chomsky normal
form for any context-free grammar
32
Greibach Normal Form
A a V1V2 Vk k 0
symbol variables
33
Examples:
S cAB
S abSb
A aA | bB | b
S aa
Bb
34
Conversion to Greinbach Normal Form:
S aTb STb
S abSb S aTa
S aa Ta a
Tb b
Greinbach
Normal Form
35
Theorem: For any context-free grammar
(which doesn’t produce )
there is an equivalent grammar
in Greibach Normal Form
Observations
• Greinbach normal forms are very good
for parsing
• It is hard to find the Greinbach normal
form of any context-free grammar
36