Академический Документы
Профессиональный Документы
Культура Документы
If there are more than one non-terminals, then which one will be
replace must be determined.
Derivation
Derivation is divided into two parts:
1. Leftmost derivation : If we replace only the leftmost non-
terminals by some production rule at each step of the
generation process of the language from the grammar.
2. Rightmost derivation : If we replace only the rightmost
non-terminals by some production rule at each step of the
generation process of the language from the grammar.
Derivation
Derivation
Parse Tree
0 S
1 A
A
0
0 B
0 B
B
1 1
Parse Tree
Parse Tree
Ambiguity
can be generated.
Ambiguity
Ambiguity
Parse tree for derivation - 1 Parse tree for derivation - 2
As are getting two parse tree for generating a string from the
given grammar, hence the Grammar is ambiguous grammar.
Does ambiguous grammars create
problem?
Does ambiguous grammars create
problem?
In place of a put 2
Correct result is 2 + 2 * 2 = 2 + 4 = 6
How to remove Ambiguity from a
grammar?
There is no particular rule to remove ambiguity
from CFG.
Sometimes it can be removed just by hand, like
in the previous example we can remove by
setting priority to the operators.
Most bad news is some CFL have only
ambiguous grammar – this type of ambiguity is
called inherent ambiguity.
Simplification of CFG
Some type of symbols and productions increase the
number of steps in generating a language from a
CFG.
CFG can be simplified in the following three
processes:
1. Removal of useless symbols
a) Removal of non-generating symbols
b) Removal of non-reachable symbols
2. Removal of unit productions
3. Removal of null productions.
Removal of useless symbols
start symbols.
Removal of useless symbols
These two type of symbols can be removed
according to the following process:
1. Find non-generating symbols.
a) If it is start symbol leave that symbol as it is.
b) Otherwise remove those productions whose
right side and left side contain those symbols.
2. Find non-reachable symbols.
a) Remove those as per 1.b.
Remove useless symbols from the given CFG
Remove useless symbols from the given CFG
Remove useless symbols from the given CFG
Cont.
Remove useless symbols from the given CFG
Removal of Unit Productions
Procedure to remove Unit Production
Remove the Unit production from the given CFG
Unit Productions
Remove the Unit production from the given CFG
Unit Productions
Remove the Unit production from the given CFG
Remove the Unit production from the given CFG
Removal of Null Productions
Procedure to remove Null production
Procedure to remove Null production
Remove null production from the following
grammar
Remove null production from the following
grammar
Normal Form
When a grammar is made in normal form, every production of the
Ex. If a CFG is in CNF, one can easily answer whether the language
• A PDA can write symbols on the stack and read them back later.
• Writing a symbol “pushes down" all the other symbols on the stack.
• The symbol on the top of the stack can be read and removed
• When the top symbol is removed the remaining symbols move up.