of 51/51
Fall 2004 COMP 335 1 Simplifications of Context-Free Grammars
• date post

13-Jan-2022
• Category

## Documents

• view

1

0

Embed Size (px)

### Transcript of Simplifications Context-Free Grammars

Microsoft PowerPoint - grahneclass9Simplifications of
Context-Free Grammars
A Substitution Rule
→ → → → →
A Substitution Rule
In general:
Nullable Variables
Removing Nullable Variables
λ→M λ→
Unit-Productions
Fall 2004 COMP 335 9
Removing Unit Productions
Example Grammar:
→ → → → →
→ → → → →
Remove
Substitute AB →
bbB aA
Remove repeated productions
Useless Productions
aAA AS
S aSbS
bAB A
aAA AS
In general:
Fall 2004 COMP 335 18
A production is useless if any of its variables is useless
xA→
Removing Useless Productions
Fall 2004 COMP 335 20
First: find all variables that can produce strings with only terminals
aCbC aaB aA
Keep only the variables that produce terminal symbols:
aCbC aaB aA
Remove useless productions
Second: Find all variables reachable from
aaB aA
Keep only the variables reachable from S
aaB aA
Remove useless productions
Removing All
Normal Forms for
Chomsky Normal Form
Examples:
Example:
AcB aabA ABaS
Introduce intermediate variable:
Introduce intermediate variable:
Final grammar in Chomsky Normal Form:
cT bT aT ATB
Fall 2004 COMP 335 33
From any context-free grammar (which doesn’t produce ) not in Chomsky Normal Form
we can obtain: An equivalent grammar in Chomsky Normal Form
In general:
The Procedure
First remove:
Nullable variables
Unit productions
In productions: replace with a aT
New variable: aT
with
Fall 2004 COMP 335 37
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
Fall 2004 COMP 335 39
Greibach Normal Form
Examples:
aaS abSbS
bT aT
aTS STaTS
Fall 2004 COMP 335 42
Theorem: For any context-free grammar (which doesn’t produce ) there is an equivalent grammar in Greibach Normal Form
λ
Observations
• Greibach normal forms are very good for parsing
• It is hard to find the Greibach normal form of any context-free grammar
Fall 2004 COMP 335 44
The CYK Parser
The CYK Membership Algorithm
• String
Output:
The Algorithm
• Grammar :G
→ → → → →
aa ab bb bb
a A
a A
b B
b B
b B
→ → → → →
a A
a A
b B
b B
b B
→ → → → →
a A
a A
b B
b B
b B
→ → → → →
Therefore: )(GLaabbb ∈
Time Complexity: 3|| w
The CYK algorithm can be easily converted to a parser (bottom up parser)
Observation: