CFG to PDA Example - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

CFG to PDA Example

Description:

q0 is always the initial state, and q2 is always the only. accepting state. Defining S, G, and Z ... 2) Now run the NFA and see how it can 'execute' this leftmost ... – PowerPoint PPT presentation

Number of Views:1629
Avg rating:3.0/5.0
Slides: 12
Provided by: Facul248
Category:
Tags: cfg | pda | example | run | up

less

Transcript and Presenter's Notes

Title: CFG to PDA Example


1
CFG to PDA Example
In this presentation, we will convert a CFG G
into a PDA M such that L(G) L(M).
2
Input CFG
This grammar generates a subset of all legal
arithmetic expressions.
G (V, S, S, P) V S,T,F S
a,,,(,) Start variable is S P S -gt
ST T T -gt TF F F -gt (S) a
3
Defining Q, q0, and A
M (Q, q0, A, S, G, Z, d) Q q0, q1, q2 The
initial state is q0. A q2.
G (V, S, S, P) V S,T,F S
a,,,(,) Start variable is S P S -gt
ST T T -gt TF F F -gt (S) a
The above definitions of Q, q0, and A will always
be the same no matter what the input CFG
is. That is, there will always be 3 states in
the PDA M, q0 is always the initial state, and q2
is always the only accepting state.
4
Defining S, G, and Z
M (Q, q0, A, S, G, Z, d) Q q0, q1, q2 The
initial state is q0. A q2. S S
a,,,(,) G V union S union Z S, T, F,
a, , , (, ), Z The initial stack character Z
is not in V or S.
G (V, S, S, P) V S,T,F S
a,,,(,) Start variable is S P S -gt
ST T T -gt TF F F -gt (S) a
S and G do depend on the input CFG, as does Z in
that if Z appears in S or G, then we must use
some other character in place of Z.
5
Defining d
M (Q, q0, A, S, G, Z, d) Q q0, q1, q2 The
initial state is q0. A q2. S S
a,,,(,) G V union S union Z S, T, F,
a, , , (, ), Z The initial stack character Z
is not in V or S.
G (V, S, S, P) V S,T,F S
a,,,(,) Start variable is S P S -gt
ST T T -gt TF F F -gt (S) a
We now begin a series of slides which create
the transition function d in phases.
6
Transitions involving q0 and q2
M (Q, q0, A, S, G, Z, d) Q q0, q1, q2 The
initial state is q0. A q2. S S
a,,,(,) G V union S union Z S, T, F,
a, , , (, ), Z The initial stack character Z
is not in V or S.
G (V, S, S, P) V S,T,F S
a,,,(,) Start variable is S P S -gt
ST T T -gt TF F F -gt (S) a
State Input Top Stack Next State Stack
Update q0 /\ Z q1 SZ q1 /\ Z q2 Z
7
Transitions involving S
M (Q, q0, A, S, G, Z, d) Q q0, q1, q2 The
initial state is q0. A q2. S S
a,,,(,) G V union S union Z S, T, F,
a, , , (, ), Z The initial stack character Z
is not in V or S.
G (V, S, S, P) V S,T,F S
a,,,(,) Start variable is S P S -gt
ST T T -gt TF F F -gt (S) a
State Input Top Stack Next State Stack
Update q0 /\ Z q1 SZ q1 /\ Z q2 Z q1 /\ S q1
ST q1 /\ S q1 T
8
Transitions involving T
M (Q, q0, A, S, G, Z, d) Q q0, q1, q2 The
initial state is q0. A q2. S S
a,,,(,) G V union S union Z S, T, F,
a, , , (, ), Z The initial stack character Z
is not in V or S.
G (V, S, S, P) V S,T,F S
a,,,(,) Start variable is S P S -gt
ST T T -gt TF F F -gt (S) a
State Input Top Stack Next State Stack
Update q0 /\ Z q1 SZ q1 /\ Z q2 Z q1 /\ S q1
ST q1 /\ S q1 T q1 /\ T q1 TF q1 /\ T q1
F
9
Transitions involving F
M (Q, q0, A, S, G, Z, d) Q q0, q1, q2 The
initial state is q0. A q2. S S
a,,,(,) G V union S union Z S, T, F,
a, , , (, ), Z The initial stack character Z
is not in V or S.
G (V, S, S, P) V S,T,F S
a,,,(,) Start variable is S P S -gt
ST T T -gt TF F F -gt (S) a
State Input Top Stack Next State Stack
Update q0 /\ Z q1 SZ q1 /\ Z q2 Z q1 /\ S q1
ST q1 /\ S q1 T q1 /\ T q1 TF q1 /\ T q1
F q1 /\ F q1 (S) q1 /\ F q1 a
10
Matching Transitions
M (Q, q0, A, S, G, Z, d) Q q0, q1, q2 The
initial state is q0. A q2. S S
a,,,(,) G V union S union Z S, T, F,
a, , , (, ), Z The initial stack character Z
is not in V or S.
G (V, S, S, P) V S,T,F S
a,,,(,) Start variable is S P S -gt
ST T T -gt TF F F -gt (S) a
State Input Top Stack Next State Stack
Update q0 /\ Z q1 SZ q1 /\ Z q2 Z q1 /\ S q1
ST q1 /\ S q1 T q1 /\ T q1 TF q1 /\ T q1
F q1 /\ F q1 (S) q1 /\ F q1 a q1 a a q1 /\ q
1 q1 /\ q1 q1 /\ q1 ( ( q1 /\ q1 ) )
q1 /\
11
Final Comments
Too get deeper insights about the algorithm, I
suggest you do the following 1) Draw a leftmost
derivation for the original grammar G for any
input string like aaaa 2) Now run the
NFA and see how it can execute this leftmost
derivation on its stack in a computation that
leads to acceptance.
Write a Comment
User Comments (0)
About PowerShow.com