0% found this document useful (0 votes)
21 views

ATCD Mod 2

The document discusses regular and context-free grammars. It covers topics such as constructing a regular grammar for a given DFA, basic definitions of grammar, ambiguity in context-free grammars, elimination of null and unit productions, and Chomsky normal form. It also discusses pushdown automata including their formal definition, transition functions, examples of PDAs for languages such as {anbn} and palindromes, and how to convert a CFG to a PDA. Finally, it briefly covers the closure properties of context-free languages under operations such as union, concatenation, and Kleene star.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

ATCD Mod 2

The document discusses regular and context-free grammars. It covers topics such as constructing a regular grammar for a given DFA, basic definitions of grammar, ambiguity in context-free grammars, elimination of null and unit productions, and Chomsky normal form. It also discusses pushdown automata including their formal definition, transition functions, examples of PDAs for languages such as {anbn} and palindromes, and how to convert a CFG to a PDA. Finally, it briefly covers the closure properties of context-free languages under operations such as union, concatenation, and Kleene star.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 76

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
21
22
Construction of Reduced Grammar

23
24
25
26
27
28
29
Example-02

30
31
32
ELIMINATION OF NULL PRODUCTIONS

33
ELIMINATION OF UNIT PRODUCTIONS

34
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

35
Convert the given CFG to CNF. Consider the given
grammar G1:

S → a | aA | B
A → aBB | ε
B → Aa | b

36
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

37
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

38
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

39
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

40
Example-02

41
42
Push Down Automata

43
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).
48
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.
49
Acceptability of PDA

50
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 .
51
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.

52
Actions of the Example PDA
aaabbbε

q0

Z0

53
Actions of the Example PDA
aabbbε

q0

a
Z0

54
Actions of the Example PDA
abbbε

q0

a
a
Z0

55
Actions of the Example PDA
bbbε

q0

a
a
a
Z0
56
Actions of the Example PDA
bbε

q1

a
a
Z0

57
Actions of the Example PDA

q1

a
Z0

58
Actions of the Example PDA
ε

q1

Z0

59
Actions of the Example PDA

qf

Z0

60
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

61
State Machine of PDA

62
PDA for Odd Palindrome

63
PDA for Even Palindrome

64
CFG to PDA (If not in GNF)

65
CFG to PDA (If in GNF)

66
67
68
69
70
71
72
Closure Properties of CFL

• Union
• Concatenation
• Kleene Star operation

73
Union

74
Concatenation

75
Kleene Star

76

You might also like