Title: CFG to PDA Example
1CFG to PDA Example
In this presentation, we will convert a CFG G
into a PDA M such that L(G) L(M).
2Input 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
3Defining 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.
4Defining 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.
5Defining 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.
6Transitions 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
7Transitions 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
8Transitions 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
9Transitions 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
10Matching 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 /\
11Final 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.