0% found this document useful (0 votes)
31 views20 pages

Automata

The document discusses the relationship between Context-Free Grammars (CFG) and Pushdown Automata (PDA), stating that for any CFG, there exists a corresponding PDA that accepts the same language. It provides examples of CFGs in Chomsky Normal Form (CNF) and illustrates how to construct PDAs from these grammars, including detailed steps for processing specific strings. Theorems are presented to affirm that the languages accepted by PDAs are equivalent to those generated by CFGs.

Uploaded by

Asma Ayub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views20 pages

Automata

The document discusses the relationship between Context-Free Grammars (CFG) and Pushdown Automata (PDA), stating that for any CFG, there exists a corresponding PDA that accepts the same language. It provides examples of CFGs in Chomsky Normal Form (CNF) and illustrates how to construct PDAs from these grammars, including detailed steps for processing specific strings. Theorems are presented to affirm that the languages accepted by PDAs are equivalent to those generated by CFGs.

Uploaded by

Asma Ayub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Lecture No 16

PDA corresponding to CFG

Dr. Nazir Ahmed Zafar


PDA corresponding to CFG
Theorem
• Corresponding to any CFG there exists a PDA
accepting the language generated by the CFG.
• Since an algorithm has already been discussed to
convert the CFG in CNF, so the PDA can be
constructed corresponding to the CFG. As the CFG
in CNF generates all the nonnull words of the
corresponding CFL, so accepting the null string (if
it is contained in the CFL), can be managed
Dr. separately.
Nazir Ahmad Zafar
PDA corresponding to CFG
Example 1:
Consider the following CFG which is in CNF and
does not generate the null string
S  SB|AB
A  CC
Bb
Ca
The corresponding PDA will be
Dr. Nazir Ahmad Zafar
PDA corresponding to CFG
a b
READ1
READ2 ACCEPT
START
C B ∆
∆ READ3
PUSH S POP
S A
S

PUSH B PUSH B PUSH C

PUSH S PUSH A PUSH C

Dr. Nazir Ahmad Zafar


PDA corresponding to CFG
• Here the STACK alphabet Γ = {S, A, B, C}, where the
TAPE alphabet Σ = {a, b}.
Note: It may be noted that when the POP state is entered
either a nonterminal is replaced by two non-terminals at
the top of the STACK accommodating a production, or a
non-terminal is popped out from the top of the stack and a
READ state is entered to read a specified letter from the
TAPE or else the machine crashes.
• The choice of path taken at POP state to accommodate the
word belonging to the CFL can be determined by the left
most derivation of the word

Dr. Nazir Ahmad Zafar


PDA corresponding to CFG
Consider the word aab with its left most derivation, as follows:
Working-String Generation Production Used
S => AB S  AB step 1
=> CCB A  CC step 2
=> aCB Ca step 3
=> aaB Ca step 4
=> aab Bb step 5

Dr. Nazir Ahmad Zafar


PDA corresponding to CFG
• First of all the START state is entered
STACK TAPE
∆… aab∆…

• The PUSH S state is entered STACK TAPE


S aab∆…

• POP state is entered, to accommodate the production S


 AB, PUSH B and PUSH A states are entered.
STACK TAPE
AB aab∆…

Dr. Nazir Ahmad Zafar


PDA corresponding to CFG
• Then the POP state is entered and to accommodate
the production A  CC, PUSH C, PUSH C states
are entered
STACK TAPE
CCB aab

• The POP state is entered and to accommodate the


production C  a, READ1 is entered and the letter a is
read from the TAPE
STACK TAPE
CB aab

Dr. Nazir Ahmad Zafar


PDA corresponding to CFG
Table shows observations discussed above, for word aab
Left most derivation STATE STACK TAPE

START ∆ aab
S PUSH S S aab
POP ∆ aab
PUSH B B aab
AB PUSH A AB aab
POP B aab
PUSH C CB aab
CCB PUSH C CCB aab
POP CB aab
aCB READ1 CB aab
POP B aab
aaB READ1 B aab
POP ∆ aab
aab READ2 ∆ aab
POP ∆ aab
READ3 ∆ aab
ACCEPT ∆ aab

Dr. Nazir Ahmad Zafar


