Cnf&Gnf Forms
-
Upload
gandhi-jabakumar -
Category
Documents
-
view
247 -
download
0
Transcript of Cnf&Gnf Forms
-
7/23/2019 Cnf&Gnf Forms
1/27
Chomsky and Greibach Normal
FormsTeodor Rus
The University of Iowa, Department of Computer Science
Computation Theory p.1/2
-
7/23/2019 Cnf&Gnf Forms
2/27
Simplifying a CFG
It is often convenient to simplify a CFG; One of the simplest and most useful simplified
forms of CFG is called the Chomsky normal
form;
Another normal form usually used in algebraicspecifications is Greibach normal form.
Note the difference between grammar cleaning and grammar simplification!
Computation Theory p.2/2
-
7/23/2019 Cnf&Gnf Forms
3/27
Fact
Normal forms are useful when more advanced topics
in computation theory are approached, as we shall see
further.
Computation Theory p.3/2
-
7/23/2019 Cnf&Gnf Forms
4/27
Definition
A context-free grammarGis in Chomsky normalform if every rule is of the form:
A BCA a
where a is a terminal, A, B,C are nonterminals, andB,Cmay not be the start variable (the axiom).
Computation Theory p.4/2
-
7/23/2019 Cnf&Gnf Forms
5/27
Observation
The ruleS , whereSis the start variable, is notexcluded from a CFG in Chomsky normal form.
Computation Theory p.5/2
-
7/23/2019 Cnf&Gnf Forms
6/27
Theorem 2.9
Any context-free language is generated by acontext-free grammar in Chomsky normal form.
Proof idea: Show that any CFGG can be converted into a CFGG in Chomsky
normal form;
Conversion procedure has several stages where the rules that violate
Chomsky normal form conditions are replaced with equivalent rules
that satisfy these conditions;
Order of transformations:(1) add a new start variable, (2) eliminate all
-rules, (3) eliminate unit-rules, (4) convert other rules;
Check that the obtained CFGG define the same language as the initial
CFGG.
Computation Theory p.6/2
-
7/23/2019 Cnf&Gnf Forms
7/27
Proof
LetG= (N,,R,S )be the original CFG.
Step 1: add a new start symbolS0toN, and the ruleS0StoR
Note: this change guarantees that the start symbol of
G does not occur on therhsof any rule.
Computation Theory p.7/2
-
7/23/2019 Cnf&Gnf Forms
8/27
Step 2: eliminate-rules
Repeat1. Eliminate the ruleA fromR whereAis not the start symbol;
2. For each occurrence ofAon therhs of a rule, add a new rule to R with
that occurrence ofA deleted;
Examples:(1) replaceB uAvbyB uAv|uv;
(2) replaceBuAvAwbyB uAvAw|uvAw|aAvw|uvw.
3. Replace the ruleB A, (if it is present) byBA|unless theruleB has been previously eliminated;
until allrules are eliminated.
Computation Theory p.8/2
-
7/23/2019 Cnf&Gnf Forms
9/27
Step 3: remove unit rules
Repeat:1. Remove a unit ruleA BR;
2. For each ruleB uR, add the ruleA uto R, unless
B uwas a unit rule previously removed;
until all unit rules are eliminated.
Note: uis a string of variables and terminals.
Computation Theory p.9/2
-
7/23/2019 Cnf&Gnf Forms
10/27
Convert all remaining rules
Repeat:1. Replace a ruleA u1u2 . . . uk,k 3, where eachui,1 ik, is
a variable or a terminal, by:
Au1A1,A1u2A2,. . .,Ak2uk1ukwhereA1,A2,. . .,Ak2 are new variables;
2. If k 2replace any terminaluiwith a new variableUiand add the rule
Uiui;
until no rules of the form Au1u2 . . . ukwith k3
remain.
Computation Theory p.10/2
-
7/23/2019 Cnf&Gnf Forms
11/27
Example CFG conversion
Consider the grammarG6 whose rules are:
S ASA|aB
A B|S
B b|
Notation:symbols removed aregreenand those added arered.
After first step of transformation we get:
S0 S
S ASA|aBA B|S
B b|
Computation Theory p.11/2
-
7/23/2019 Cnf&Gnf Forms
12/27
Removingrules
RemovingB:
S0 S
S ASA|aB|a
A B|S|
B b|
RemovingA :
S0 S
S ASA|aB|a|SA|AS|S
A B|S|
B b
Computation Theory p.12/2
-
7/23/2019 Cnf&Gnf Forms
13/27
Removing unit rule
RemovingSS:
S0 S
S ASA|aB|a|SA|AS|S
A B|S
B b
RemovingS0S:
S0 S|ASA|aB|a|SA|AS
S ASA|aB|a|SA|AS
A B|S
B b
Computation Theory p.13/2
-
7/23/2019 Cnf&Gnf Forms
14/27
More unit rulesRemovingAB:
S0 ASA|aB|a|SA|AS
S ASA|aB|a|SA|AS
A B|S|b
B b
RemovingA S:
S0 ASA|aB|a|SA|AS
S ASA|aB|a|SA|AS
A S|b|ASA|aB|a|SA|AS
B b
Computation Theory p.14/2
-
7/23/2019 Cnf&Gnf Forms
15/27
Converting remaining rules
S0 AA1|U B|a|SA|AS
S AA1|U B|a|SA|AS
A b|AA1|U B|a|SA|AS
A1 SA
U a
B b
Computation Theory p.15/2
-
7/23/2019 Cnf&Gnf Forms
16/27
Observation
The conversion procedure produces severalvariablesUialong with several rulesUia;
Since all these represent the same rule, we may
simplify the result using a single variableUand asingle ruleUa.
Computation Theory p.16/2
-
7/23/2019 Cnf&Gnf Forms
17/27
Greibach Normal Form
A context-free grammarG= (V,,R,S )is inGreibach normal form if each rulerRhas theproperty: lhs(r)V,rhs(r) =a,aand
V.
Note:Greibach normal form provides a justification of
operator prefix-notation usually employed in algebra.
Computation Theory p.17/2
-
7/23/2019 Cnf&Gnf Forms
18/27
-
7/23/2019 Cnf&Gnf Forms
19/27
Construction
1. Modify the rules inR so that ifAiAjRthenj > i2. Starting withA1 and proceeding toAm this is done as follows:
(a) Assume that productions have been modified so that for
1ik,AiAjRonly ifj > i;(b) IfAk Ajis a production withj < k, generate a new set of
productions substituting for theAj the rhs of eachAj production;
(c) Repeating (b) at mostk 1times we obtain rules of the form
Ak Ap,pk;
(d) Replace rulesAk Akby removing left-recursive rules.
Computation Theory p.19/2
-
7/23/2019 Cnf&Gnf Forms
20/27
Facts
Letmbe the highest order nonterminal generatedduring Greibach normal form construction algorithm.
Note: if no left-recursive rules (Ai Ai, for some1im) are in the
grammar then we have:
1. The leftmost symbol on the rhs of each rule forAm must be a terminal,
sinceAm is the highest order variable.
2. The leftmost symbol on the rhs of any rule forAm1 must be either
Am or a terminal symbol. If it is Amwe can generate new rules
replacingAm with the rhs of the Am rules.
3. Repeating (2) forAm2, . . . , A2, A1 we obtain only rules whose rhsstart with terminal symbols.
Computation Theory p.20/2
-
7/23/2019 Cnf&Gnf Forms
21/27
Left recursion
A CFG containing rules of the form
AA|
is called left-recursive inA.
The language generated by such rules is of the form
A
n
. If we replace the rulesAA|by
AB|,BB|
where Bis a new variable, then the language generated
by A is the same while no left-recursive A-rules are
used in the derivation.Computation Theory p.21/2
-
7/23/2019 Cnf&Gnf Forms
22/27
Removing left-recursion
Left-recursion can be eliminated by the followingscheme:
IfA A1|A2 . . . |Ar are allA left recursive rules, and
A1|2| . . . |s are all remainingA-rules then chose a newnonterminal, sayB ;
Add the newB-rulesB i|iB,1 ir;
Replace theA-rules byA i|iB,1is.
This construction preserve the languageL.
Computation Theory p.22/2
-
7/23/2019 Cnf&Gnf Forms
23/27
Conversion algorithm
for (k=1; k beta
add A_k ---> beta alpha
remove A_k ---> A_j alpha
}
for (each rule A_k ---> A_k alpha)
{add rules B_k ---> alpha | alpha B_k
remove A_k ---> A_k alpha
}
for (each rule A_k ---> beta, beta does not begin with A_k)
add rule A_k ---> beta B_k
}
}
Computation Theory p.23/2
-
7/23/2019 Cnf&Gnf Forms
24/27
More on Greibach NF
See Introduction to Automata Theory, Languages, andComputation, J.E, Hopcroft and J.D Ullman, Addison-
Wesley 1979, p. 9496.
Computation Theory p.24/2
-
7/23/2019 Cnf&Gnf Forms
25/27
Example
Convert the CFGG= ({A1, A2, A3}, {a, b}, R , A1)
where
R={A1 A2A3, A2A3A1|b, A3A1A2|a}
into Greibach normal form.
Computation Theory p.25/2
-
7/23/2019 Cnf&Gnf Forms
26/27
Solution
1. Step 1: ordering the rules: (OnlyA3 rules violate ordering conditions,hence onlyA3 rules need to be changed).
Following the procedure we replaceA3 rules by:
A3 A3A1A3A2|bA3A2|a
2. Eliminating left-recursion we get: A3 bA3A2B3|aB3|bA3A2|a,
B3A1A3A2|A1A3A2B3;
3. AllA3 rules start with a terminal. We use them to replaceA1 A2A3.
This introduces the rulesB3 A1A3A2|A1A3A2B3;
4. UseA1 production to make them start with a terminal.
Computation Theory p.26/2
-
7/23/2019 Cnf&Gnf Forms
27/27
The result
A3bA3A2B3|bA3A2|aB3|aA2bA3A2B3A1|bA3A2A1|aB3A1|aA1|b
A1bA3A2B3A1A3|bA3A2A1A3|aB3A1A3|aA1A3|bA3B3bA3A2B3A1A3A3A2B3|bA3A2B3A1A3A1A3A3A2
B3bA3A3A2B3|bA3A3A2|bA3A2A1A3A3A2B3|bA3A2A1A3A3A2
B3aB3A1A3A3A2B3|aB3A1A3A3A2
B3aA1A3A3A2B3|aA1A3A3A2
Disclaimer:I copied this result from Hopcroft and Ulman, page 98-99!
Hence, I cannot guarantee its correctness.
Computation Theory p.27/2