Title: Simplifications of ContextFree Grammars
1Simplifications of Context-Free Grammars
2A Substitution Rule
Equivalent grammar
Substitute
3A Substitution Rule
Substitute
Equivalent grammar
4In general
Substitute
equivalent grammar
5Nullable Variables
Nullable Variable
6Removing Nullable Variables
Example Grammar
Nullable variable
7Final Grammar
Substitute
8Unit-Productions
Unit Production
(a single variable in both sides)
9Removing Unit Productions
Observation
Is removed immediately
10Example Grammar
11Substitute
12Remove
13Substitute
14Remove repeated productions
Final grammar
15Useless Productions
16Another grammar
Not reachable from S
17contains only terminals
In general
if
then variable is useful
otherwise, variable is useless
18A production is useless if any of
its variables is useless
19Removing Useless Productions
Example Grammar
20First
find all variables that can produce strings with
only terminals
Round 1
Round 2
21Keep only the variables that produce terminal
symbols
(the rest variables are useless)
Remove useless productions
22Second
Find all variables reachable from
Use a Dependency Graph
not reachable
23Keep only the variables reachable from S
(the rest variables are useless)
Final Grammar
Remove useless productions
24Removing All
- Step 1 Remove Nullable Variables
- Step 2 Remove Unit-Productions
- Step 3 Remove Useless Variables
25Normal FormsforContext-free Grammars
26Chomsky Normal Form
Each productions has form
or
variable
variable
terminal
27Examples
Chomsky Normal Form
Not Chomsky Normal Form
28Convertion to Chomsky Normal Form
Not Chomsky Normal Form
29Introduce variables for terminals
30 Introduce intermediate variable
31Introduce intermediate variable
32Final grammar in Chomsky Normal Form
Initial grammar
33In general
From any context-free grammar (which doesnt
produce ) not in Chomsky Normal Form
we can obtain An equivalent grammar
in Chomsky Normal Form
34The Procedure
First remove Nullable variables Unit
productions
35Then, for every symbol
Add production
In productions replace with
New variable
36Replace any production
with
New intermediate variables
37Theorem
For any context-free grammar (which doesnt
produce ) there is an equivalent grammar in
Chomsky Normal Form
38Observations
- 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
39Greinbach Normal Form
All productions have form
symbol
variables
40Observations
- Greinbach normal forms are very good
- for parsing
- It is hard to find the Greinbach normal
- form of any context-free grammar
41Compilers
42Machine Code
Program
Add v,v,0 cmp v,5 jmplt ELSE THEN add x,
12,v ELSE WHILE cmp x,3 ...
v 5 if (vgt5) x 12 v while (x !3)
x x - 3 v 10 ......
Compiler
43Compiler
Lexical analyzer
parser
input
output
machine code
program
44A parser knows the grammar of the programming
language
45Parser
PROGRAM STMT_LIST STMT_LIST STMT
STMT_LIST STMT STMT EXPR IF_STMT
WHILE_STMT
STMT_LIST EXPR EXPR EXPR EXPR - EXPR
ID IF_STMT if (EXPR) then STMT
if (EXPR) then STMT else STMT WHILE_STMT
while (EXPR) do STMT
46The parser finds the derivation of a particular
input
derivation
Parser
input
E gt E E gt E E E gt 10 EE gt
10 2 E gt 10 2 5
E -gt E E E E INT
10 2 5
47derivation tree
derivation
E
E gt E E gt E E E gt 10 EE gt
10 2 E gt 10 2 5
E
E
10
E
E
2
5
48derivation tree
E
machine code
E
E
mult a, 2, 5 add b, 10, a
10
E
E
2
5
49Parsing
50Parser
input string
derivation
grammar
51Example
Parser
derivation
input
?
52Exhaustive Search
Phase 1
Find derivation of
All possible derivations of length 1
53(No Transcript)
54Phase 2
Phase 1
55Phase 2
Phase 3
56Final result of exhaustive search
(top-down parsing)
Parser
input
derivation
57Time complexity of exhaustive search
Suppose there are no productions of the form
Number of phases for string approx. w
58For grammar with rules
Time for phase 1
possible derivations
59Time for phase 2
possible derivations
60Time for phase w is 2w
A total of 2w possible derivations
61Total time needed for string
phase 1
phase w
phase 2
Extremely bad!!!
62For general context-free grammars
There exists a parsing algorithm that parses a
string in time
The CYK parser