Diagrams • PDAs for balance • Chart of progress • Formal definitions

PDAs
• Diagrams
• PDAs for balance
• Chart of progress
• Formal definitions
• Recognition Again
Diagrams
• States and transitions (again)
• Label includes an input symbol, a
• Also, a stack symbol, g
•
… and a stack action
a, g / <action>
STD for Balance
(see Fig 13.2)
• Strategy
ÿ stack symbol I counts n( - n)
e.g., I I means n( - n) = 2
ÿ n( - n) can’t go negative.
• if stack empty,
optionally go to accepting state
else, stay (at start).
• If input = (, push I.
• If input = ),
if possible to pop, do so; else fail
Example of Processing
with the PDA for Balance
State Input
Stack
Stack Action
q0
(
( ) ( ) )
<|
Push I
q0
(
) ( ) )
I <|
Push I
q0
)
( ) )
II <|
Pop
q0
(
) )
I <|
Push I
q0
)
)
II <|
Pop
q0
)
I <|
Pop
q0
L
<|
—
q1
(Accept)
STD for anbn
(see Fig 13.1)
• stack symbol I counts na - nb
… by pushes and pops
• 2 states: a’s at q0 and then b’s at q1
ÿ start at q0
ÿ if input = a, stay at q0
ÿ if input = b, go to q1
ÿ no going back
ÿ if stack empty, na - nb = 0
so go to accepting state
PDA Processing Example #2
Strings in anbn
State
Input
Stack
Stack Action
q0
aaabbb
<|
Push I
q0
aabbb
I <|
Push I
q0
abbb
II <|
Push I
q0
bbb
III <| Pop
q1
bb
II <|
Pop
q1
b
I <|
Pop
q1
L
<|
—
q2
(Accept)
Stack Actions as Pushes
•
L: Push nothing to get a pop
d(q, a, g) = (r, L)
•
g : Push back the popped symbol
for no change:
d(q, a, g) = (r, g)
•
gx : Push more to get a net push:
d(q, a, g) = (r, gx)
PDAs, the Definition:
a septuple (7-tuple)
M = (Q, S, G, q0, <|, A, d )
Old:
Q, S, q0 , A .. as in FAs
New:
G, <|
Different:
.. for the stack
d
Definition Details
G
finite set of stack symbols
<| Œ G
start symbol for the stack
d : Q ¥ ( S » {L} ) ¥ G Æ 2
Q ¥ G*
Example
( {q0, q1, q2}, {a, b}, {I, <|}, q0, <|, {q2}, d), where:
d ( q0, a, I ) = {(q0, II)}
d ( q0, b, I ) = {(q1, L)}
d (q1, L , <| ) = {(q2, <|)}
NPDAs for CFG Parsing:
3 Design Issues
• where to start building the tree
ÿ top-down
ÿ bottom-up
• handling nondeterminacy
ÿ NPDA: search
ÿ DPDA: lookahead
ÿ fix the grammar
• search-tree strategy
ÿ breadth-first
ÿ depth-first
NPDA from CFG
for Top-Down Parsing
CFG, G:
({S}, {a, b}, S, {S Æ a S b, S Æ L} )
L(G):
{anbn}
NPDA, M:
({q0, q1, q2}, {a, b}, {a, b, S, <|}, q0, <|, {q2}, d)
Rules (d):
#
Type
from input stack
changes
1
Start
q0
L
<|
{(q1, S <|)}
2
Expand q1
L
S
3
4
Match
Match
q1
q1
5
Stop
q1
a
b
L
a
b
<|
{(q1, a S b), (q1, L)}
{(q1, L)}
{(q1, L)}
{(q2, <|)}
Parsing Example
with Lucky choice of aSb vs L
State
Input
Stack
Rule #
q0
q1
aabb
aabb
<|
S <|
1
2 (with aSb)
q1
aabb
a S b <|
3
q1
abb
S b <|
2 (with aSb)
q1
abb
q1
bb
a S b b <| 3
2 (with L)
S b b <|
q1
bb
b b <|
4
q1
b <|
q1
b
d
4
5
q2
L
<|
(Accept)
DPDA Parsing:
Top-Down + Lookahead
Type
from
input
stack
changes
Start
q0
L
<|
(q1, S <|)
Expand
q1
a
S
( qa , a S b)
Expand
q1
b
S
( qb , L)
Tidy-up
qa
L
a
Tidy-up
qb
L
b
(q1, L)
(q1, L)
Match
q1
a
a
Match
q1
b
b
(q1, L)
(q1, L)
Stop
q1
L
<|
(q2, <|)
Deterministic
(Lookahead)
Parsing Example
State
Input
Stack
q0
aabb
<|
q1
aabb
S <|
qa
abb
aSb <|
q1
abb
Sb <|
qa
bb
aSbb <|
q1
bb
Sbb <|
qb
b
bb <|
q1
b
b <|
q1
L
<|
q2
Left-Recursion
Left-Recursion frustrates lookahead
S Æ Sa ˜ b
Since every string begins with b ,
looking (ahead) at the first symbol
can never help decide.
Eliminating Left-Recursion, #1
SÆbT
TÆaT ˜ L
Same language but T Æ a T is not
left-recursive.
Eliminating Left-Recursion, #2
SÆSS | [ S ]
SÆT
˜ L
TÆ [ T ] T
˜ L