0% found this document useful (0 votes)
62 views26 pages

Lecture-23 Context Free Grammar (CFG)

Automata

Uploaded by

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

Lecture-23 Context Free Grammar (CFG)

Automata

Uploaded by

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

Parsing by CFG

Parsing by CFG
• As in English language any sentence can be
expressed by parse tree, so any word generated
by the given CFG can also be expressed by the
parse tree, e.g. consider the following CFG
1. S → AA
2. A → AAA|bA|Ab|a

• Obviously, baab can be generated by the above


CFG.
Parsing by CFG
• To express the word baab as a parse tree, start
with S.
• Replace S by the string AA, of nonterminals,
• Drawing the downward lines from S to each
character of this string
Example 15
1. S → AA
2. A → AAA|bA|Ab|a
Syntax tree/Generation tree/Derivation tree

Thus the word ‘baab’ is generated.


Ambiguity in Context-Free Grammars

If a context free grammar G has more than one


derivation tree for some string w ∈L(G), it is
called an ambiguous grammar. There exist
multiple right-most or left-most derivations for
some string generated from that grammar.
Problem
Check whether the grammar G with production
rules −
X → X+X | X*X |X| a
is ambiguous or not?
Problem
• Let’s find out the derivation tree for the string
"a+a*a". It has two leftmost derivations.
Solution
Derivation 1 −
X → X+X → a +X → a+ X*X → a+a*X → a+a*a
Parse tree 1 −
Solution
Derivation 2 −
X → X*X → X+X*X → a+ X*X →a+a*X →
a+a*a
Parse tree 2 −
Solution
• As there are two parse trees for a single string
"a+a*a", the grammar G is ambiguous.
Ambiguities in Words Generation
Example 16
Consider the following CFG
S → S+S|S*S|number
number → 0|1|2|3|4|5|6|7|8|9
Where S and number are non-terminals and the
operators behave like terminals.
The above CFG creates ambiguity as the
expression 3+4*5 has two Derivations:
– (3+4)*5 = 35
– 3+(4*5) = 23
Ambiguities in Words Generation
Ambiguities in Words Generation
Ambiguous Grammar
Ambiguous CFG: A CFG is said to be ambiguous
if there exists at least one word of it’s language
that can be generated by different production
trees.
Example 21
For CFG: S → aS|Sa|a, word aaa is generated by
following three different trees. Thus CFG is ambiguous,
Un-ambiguous Grammar
Example 22
S → aS|a
is not ambiguous as neither the word aaa nor
any other word can be derived more than one
production trees.
• The derivation tree for aaa is as follows
Removing Ambiguity
Example 23
Consider the following CFG
S → aS | bS | aaS | Λ
• It can be observed that the word aaa can be
derived from more than one production trees.
• Thus, the above CFG is ambiguous.
• This ambiguity is removed by removing the
production:
S → aaS
Un-ambiguous Grammar
Consider the CFG of the language PALINDROME
S → aSa|bSb|a|b|Λ
• It may be noted that this CFG is un-ambiguous
as all the words of the language PALINDROME
can only be generated by a unique production
tree.
• It may be noted that if the production S →
aaSaa is added the CFG obtained will be again
un-ambiguous.
Polish Notation (o-o-o)
Polish Notation (o-o-o)
Polish Notation (o-o-o)
Polish Notation (o-o-o)
Conversion of Prefix into Infix Expression
Total Language Tree
• For a given CFG, a tree with the start symbol S
as its root and whose nodes are working
strings of terminals and non-terminals.
• The descendants of each node are all possible
results of applying every production to the
working string.
• This tree is called total language tree
Total Language Tree
Language Generated by Total Language
Tree
Total Language Tree

You might also like