Simplifications of Context-Free Grammars - PowerPoint PPT Presentation

About This Presentation
Title:

Simplifications of Context-Free Grammars

Description:

Title: Languages and Finite Automata Author: Costas Busch Last modified by: ENCS Created Date: 8/31/2000 1:12:33 AM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 52
Provided by: Cost104
Learn more at: http://web.cs.wpi.edu
Category:

less

Transcript and Presenter's Notes

Title: Simplifications of Context-Free Grammars


1
Simplifications of Context-Free Grammars

2
A Substitution Rule
Equivalent grammar
Substitute
3
A Substitution Rule
Substitute
Equivalent grammar
4
In general
Substitute
equivalent grammar
5
Nullable Variables
Nullable Variable
6
Removing Nullable Variables
Example Grammar
Nullable variable
7
Final Grammar
Substitute
8
Unit-Productions
Unit Production
(a single variable in both sides)
9
Removing Unit Productions
Observation
Is removed immediately
10
Example Grammar
11
Substitute
12
Remove
13
Substitute
14
Remove repeated productions
Final grammar
15
Useless Productions
16
Another grammar
Not reachable from S
17
contains only terminals
In general
if
then variable is useful
otherwise, variable is useless
18
A production is useless if any of
its variables is useless
19
Removing Useless Productions
Example Grammar
20
First
find all variables that can produce strings with
only terminals
Round 1
Round 2
21
Keep only the variables that produce terminal
symbols
(the rest variables are useless)
Remove useless productions
22
Second
Find all variables reachable from
Use a Dependency Graph
not reachable
23
Keep only the variables reachable from S
(the rest variables are useless)
Final Grammar
Remove useless productions
24
Removing All
  • Step 1 Remove ?-productions
  • Step 2 Remove Unit-productions
  • Step 3 Remove Useless productions

25
Normal FormsforContext-free Grammars

26
Chomsky Normal Form
Each productions has form
or
variable
variable
terminal
27
Examples
Chomsky Normal Form
Not Chomsky Normal Form
28
Conversion to Chomsky Normal Form
  • Example

Not Chomsky Normal Form
29
Introduce variables for terminals
30

Introduce intermediate variable
31
Introduce intermediate variable
32
Final grammar in Chomsky Normal Form
Initial grammar
33
In general
From any context-free grammar (which doesnt
produce ) not in Chomsky Normal Form
we can obtain An equivalent grammar
in Chomsky Normal Form
34
The Procedure
First remove Nullable variables Unit
productions
35
Then, for every symbol
Add production
In productions replace with
New variable
36
Replace any production
with
New intermediate variables
37
Theorem
For any context-free grammar (which doesnt
produce ) there is an equivalent grammar in
Chomsky Normal Form
38
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

39
Greibach Normal Form
All productions have form
symbol
variables
40
Examples
Greibach Normal Form
Not Greibach Normal Form
41
Conversion to Greibach Normal Form
Greibach Normal Form
42
Theorem
For any context-free grammar (which doesnt
produce ) there is an equivalent grammar in
Greibach Normal Form
43
Observations
  • Greibach normal forms are very good
  • for parsing
  • It is hard to find the Greibach normal
  • form of any context-free grammar

44
The CYK Parser
45
The CYK Membership Algorithm
Input
  • Grammar in Chomsky Normal Form
  • String

Output
find if
46
The Algorithm
Input example
  • Grammar
  • String

47
(No Transcript)
48
(No Transcript)
49
(No Transcript)
50
(No Transcript)
51
Therefore
Time Complexity
Observation
The CYK algorithm can be easily converted to a
parser (bottom up parser)
Write a Comment
User Comments (0)
About PowerShow.com