Docsity

Chomsky Normal Form: Transforming Context-Free Grammars, Slides of Ethics

Toggle sidebar
bg1
CSE 322 - Introduction to Formal Methods in Computer Science
Chomsky Normal Form
Dave Bacon
Department of Computer Science & Engineering, University of Washington
A useful form for dealing with context free grammars is the Chomksy normal form. This is a particular form of
writing a CFG which is useful for understanding CFGs and for proving things about them. It also makes the parse
tree for derivations using this form of the CFG a binary tree. And as a CS major, I know you really love binary trees!
So what is Chomsky normal form? A CFG is in Chomsky normal form when every rule is of the form A→BC
and A→a, where ais a terminal, and A,B, and Care variables. Further Band Care not the start variable.
Additionally we permit the rule S→εwhere Sis the start variable, for technical reasons. Note that this means that
we allow S→εas one of many possible rules.
Okay, so if this is the Chomsky normal form what is it good for? Well as a first fact, note that parse trees for a
derivation using such a grammar will be a binary tree. Thats nice. It will help us down the road. Okay, so if it might
be good for something, we can ask the natural question: is it possible to convert an arbitrary CFG into an equivalent
grammar which is of the Chomsky normal form? The answer, it turns out, is yes. Lets see how such a conversion
would proceed.
A. A new start variable
The first step is simple! We just add a new start variable S0and the rule S0→Swhere Sis the original start
variable. By doing this we guarantee that the start variable doesn’t occur on the right hand side of a rule.
B. Eliminate the εrules
Next we remove the εrule. We do this as follows. Suppose we are removing the εrule A→ε. We remove this rule.
But now we have to “fix” the rules which have an Aon their right-hand side. We do this by, for each occurrence of A
on the right hand side, adding a rule (from the same starting variable) which has the Aremoved. Further if Ais the
only thing occurring on the right hand side, we replace this Awith ε. Of course this latter fact will have created a
new εrule. So we do this unless we have previously removed A→ε. But onward we press: simply repeat the above
process over and over again until all εrules have been removed.
For example, suppose our rules contain the rule A→εand the rule B→uAv where uand vare not both the
empty string. First we remove A→ε. Then we add to this rule the rule B→uv . (Make sure that you don’t delete
the original rule B→uAv. If, on the other hand we had the rule A→εand B→A, then we would remove the
A→εand replace the rule B→Awith the rule B→ε. Of course we now have to eliminate this rule via the same
procedure.
C. Remove the unit rules
Next we need to remove the unit rules. If we have the rule A→B, then whenever the rule B→uappears, we will
add the rule A→u(unless this rule was already replaced.) Again we do this repeatedly until we eliminate all unit
rules.
D. Take care of rules with more than two terminals or variables
At this point we have converted our CFG to one which has no εtransitions, and where all rules are either of the
form variables goes to terminal, or of the form variable goes to string of variables and terminals with two or more
symbols. These later rules are of the appropriate Chomsky normal form. To convert the remaining rules to proper
form, we introduce extra variables. In particular suppose A→u1u2. . . unwhere n > 2. Then we convert this to a
set of rules, A→u1A1,A1→u2A2,. . .,Ak−2→uk−1uk. Now we need to take care of the rules with two elements
on the right hand side. If both of the elements are variables, then we are fine. But if any of them are terminals, we
pf3
pf4

Partial preview of the text

Download Chomsky Normal Form: Transforming Context-Free Grammars and more Slides Ethics in PDF only on Docsity!

CSE 322 - Introduction to Formal Methods in Computer Science

Chomsky Normal Form

Dave Bacon Department of Computer Science & Engineering, University of Washington

A useful form for dealing with context free grammars is the Chomksy normal form. This is a particular form of writing a CFG which is useful for understanding CFGs and for proving things about them. It also makes the parse tree for derivations using this form of the CFG a binary tree. And as a CS major, I know you really love binary trees! So what is Chomsky normal form? A CFG is in Chomsky normal form when every rule is of the form A → BC and A → a, where a is a terminal, and A, B, and C are variables. Further B and C are not the start variable. Additionally we permit the rule S → ε where S is the start variable, for technical reasons. Note that this means that we allow S → ε as one of many possible rules. Okay, so if this is the Chomsky normal form what is it good for? Well as a first fact, note that parse trees for a derivation using such a grammar will be a binary tree. Thats nice. It will help us down the road. Okay, so if it might be good for something, we can ask the natural question: is it possible to convert an arbitrary CFG into an equivalent grammar which is of the Chomsky normal form? The answer, it turns out, is yes. Lets see how such a conversion would proceed.

A. A new start variable

The first step is simple! We just add a new start variable S 0 and the rule S 0 → S where S is the original start variable. By doing this we guarantee that the start variable doesn’t occur on the right hand side of a rule.

B. Eliminate the ε rules

Next we remove the ε rule. We do this as follows. Suppose we are removing the ε rule A → ε. We remove this rule. But now we have to “fix” the rules which have an A on their right-hand side. We do this by, for each occurrence of A on the right hand side, adding a rule (from the same starting variable) which has the A removed. Further if A is the only thing occurring on the right hand side, we replace this A with ε. Of course this latter fact will have created a new ε rule. So we do this unless we have previously removed A → ε. But onward we press: simply repeat the above process over and over again until all ε rules have been removed. For example, suppose our rules contain the rule A → ε and the rule B → uAv where u and v are not both the empty string. First we remove A → ε. Then we add to this rule the rule B → uv. (Make sure that you don’t delete the original rule B → uAv. If, on the other hand we had the rule A → ε and B → A, then we would remove the A → ε and replace the rule B → A with the rule B → ε. Of course we now have to eliminate this rule via the same procedure.

C. Remove the unit rules

Next we need to remove the unit rules. If we have the rule A → B, then whenever the rule B → u appears, we will add the rule A → u (unless this rule was already replaced.) Again we do this repeatedly until we eliminate all unit rules.

D. Take care of rules with more than two terminals or variables

At this point we have converted our CFG to one which has no ε transitions, and where all rules are either of the form variables goes to terminal, or of the form variable goes to string of variables and terminals with two or more symbols. These later rules are of the appropriate Chomsky normal form. To convert the remaining rules to proper form, we introduce extra variables. In particular suppose A → u 1 u 2... un where n > 2. Then we convert this to a set of rules, A → u 1 A 1 , A 1 → u 2 A 2 ,.. ., Ak− 2 → uk− 1 uk. Now we need to take care of the rules with two elements on the right hand side. If both of the elements are variables, then we are fine. But if any of them are terminals, we

add a new variable and a new rule to take care of these. For example, if we have A → u 1 B where u 1 is a terminal, then we replace this by A → U 1 B and U → u 1.

I. EXAMPLE CONVERSION TO CHOMSKY NORMAL FORM

Lets work out an example. Consider the grammar

S → ASB A → aAS|a|ε B → SbS|A|bb

First we add a new start state:

S 0 → S S → ASB A → aAS|a|ε B → SbS|A|bb

Next we need to eliminate the ε rules. Eliminating A → ε yields

S 0 → S S → ASB|SB A → aAS|a|aS B → SbS|A|bb|ε

Now we have a new ε rule., B → ε. Lets remove it

S 0 → S S → ASB|SB|S|AS A → aAS|a|aS B → SbS|A|bb

Next we need to remove all unit rules. Lets begin by removing B → A:

S 0 → S S → ASB|SB|S|AS A → aAS|a|aS B → SbS|bb|aAS|a|aS

Next lets remove S → S:

S 0 → S S → ASB|SB|AS A → aAS|a|aS B → SbS|bb|aAs|a|aS

Further we can eliminate S 0 → S:

S 0 → ASB|SB|AS S → ASB|SB|AS A → aAS|a|aS B → SbS|bb|aAs|a|aS

A quick examination shows us that we have ended up with a grammar in Chomsky normal form. (This can, of course, be simplified.)