PDA corresponding to CFG
• The POP state is entered and to accommodate the production C  a,
READ1 state is entered and the letter a is read from the TAPE
STACK TAPE
B aab

• The POP state is entered to accommodate the production B  b,


READ2 is entered and letter b is read from the TAPE

STACK TAPE
∆ aab

• The ∆ shown in the STACK indicates that there are no non-terminals


in the working string and ∆ is read from the STACK which leads to
READ3 state where the ∆ is read from the TAPE and the ACCEPT
state is entered, shows that the word aab is accepted by the PDA.

Dr. Nazir Ahmad Zafar


PDA corresponding to CFG
Building the PDA corresponding to the given CFG:
Example 2:
Consider the following CFG
S  XY
X  aX | bX |a
Y  Ya | Yb | a
Converting CFG to CNF, introduce the non-terminals A and B
Aa
Bb

Dr. Nazir Ahmad Zafar


PDA corresponding to CFG

The following CFG is in CNF


S  XY
X  AX | BX |a
Y  YA | YB | a
Aa
Bb
The PDA corresponding to the above CFG will be

Dr. Nazir Ahmad Zafar


PDA corresponding to CFG
a a a b
READ3 READ4
READ1
READ2
B
START A ACCEPT
X Y

PUSH S POP ∆ READ5

S
X Y
X
Y
PUSH Y PUSH X PUSH X PUSH A PUSH B

PUSH X PUSH A PUSH B PUSH Y PUSH Y

Dr. Nazir Ahmad Zafar


PDA corresponding to CFG
The word aaab can be generated as
String Generation Production used
S => XY S  XY step 1
=> AXY X  AX step 2
=> aXY Aa step 3
=> aaY Xa step 4
=> aaYB Y  YB step 5
=> aaaB Ya step 6
=> aaab Bb step 7
Dr. Nazir Ahmad Zafar
Observations discussed above for word aab
STACK TAPE STACK TAPE
(ST) ∆ aaab (PP) Y aaab
(PH S) S aaab (RD1) Y aaab
(PP) ∆ aaab (PP) ∆ aaab
(PH Y) Y aaab (PH B) B aaab
(PH X) XY aaab (PH Y)YB aaab
(PP) Y aaab (PP) B aaab
(PH X)XY aaab (RD2) B aaab
(PHA)AXY aaab (PP) ∆ aaab
(PP) XY aaab (RD4) ∆ aaab
(RD3)XY aaab (PP) ∆ ∆
(RD5) ∆ ∆

Dr. Nazir Ahmad Zafar


Building a PDA for every CFG
 The set of all languages accepted by PDAs is the same
as the set of all languages generated by CFGs.

Theorem 30
 Given a CFG that generates the language L, there is a PDA that
accepts exactly L.

Theorem 31
 Given a PDA that accepts the language L, there exists a CFG that
generates exactly L.

Dr. Nazir Ahmad Zafar


PROOF OF THEOREM 30
 Given a CFG that generates L, there is a PDA that
accepts exactly L.
 Convert CFG to CNF
 Then use the approach given next to construct PDA

Dr. Nazir Ahmad Zafar


EXAMPLE 1
START

S SB b
S AB a
READ1 READ2
A CC
B b ACCEPT
C a B
C

PUSH S ∆ READ3
POP
S A
S

PUSH B PUSH B PUSH C

PUSH S PUSH A PUSH C

Dr. Nazir Ahmad Zafar


EXAMPLE 2

S SB a a b
B AB READ3
READ1 READ2
B a START
A BB ACCEPT
A a B
A B ∆
B b
PUSH S ∆
POP READ4
S B
A

PUSH B PUSH B PUSH B

PUSH S PUSH B PUSH A

Dr. Nazir Ahmad Zafar


EXAMPLE 3

S AR1
R1 SA
a, b b a
S BR2
R2 SB READ1 READ2 READ3
S AA START
ACCEPT
S BB S A
B
S a ∆
S
S b ∆
PUSH S POP READ4
A a S S
B b S R2 S
R1
S ∆ PUSH R1 PUSH A PUSH R2 PUSH B PUSH A PUSH B

PUSH A PUSH S PUSH B PUSH S PUSH A PUSH B

Dr. Nazir Ahmad Zafar

You might also like