Вы находитесь на странице: 1из 96

Context Free Grammar

Ratan Kr. Basak


Ty p e 2 : C F G
CFG
CFG
CFG
CFG
CFG
Derivation and Parse Tree
Derivation:
The process of generating a language form the given production
of a grammar, the non-terminals are replaced by the
corresponding strings of the right hand side of the production.

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

Parsing a string is finding a derivation for that string

from a given grammar.

A parse tree is an ordered tree in which the LHS of a

production represents a parent node and the RHS

of a production represents children.


Parse Tree
Parse Tree

0 S

1 A
A
0
0 B

0 B
B

1 1
Parse Tree
Parse Tree
Ambiguity

A grammar or language is called ambiguous if any

of the cases for generating a particular string; more

than one left most derivation or more than one

right most derivation or more than one parse tree

can be generated.
Ambiguity
Ambiguity
Parse tree for derivation - 1 Parse tree for derivation - 2

Hence the Grammar is ambiguous


grammar.
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

1. Non-generating symbols are those symbols

which do not produce any terminal string.

2. Non-reachable symbols are those symbols which

cannot be reached at any time starting from the

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

grammar is converted in some specific form.

These helps to design some algorithm to answer certain question.

Ex. If a CFG is in CNF, one can easily answer whether the language

generated by the grammar is finite or not.

Parse tree generated from CNF is always a binary tree.


Chomsky Normal Form (CNF)
Process of converting a CFG into CNF
Process of converting a CFG into CNF
Process of converting a CFG into CNF
Convert the following grammar in CNF
Convert the following grammar in CNF
Greibach Normal Form
Greibach Normal Form (GNF)
Lemma
Lemma
Process for conversion of a CFG into GNF
Convert the following grammar into GNF
Step3 Cont.
Cont.
Cont.
Convert the following grammar into GNF
Cont.
Cont.
Cont.
Cont.
Cont.
Push Down Automata
Push Down Automata (PDA)
• CFG generates a context-free language.
• A PDA recognizes a context-free language.
• PDAs are like NFAs but have an extra component called
a stack
– The stack provides additional memory beyond the finite
amount available in the control.
– The stack allows PDA to recognize some non-regular
languages.
Push Down Automata (PDA)
NFA PDA

• 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.

• A stack can hold an unlimited amount of information.


Formal Definition of PDA
Instantaneous Description (ID)
Acceptance by PDA
Acceptance by PDA
Cont.
Cont.
Cont.
Cont.
Cont.

Accepted by the empty stack

Accepted by the final state


Accepted by the empty stack

Accepted by the final state


Accepted by the empty stack

Accepted by the final state


Accepted by the empty stack

Accepted by the final state


Accepted by the empty stack

Accepted by the final state


Accepted by the empty stack

Accepted by the final state


Accepted by the empty stack

Accepted by the final state


Ans:
Ans:
Ans:
Ans:
Ans:
Ans:
Ans:
Ans:
Ans:

Вам также может понравиться