Professional Documents
Culture Documents
of
Context-Free Grammars
1
A Substitution Rule
Equivalent
grammar
S aB
S aB | ab
A aaA
Substitute A aaA
A abBc Bb A abBc | abbc
B aA
B aA
Bb
2
A Substitution Rule
S aB | ab
A aaA
A abBc | abbc
B aA
Substitute
B aA
S aB | ab | aaA
A aaA Equivalent
A abBc | abbc | abaAc grammar
3
In general:
A xBz
B y1
Substitute
B y1
equivalent
A xBz | xy1z grammar
4
Nullable Variables
production : A
5
Removing Nullable Variables
Example Grammar:
S aMb
M aMb
M
Nullable variable
6
Final Grammar
S aMb
S aMb
Substitute S ab
M aMb M
M aMb
M
M ab
7
Unit-Productions
Unit Production: A B
8
Removing Unit Productions
Observation:
A A
Is removed immediately
9
Example Grammar:
S aA
Aa
A B
BA
B bb
10
S aA
S aA | aB
Aa
Substitute Aa
A B A B B A| B
BA
B bb
B bb
11
S aA | aB S aA | aB
Aa Remove Aa
B A| B BB BA
B bb B bb
12
S aA | aB
S aA | aB | aA
Aa Substitute
BA Aa
BA
B bb
B bb
13
Remove repeated productions
Final grammar
S aA | aB | aA S aA | aB
Aa Aa
B bb B bb
14
Useless Productions
S aSb
S
SA
A aA Useless Production
S A aA aaA aa aA
15
Another grammar:
SA
A aA
A
B bA Useless Production
Not reachable from S
16
In general: contains only
terminals
if S xAy w
w L(G )
17
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
Example Grammar:
S aS | A | C
Aa
B aa
C aCb
19
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 }
20
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
21
Second: Find all variables
reachable from S
S aS | A
Aa S A B
B aa not
reachable
22
Keep only the variables
reachable from S
(the rest variables are useless)
Final Grammar
S aS | A
S aS | A
Aa
Aa
B aa
23
Removing All
24
Normal Forms
for
Context-free Grammars
25
Chomsky Normal Form
A BC or Aa
26
Examples:
S AS S AS
S a S AAS
A SA A SA
Ab A aa
Chomsky Not Chomsky
Normal Form Normal Form
27
Convertion to Chomsky Normal Form
Example: S ABa
A aab
B Ac
Not Chomsky
Normal Form
28
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
29
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
30
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 31
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 32
In general:
we can obtain:
An equivalent grammar
in Chomsky Normal Form
33
The Procedure
First remove:
Nullable variables
Unit productions
34
Then, for every symbol a:
Add production Ta a
New variable: Ta
35
Replace any production A C1C2 Cn
with A C1V1
V1 C2V2
Vn2 Cn1Cn
37
Observations
38
Greinbach Normal Form
A a V1V2 Vk k 0
symbol variables
39
Observations
40
Compilers
41
Machine Code
Program Add v,v,0
v = 5; cmp v,5
if (v>5) jmplt ELSE
x = 12 + v; THEN:
while (x !=3) { Compiler
add x, 12,v
x = x - 3; ELSE:
v = 10; WHILE:
} cmp x,3
...... ...
42
Compiler
Lexical
parser
analyzer
input output
machine
program
code
43
A parser knows the grammar
of the programming language
44
Parser
PROGRAM STMT_LIST
STMT_LIST STMT; STMT_LIST | STMT;
STMT EXPR | IF_STMT | WHILE_STMT
| { STMT_LIST }
45
The parser finds the derivation
of a particular input
derivation
Parser
input E => E + E
E -> E + E
=> E + E * E
10 + 2 * 5 |E*E
=> 10 + E*E
| INT
=> 10 + 2 * E
=> 10 + 2 * 5
46
derivation tree
derivation
E
E => E + E E + E
=> E + E * E
=> 10 + E*E 10
E * E
=> 10 + 2 * E
=> 10 + 2 * 5 2 5
47
derivation tree
E machine code
E + E mult a, 2, 5
add b, 10, a
10
E * E
2 5
48
Parsing
49
Parser
input
grammar derivation
string
50
Example:
Parser
S SS derivation
input
S aSb
aabb ?
S bSa
S
51
Exhaustive Search
S SS | aSb | bSa |
53
Phase 2 S SS | aSb | bSa |
S SS SSS
S SS aSbS aabb
Phase 1 S SS bSaS
S SS S SS S
S aSb S aSb aSSb
S aSb aaSbb
S aSb abSab
S aSb ab 54
S SS | aSb | bSa |
Phase 2
S SS SSS
S SS aSbS aabb
S SS S
S aSb aSSb
S aSb aaSbb
Phase 3
S aSb aaSbb aabb
55
Final result of exhaustive search
(top-down parsing)
Parser
S SS
input
S aSb
aabb
S bSa
S
derivation
A
A B
Number of phases for string w : approx. |w|
57
For grammar with k rules
k possible derivations
58
Time for phase 2: 2
k
2 possible derivations
k
59
Time for phase |w| is 2|w|:
60
Total time needed for string w:
k k k
2 | w|
Extremely bad!!!
61
For general context-free grammars:
62