The document outlines the steps to convert a context-free grammar into Chomsky normal form or Greibach normal form. It begins with an introduction to grammars and provides an example. It then outlines the steps as: 1) introducing Chomsky normal form, 2) performing preliminary simplifications including eliminating useless, epsilon, and unit productions, 3) performing final steps, 4) introducing Greibach normal form, 5) providing an algorithm and example, and 6) concluding with a summary.
Presentation Outline
2
• Introduction
•Chomsky normal form
• Preliminary simplifications
• Final steps
• Greibach Normal Form
• Algorithm (Example)
• Summary
3.
Introduction
3
Grammar: G =(V, T, P, S)
T = { a, b }T = { a, b }Terminals
V = A, B, CV = A, B, CVariables
SSStart Symbol
P = S → AP = S → AProduction
4.
Introduction
4
Grammar example
S →aBSc
S → abc
Ba → aB
Bb → bb
L = { an
bn
cn
| n ≥ 1 }L = { an
bn
cn
| n ≥ 1 }
S aBSc aBabcc aaBbcc aabbcc⇒ ⇒ ⇒ ⇒
5.
Introduction
5
Context free grammar
Thehead of any production contains only one
non-terminal symbol
S → P
P → aPb
P → ε
L = { an
bn
| n ≥ 0 }L = { an
bn
| n ≥ 0 }
6.
Presentation Outline
6May 27,2009
• Introduction
• Chomsky normal form
• Preliminary simplifications
• Final simplification
• Greibach Normal Form
• Algorithm (Example)
• Summary
7.
Chomsky Normal Form
7
A→ BC
A → α
A context free grammar is said to be in Chomsky
Normal Form if all productions are in the following
form:
• A, B and C are non terminal symbols
• α is a terminal symbol
8.
Presentation Outline
8
• Introduction
•Chomsky normal form
• Preliminary simplifications
• Final steps
• Greibach Normal Form
• Algorithm (Example)
• Summary
9.
Preliminary Simplifications
9
Eliminate UselessSymbolsEliminate Useless Symbols1
Eliminate ε productionsEliminate ε productions2
Eliminate unit productionsEliminate unit productions3
There are three preliminary simplifications
10.
Preliminary Simplifications
10
Eliminate UselessSymbols
We need to determine if the symbol is useful by
identifying if a symbol is generating and is reachable
• X is generating if X ω for some terminal string ω.
• X is reachable if there is a derivation X αXβ
for some α and β
⇒
⇒
*
*
11.
Preliminary Simplifications
11May 27,2009
Example: Removing non-generating symbols
S → AB | a
A → b
S → AB | a
A → b Initial CFL grammar
S → AB | a
A → b
S → AB | a
A → b
Identify generating symbols
S → a
A → b
S → a
A → b
Remove non-generating
Preliminary Simplifications
13
The orderis important.
S → AB | a
A → b
S → AB | a
A → b
Looking first for non-reachable symbols and then
for non-generating symbols can still leave some
useless symbols.
S → a
A → b
S → a
A → b
14.
Preliminary Simplifications
14
Finding generatingsymbols
If there is a production A → α, and every symbol
of α is already known to be generating. Then A is
generating
S → AB | a
A → b
S → AB | a
A → b
We cannot use S → AB
because B has not been
established to be generating
15.
Preliminary Simplifications
15
Eliminate UselessSymbolsEliminate Useless Symbols1
Eliminate ε productionsEliminate ε productions2
Eliminate unit productionsEliminate unit productions3
There are three preliminary simplifications
16.
Preliminary Simplifications
16
Eliminate εProductions
• In a grammar ε productions are convenient but
not essential
• If L has a CFG, then L – {ε} has a CFG
Nullable variable
A εA ε⇒*
17.
Preliminary Simplifications
17
If Ais a nullable variable
• Whenever A appears on the body of a production
A might or might not derive ε
S → ASA | aB
A → B | S
B → b | ε
Nullable: {A, B}
18.
Preliminary Simplifications
18
• Createtwo version of the production, one with
the nullable variable and one without it
• Eliminate productions with ε bodies
S → ASA | aB
A → B | S
B → b | ε
S → ASA | aB | AS | SA | S | a
A → B | S
B → b
Eliminate ε Productions
19.
Preliminary Simplifications
19
• Createtwo version of the production, one with
the nullable variable and one without it
• Eliminate productions with ε bodies
S → ASA | aB
A → B | S
B → b | ε
S → ASA | aB | AS | SA | S | a
A → B | S
B → b
Eliminate ε Productions
20.
Preliminary Simplifications
20
• Createtwo version of the production, one with
the nullable variable and one without it
• Eliminate productions with ε bodies
S → ASA | aB
A → B | S
B → b | ε
S → ASA | aB | AS | SA | S | a
A → B | S
B → b
Eliminate ε Productions
21.
Preliminary Simplifications
21
Eliminate UselessSymbolsEliminate Useless Symbols1
Eliminate ε productionsEliminate ε productions2
Eliminate unit productionsEliminate unit productions3
There are three preliminary simplifications
22.
Preliminary Simplifications
22
Eliminate unitproductions
A unit production is one of the form A → B where both
A and B are variables
A BA B⇒*
A → B, B → ω, then A → ω
Identify unit pairs
23.
Eliminate unit productions
•Consider the CFG
S->0A | 1B|C
A-> 0S|00
B->1|A
C->01
remove unit productions.
S-> C
B-> A unit productions
S->0A|1B|01
A->0S|00
B->1|0S|00
May 27, 2009 23