Unit-02
Regular and Context Free
Grammar
1
CONSTRUCTION OF A REGULAR GRAMMAR
GENERATING T(M) FOR A GIVEN DFA M
2
3
4
5
BASIC DEFINITIONS AND EXAMPLE of GRAMMAR
6
7
8
Find the Derivation of below Grammar
9
10
11
12
13
14
15
16
17
AMBIGUITY IN CONTEXT-FREE GRAMMARS
18
19
20
Problem-02
21
22
23
24
Construction of Reduced Grammar
25
26
27
28
29
30
31
Example-02
32
33
34
ELIMINATION OF NULL PRODUCTIONS
35
ELIMINATION OF UNIT PRODUCTIONS
36
Chomsky's Normal Form (CNF)
G1 = {S → AB, S → c, A → a, B → b} is in CNF
G2 = {S → aA, A → a, B → c} is not in CNF
37
Convert the given CFG to CNF. Consider the given
grammar G1:
S → a | aA | B
A → aBB | ε
B → Aa | b
38
Solution
Step-01: As grammar G1 contains A → ε null production, its
removal from the grammar yields:
S → a | aA | B
A → aBB
B → Aa | b | a
39
Step-2: Now, as grammar G1 contains Unit production S → B,
its removal yield:
S → a | aA | Aa | b
A → aBB
B → Aa | b | a
40
Step-03: In the production rule S → aA | Aa, A → aBB and B →
Aa, terminal a exists on RHS with non-terminals. So we will
replace terminal a with X:
S → a | XA | AX | b
A → XBB
B → AX | b | a
X→a
41
Step-04: In the production rule A → XBB, RHS has more than
two symbols, removing it from grammar yield:
S → a | XA | AX | b
A → RB
B → AX | b | a
X→a
R → XB
42
Example-02
43
44
Push Down Automata
45
tape
tape head
stack head
finite
stack
control
a l p h a b e t
The tape is divided into finitely many cells.
Each cell contains a symbol in an alphabet
Σ.
The stack head always scans the top
symbol of the stack. It performs two
basic operations:
Push: add a new symbol at the top.
Pop: read and remove the top symbol.
Alphabet of stack symbols: Γ
a
• The head scans at a cell on the tape and
can read a symbol on the cell. In each
move, the head can move to the right cell.
PDA Formalism
A PDA is described by:
1. A finite set of states (Q, typically).
2. An input alphabet (Σ, typically).
3. A stack alphabet (Γ, typically).
4. A transition function (δ, typically).
5. A start state (q0, in Q, typically).
6. A start symbol (Z0, in Γ, typically).
7. A set of final states (F ⊆ Q, typically).
50
The Transition Function
Takes three arguments:
1. A state, in Q.
2. An input, which is either a symbol in Σ or
ε.
3. A stack symbol in Γ.
δ(q, a, Z) is a set of zero or more
actions of the form (p, ).
p is a state; is a string of stack symbols.
51
Actions of the PDA
If δ(q, a, Z) contains (p, ) among its
actions, then one thing the PDA can
do in state q, with a at the front of
the input, and Z on top of the stack is:
1. Change the state to p.
2. Remove a from the front of the input
(but a may be ε).
3. Replace Z on the top of the stack by .
52
Example: PDA
Design a PDA to accept {anbn | n > 1}.
The states:
q0 = start state. We are in state q if we
0
have seen only a’s so far.
q = we’ve seen at least one b and may
1
now proceed only if the inputs are b’s.
qf = final state; accept.
53
Example: PDA Cont…
The transitions:
δ(q0, a, Z0) = (q0, aZ0).
δ(q0, a, a) = (q0, aa).
δ(q , b, a) = (q , ε).
0 1
δ(q , b, a) = (q , ε).
1 1
δ(q , ε, Z0) = (qf, Z0). Accept at bottom.
1
54
Actions of the Example PDA
aaabbbε
q0
Z0
55
Actions of the Example PDA
aabbbε
q0
a
Z0
56
Actions of the Example PDA
abbbε
q0
a
a
Z0
57
Actions of the Example PDA
bbbε
q0
a
a
a
Z0
58
Actions of the Example PDA
bbε
q1
a
a
Z0
59
Actions of the Example PDA
bε
q1
a
Z0
60
Actions of the Example PDA
ε
q1
Z0
61
Actions of the Example PDA
qf
Z0
62
State Machine of PDA
63
PDA for Odd Palindrome
64
PDA for Even Palindrome
65
CFG to PDA (If not in GNF)
66
CFG to PDA (If in GNF)
67
68
69
70
71
72
73
Closure Properties of CFL
• Union
• Concatenation
• Kleene Star operation
74
Union
75
Concatenation
76
Kleene Star
77