0% found this document useful (0 votes)
34 views10 pages

7-Parsing and Ambiguity-16-09-2024

gd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views10 pages

7-Parsing and Ambiguity-16-09-2024

gd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Parsing and Ambiguity

Membership problem. Given a string x in T* and X, where X can be a G = (V,


x L(X)
T, P, S), or a automata M, determine whether

Given a string x in T* and CFG G = (V, T, P, S),


determine whether x  L(G) and if x  L(G),
find a derivation S * x.

This problem is called Parsing.

To solve the parsing problem, we first study


the parse tree.
Parse Tree (Derivation tree)
A parse tree (or derivation tree) of a derivation is a tree in
which:

• S – the start symbol of G, is the root


• Each internal node is labeled with a variable (non-ter.)
• Each leaf is labeled with a terminal symbol or lambda
• If a rule A  A1A2…An occurs in the derivation then
A is a parent node whose child nodes are labeled
A1, A2, …, An in order.
• A parse tree is a top-down representation of a derivation
• Good way to visualize the derivation process
•Yield of the parse tree is concatenating all leaf
symbols from left to right in order.
Consider the rules P = {S → aSb | bSa | SS |  }.

(mixed order) One (LEFT) Anotherway (RIGHT) Anotherway


way to generate to generate abb is: to generate abb is:
abab is : S  SS  aSbS  S  SS  SaSb 
S  SS  aSbS abS  abaSb  Sab  aSbab 
 aSbaSb  abab abab
abaSb  abab S  aSb  abSab  abab
S
S

S S
a S b

a S b a S b
b S a

 Lesson learnt: Order is immaterial



for parse tree. For the same word, 
there may exist more than one
parse tree.
Derivation Trees
S A|AB
Other derivation
A |a|Ab|AA w = aabb
B b|bc|Bc|bB
trees for this string?

S
S S
A
A B A B
A A Many others
A A b B A A b possible.
A A A b
a a b a A b
a  A b
a
a
Ambiguous context-free grammars
• A context-free grammar is said to be AMBIGUOUS if there exists a
string w є L(G) that has at least two distinct derivation trees or at
least two rightmost/ leftmost derivations for the same string w.
• Else, the grammar is unambiguous.

Check the ambiguity of the following grammar.


G = ({S}, {a, b, c}, P, S) where P = {S → SbS | ScS | a}
Consider the string abaca. It has two parse trees.
S S
S b S S c S

a S c S S b S a

a a a a
Sample Parse Tree
• Sample parse tree for the palindrome CFG for
1110111:
P
P   | 0 | 1 | 0P0 | 1P1
1 P 1
This grammar is not ambiguous
1 P 1

HOMEWORK:
1 P 1
Check the ambiguity of the CFG
G = ({S, A}, {0,1}, P, S) where
P = {S → A00, A →  | AA | 0 | 1} 0
Show that the following grammars
are ambiguous

1.S -> SS | a | b
2.S -> ABA;
A -> aA | e; B -> bB | e
3. S -> aSb | aaSb | e
4. S -> aSb | abS | e
5. S → A00, A →  | AA | 0 | 1
Unambiguous context-free grammars
• A context-free grammar is said to be UNAMBIGUOUS if for
every string w є L(G), there exists a unique derivation tree
(upto isomorphism).

Unambiguous context-free language


• A context-free language L is said to be UNAMBIGUOUS if
there exists an unambiguous grammar G such that L= L(G).

Ambiguous context-free language


• A context-free language L is said to be INHERENTLY
AMBIGUOUS if every grammar that generates L is
ambiguous (you cannot find an unambiguous grammar for L).
BAD NEWS
• Consider the grammar G = ({S}, {a,b}, P, S)
where P = {S → SbS | a}.
• Is it ambiguous? Check for the string abababa
• Is the Language inherently ambiguous?
• NO!!!!! Why?????
• {S → abS | a} is an unambiguous grammar for
the language.
• The problem of deciding whether a given CFG G
is ambiguous or not, is undecidable (might be
covered in the last classes of my lecture).
Example of an Inherently Ambiguous CFL
• There exist CFLs that are inherently ambiguous, i.e.
every grammar for them is ambiguous
• L = {anbncm |n≥1,m≥1} U {anbmcm |n≥1, m≥1}

• G = ({S, S1, S2, A, C}, {a, b, c}, P, S) where P is

• S → S1 | S 2
• S1 → aS1b | C
• C → cC | c
• S2 → A | bS2c Ambiguous for the string abc

• A → aA | a

You might also like