S. J. P. N.
TRUST’S
HIRASUGAR INSTITUTE OF TECHNOLOGY, NIDASOSHI
Accredited at 'A+' Grade by NAAC
Programmes Accredited by NBA: CSE, ECE
Department of Computer Science & Engineering
Course: Theory of Computation(BCS503)
Module 3: Context Free Grammar &
Pushdown Automata
Prof. A. A. Daptardar
Asst. Prof. , Dept. of Computer Science &
Engg.,
1
Hirasugar Institute of Technology, Nidasoshi
Module-3
Content to be covered:
• Context-Free Grammars, Parse Trees,
Ambiguity in Grammars and Languages,
Ambiguity in Grammars and Languages,
Definition of the Pushdown Automaton,
The Languages of a PDA, Equivalence of
PDA's and CFG's, Deterministic
Pushdown Automata.
• TEXT BOOK: Sections 5.1, 5.2, 5.4,
6.1,6.2,6.3.1,6.4 2
CONTEXT FREE GRAMMARS
AND LANGUAGES
Context free grammars
Parse Trees
Ambiguity in Grammars and Languages
3
Grammar
4
What is Grammar?
5
Components of Grammar
6
Notations used while
constructing the Grammar
7
Chomsky Hierarchy of Languages
Languages from “simplest” to “complex”
Each is a subset of the ones below
• Regular
• Context Free
• Context Sensitive
• Recursively Enumerable
Can be defined by the type of
Machine that will recognize it. Noam Chomsky 8
Obtaining Grammar from FA
9
10
11
Another method
12
13
14
15
16
17
18
19
20
21
Alternate Method
22
OBTAINING GRAMMAR FROM
RE
23
24
25
26
LANGUAGE
What is the Language Generated by Grammar?
27
28
29
30
31
32
33
v
34
35
36
37
38
39
40
41
42
43
44
45
DERIVATION
46
47
48
LEFTMOST DERIVATION
49
Definition
• If a word w is generated by z CFG by a
certain derivation, and at each step in the
derivation a rule of production is applied to
the leftmost nonterminal in the working
string, then this derivation is called a
leftmost derivation.
50
51
52
RIGHTMOST DERIVATION
53
Definition
• If a word w is generated by z CFG by a
certain derivation, and at each step in the
derivation a rule of production is applied to
the rightmost nonterminal in the working
string, then this derivation is called a
leftmost derivation.
54
55
56
DERIVATION TREE /
PARSE TREE
57
v
58
AMBIGUOUS GRAMMAR
59
Definition
• A CFG is called ambiguous of for atleast
one word in the language that it generates
two or more possible derivations of the
word that corresponds to different
derivation trees.
• OR
• If a grammar G generates two different
derivation trees for the same string is
called ambiguous grammar.
60
v
61
62
63
64
65
66
67
68
69
PUSH DOWN AUTOMATA
PDA
70
Is there any need of Push Down Automata
when FINITE AUTOMATA is there?
NOTE
PDA definition:
Transition function:
INSTANTANEOUS
DESCRIPTION
80
Acceptance of a language by
PDA
• There are two cases wherein a string w is
accepted by a PDA.
– Get the final state from the start state.
– Get an empty stack from the start state
• In the first case, we say that the language
is accepted by a final state and in the
second case we say that the language is
accepted by an empty stack or null stack.
83
• The formal definitions to accept the string
by a final state and by an empty stack are
defined as follows.
84
• For w ϵ Ʃ*, q0,p ϵ Q . It means that when
the string w is accepted by an empty
stack, the final state is irrelevant, the input
should be completely read and the stack
should be empty. Here the state p is not
the final state, only thing is that the string
w should be completely read and stack
should be empty.
85
CONSTRUCTION OF PDA
86
• General Procedure: To check for the palindrome, let us push
all scanned symbols onto the stack till we encounter the letter C.
Once we pass the middle string, if the string is a palindrome, for
each scanned input symbol, there should be a corresponding
symbol (same as input symbol) on the stack. Finally, if there is no
input and stack is empty, we say that the given string is a
palindrome.
• Step 1: Input symbols can be a or b.
Let q0 be the initial state and Z0 be the initial symbol on
the stack. In state q0 and when top of the stack is Z0
whether the input symbol is a or b push it on to the stack
and remain in q0. The transitions defined for this can be
of the form
88
• Once the first scanned input symbol is pushed on the
stack, the stack may contain either a or b. Now, in state
q0, the input symbol can be either a or b. Note that
irrespective of what is the input or what is there on the
stack, we have to keep pushing all the symbols on to the
stack, till we encounter C. So, the transitions defined for
this can be of the form
• Step 2: Input symbol is C.
Now, if the next input symbol is C, the top of the stack
may be a or b. Another possibility is that, in state q0, the
first symbol itself can be C. In this case w is null string
and Z0 will be on the stack. In all these cases, the input
symbol is C i.e. the symbol which is present in the
middle of the string. So, change the state to q1 and don
not alter the contents of the stack, The transitions for this
can be of the form
Now we have passed the middle of the string
90
• Step 3: Input symbols can be either a or b.
To be a palindrome, for each input symbol there should
be a corresponding symbol(same as input symbol) on the
stack. So, whenever the input symbol is same as symbol
on the stack, remain in state q1 and delete that symbol
from the stack and repeat the process. The transitions
defined for this can be of the form
• Step 4 : Finally, in state q1, if the string is a palindrome,
there is no input symbol to be scanned and the stack
should be empty i.e. the stack should contain Z0. Now,
change the state to q2 and do not alter the contents of the
stack. The transition for this can be of the form
v
Deterministic PDA
107
Deterministic PDA
108
• Example: Is the PDA to accept the language
L(M) = {wCwR | w ϵ (a+b)* } is deterministic?
• Solution: The transitions defined for this machine
are obtained as follows:
109
110
111
112
113
114
CFG TO PDA
115
APPLICATIONS OF GNF
Obtain CFG,
Convert it into GNF,
Then Obtain the PDA 126
PDA TO CFG
132
Procedure to convert pda to
cfg: