0% found this document useful (0 votes)
98 views

Ambiguity in Context Free Languages

The document discusses ambiguity in context-free grammars. It begins by defining what makes a CFG ambiguous and provides examples using the grammar E -> E+E | E*E | (E) | a. It then discusses why ambiguity is undesirable for programming languages and introduces Chomsky normal form as a way to simplify grammars and potentially resolve ambiguity. Chomsky normal form restricts rules to be either variables on the left-hand side producing a single terminal on the right or two variables. The document also covers converting arbitrary CFGs to Chomsky normal form through a series of steps.

Uploaded by

Adil Ahmad
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Ambiguity in Context Free Languages

The document discusses ambiguity in context-free grammars. It begins by defining what makes a CFG ambiguous and provides examples using the grammar E -> E+E | E*E | (E) | a. It then discusses why ambiguity is undesirable for programming languages and introduces Chomsky normal form as a way to simplify grammars and potentially resolve ambiguity. Chomsky normal form restricts rules to be either variables on the left-hand side producing a single terminal on the right or two variables. The document also covers converting arbitrary CFGs to Chomsky normal form through a series of steps.

Uploaded by

Adil Ahmad
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Ambiguity in Context Free

Languages
Ambiguity

CFG ambiguous  any of following equivalent statements:

  string w with multiple derivation trees.


  string w with multiple leftmost derivations.
  string w with multiple rightmost derivations.

Defining ambiguity of grammar, not language


E  E  E | E  E | (E) | a
a  aa

E E  E  E  a E  a EE
 a  a E  a  a*a
E  E
Rightmost derivation

a E  E

a a
E  E  E | E  E | (E) | a
a  aa

E  EE  E  EE  a EE E


 a  aE  a  aa
E  E
rightmost derivation

E  E a

a a
E  E  E | E  E | (E) | a
a  aa

Two derivation trees


E E

E  E E  E

a E  E E  E a

a a a a
The grammar E  E  E | E  E | ( E ) | a

is ambiguous:

string a  a  a has two derivation trees

E E

E  E E  E

a E  E E  E a

a a a a
The grammar E  E  E | E  E | ( E ) | a

is ambiguous:

string a  aa has two leftmost derivations

E  E  E  a E  a EE
 a  a E  a  a*a

E  EE  E  EE  a EE


 a  aE  a  aa
Definition:

A context-free grammar G is ambiguous if some string w L(G )

has two or more derivation.


Why do we care about ambiguity?

a  aa
take a  2

E E

E  E E  E

a E  E E  E a

a a a a
2  22

E E

E  E E  E

2 E  E E  E 2

2 2 2 2
2  22  6 2  22  8
6 8
E E
2 4 4 2
E  E E  E
2 2 2 2
2 E  E E  E 2

2 2 2 2
Correct result: 2  22  6

6
E
2 4
E  E
2 2
2 E  E

2 2
• Ambiguity is bad for programming languages
• We want to remove ambiguity
Chomsky Normal Form

Transformations are developed to convert an arbitrary CFG into an


equivalent grammar that satisfies the conditions of the normal
form.
Even though we can’t get every grammar into unambiguous form,
there is an especially simple form that general CFG’s can be
converted into.
• ** The derivation tree for derivations in CNF are binary trees.
• Normal form imposes restrictions on the form of the rules to
facilitate the analysis of C.F.Gs and languages. Two properties
characterize a normal form:
– The grammars that satisfy the normal form requirement
should generate the entire set of context-free languages.
– There should be an algorithmic transformation of an
arbitrary context-free grammar into an equivalent grammar
in the normal form.
Noam Chomsky came up with an especially simple type of
context free grammars which is able to capture all context
free languages.
Chomsky's grammatical form is particularly useful when one
wants to prove certain facts about context free languages.
This is because assuming a much more restrictive kind of
grammar can often make it easier to prove that the
generated language has whatever property you are
interested in.
• The transformation of a grammar into a normal form consists
of a sequence of the original grammars, deletion, or
modifications, each of which preserves the language of the
original grammar.
Chomsky Normal Form: DEFINITION
A CFG is said to be in Chomsky Normal Form if every rule
in the grammar has one of the following forms:

– Sλ (λ for epsilon’s sake only)


– A  BC (dyadic variable productions)
– Aa (unit terminal productions)

where S is the start variable, A,B,C are variables and a is a


terminal. Thus epsilons may only appear on the right hand
side of the start symbol and other RHS are either 2 variables
or a single terminal.
Write from Sudkamp PP 122

• Since the maximum number of symbols on the right-hand side of


a rule is two, the derivation tree associated with a derivation in a
CNF is a binary tree. For a CNF the grammar should have
– No recursive starting symbol.
– No null-rules other than S → λ.
– No chain rules.
– No useless symbols.

• S → aABC | a; A → aA | a; B → bcB | bc; C → cC | c.


CFG  CNF
Converting a general grammar into Chomsky Normal Form works
in four steps:
1. Ensure that the start variable doesn't appear on the right hand
side of any rule.
2. Remove all epsilon productions, except from start variable.
3. Remove unit variable productions of the form A  B where
A and B are variables.
4. Add variables and dyadic variable rules to replace any longer
non-dyadic or non-variable productions.

For any grammar G, we build an equivalent grammar G’ in which


the start symbol is non-recursive.
CFG  CNF
Example

Let’s see how this works on the following example grammar


for pal:
CFG  CNF
1. Start Variable

Ensure that start variable doesn't appear on the right hand side of
any rule.
CFG  CNF
2. Remove Epsilons

Remove all epsilon productions, except from start variable.


CFG  CNF
3. Remove Variable Units

Remove unit variable productions of the form A  B.


CFG  CNF
4. Longer Productions
Add variables and dyadic variable rules to replace any longer
productions.
CFG  CNF
Result
Inherent Ambiguity

• Some context free languages have only


ambiguous grammars
n n m n m m
Example: L  {a b c }  {a b c }

S  S1 | S 2 S1  S1c | A S2  aS2 | B
A  aAb |  B  bBc | 
n n n
The string a b c
has two derivation trees

S S

S1 S2

S1 c a S2
Convert the CFG to CNF
• Let G be the grammar with productions
S  AACD
A  aAb | 
C aC | a
D  aDa | bDb | 
• Let us go through each step of the conversion to CNF.
• Eliminate nullable -productions: The nullable variables are A and
D.
S  AACD | ACD | AAC | CD | AC | C
A  aAb | ab
C aC | a
D  aDa | bDb | aa | bb
• Eliminate Unit Productions
• Here we may simply add the productions S → aC | a. and delete S → C.

• Restricting the right hand sides of productions to single terminals or strings


of two or more variables. This step yields the productions

S  AACD | ACD | AAC | CD | AC | X a C | a


A  X a AX b | X a X b
C X a C | a
D  X a DX a | X b DX b | X a X a | X b X b
Xa a
Xb b
Exercise

• Convert the following grammar into normal form.


S  aS | AB | AC
A  aA | 
B  bB | bS
C  cC | 
Answer
S' S
S  aS | AB | AC
A  aA | 
B  bB | bS
C  cC | 
Greibach Normal Form (SUDKAMP-pp131)

• A context-free grammar G is in Greibach normal form if each rule


has one of the following forms:
– A → aA1A2…An ,
– A → A, or
– S → λ,

You might also like