Você está na página 1de 56

Foundations of Computer

Science (COSC-3302),
Lecture 5 (prepared after
Chapter 5 of Martins 2011
Stefan Andrei
textbook)

04/22/16

Lecture 5 COSC 3302

Course topics
1.1. Mathematical Tools and Techniques (Chapter 1)
1.2. Finite Automata and the Languages they Accept (Chapter 2)
1.3. Regular Expressions, Nondeterminism, and Kleenes Theorem (Chapter 3)
Exam 1
2.1. Context-Free Languages (Chapter 4)
2.2. Pushdown Automata (Chapter 5)
2.3. Context-Free and Non-Context-Free Languages (Chapter 6)
Exam 2
3.1. Turing Machines (Chapter 7)
3.2. Recursively Enumerable Languages (Chapter 8)
3.3. Undecidable Problems (Chapter 9)
3.4. Computable Functions (Chapter 10)
3.5. Introduction to Computational Complexity (Chapter 11)
Exam 3
04/22/16

Lecture 5 COSC 3302

Overview of Previous
Lecture
Context-Free Languages
1.
2.

3.
4.
5.

Using Grammar Rules to Define a Language


Context-Free Grammars: Definitions and More
Examples
Regular Languages and Regular Grammars
Derivation Trees and Ambiguity
Simplified Forms and Normal Forms

04/22/16

Lecture 5 COSC 3302

Pushdown Automata
1.
2.
3.
4.
5.

Definitions and Examples


Deterministic Pushdown Automata
A PDA from a Given CFG
A CFG from a Given PDA
Parsing

04/22/16

Lecture 5 COSC 3302

Definitions and Examples

Theorem. A language can be generated by


a CFG if and only if it can be accepted by a
pushdown automaton.
A pushdown automaton is similar to a FA
but has an auxiliary memory in the form of
a stack.
Pushdown automata are, by default,
nondeterministic.
Unlike FAs, the nondeterminism cannot
always be removed.

04/22/16

Lecture 5 COSC 3302

Definitions and Examples


(contd.)

Well start with a simple example, the


language AnBn = {anbn | n 0}.

This is not a regular language, but it is contextfree.

In processing the first part of an input


string that might be in AnBn, all we need
to remember is the number of as.

Saving the actual as is a simple way to do this.


So, the PDA will start by reading as and
pushing them onto the stack.

04/22/16

Lecture 5 COSC 3302

Definitions and Examples


(contd.)

As soon as the PDA reads a b, two things should


happen:
1. It enters a new state in which only bs are legal
inputs;
2. It pops one a off the stack to cancel this b.
In the new state, the correct move on the input
symbol b is to pop an a off the stack to cancel it.

Once enough bs have been read to cancel the as


on the stack, the string read so far is accepted.
The stack has no limit to its size, so the PDA can
handle anything in AnBn.

04/22/16

Lecture 5 COSC 3302

Definitions and Examples


A single move of a PDA will depend on the current
(contd.)

state, the next input, and the symbol currently on


top of the stack (the only one the PDA can see).
In the move, the PDA is allowed to change states
and to modify the top of the stack.
In many stack applications, the only legal stack moves
are to push a symbol on and to pop one off;
Here, to make things a little simpler, we will allow the
PDA to replace the top symbol X by a string of stack
symbols.
Two special cases are:
1. pushing a symbol Y (replacing X by YX) and
2. popping X (replacing X by ).

04/22/16

Lecture 5 COSC 3302

Definitions and Examples


(contd.)
Definition 5.1: A pushdown automaton is a 7-tuple

M = (Q, , , q0, Z0, A, ), where:

Q is a finite set of states;


The input and stack alphabets and are finite sets;
q0 Q is the initial state;
Z0 is the initial stack symbol;
A Q is the set of accepting states;
The transition function is
: Q ( {}) the set of finite subsets of Q *.

Because values of are sets, M may be


nondeterministic.
A move requires that there be at least one symbol on
the stack.

Z0 is the one on the stack initially.

04/22/16

Lecture 5 COSC 3302

Definitions and Examples


(contd.)
A configuration of a PDA is a triple (q, x, ), where:

q Q is the current state;


x * is the portion of the input string that has not
yet been read;
The contents of the stack is * (the convention
will be that the top corresponds to the leftmost
symbol).

We write (p, x, ) M (q, y, ) to mean that one of


the possible moves in the first configuration takes
M to the second.

Mn and M* refer to n moves and zero or more


moves.

04/22/16

Lecture 5 COSC 3302

10

Definitions and Examples


(contd.)

Definition 5.2: If M = (Q, , , q0, Z0, A, ) and x


*,
the string x is accepted by M if (q0, x, Z0) M*
(q, , ) for some * and some q A.

Note: A language L is said to be accepted by M if


L is precisely the set of strings accepted by M.
Sometimes a string accepted by M is said to be
accepted by final state, because acceptance does
not depend on the final stack contents at all.

04/22/16

Lecture 5 COSC 3302

11

Definitions and Examples


(contd.)
A PDA for AnBn is M = (Q, , , q , Z , A, )
0

where Q = {q0, q1, q2, q3}, A = {q0, q3},


and the transitions are shown in this table:
Move #

State

Input

Stack
top

Move(s)

q0

Z0

(q1, aZ0)

q1

(q1, aa)

q1

(q2, )

q2

(q2, )

q2

Z0

(q3, Z0)

all other combinations

04/22/16

Lecture 5 COSC 3302

none

12

Definitions and Examples


(contd.)

The moves that M makes as it processes the


string aabb are shown below:
(q0, aabb, Z0)
1
(q1, abb, aZ0)
2
(q1, bb, aaZ0)
3
(q2, b, aZ0)
4
(q2, , Z0)
5
(q3, , Z0).

M is deterministic, because it never has a choice


of moves.

Get a star: Prove that (q0, anbn, Z0) 2n+1 times (q3, ,
Z0).

04/22/16

Lecture 5 COSC 3302

13

Deterministic Pushdown
Automata
Definition 5.10: A pushdown automaton

M = (Q, , , q0, Z0, A, ) is deterministic


if it satisfies both of the following conditions:
1.

2.

For every q Q, every in {}, and every X


, the set (q, , X) has at most one element.
For every q Q, every , and every X ,
the two sets (q, , X) and (q, , X) cannot both
be nonempty.

Note: A language L is a deterministic contextfree language (DCFL) if there is a deterministic


PDA (DPDA) accepting L.

04/22/16

Lecture 5 COSC 3302

14

Deterministic Pushdown
Automata (contd.)

One example is the previous PDA accepting


AnBn.
Another example is the language of
balanced strings of brackets described by
the below three conditions:
1.

2.
3.

04/22/16

Two states q0 and q1, where q0 is the


accepting state
Input symbols are [ and ]
Stack symbols are the input symbols plus Z0
Lecture 5 COSC 3302

15

Deterministic Pushdown
Automata (contd.)

The transition table for a PDA accepting the


language of balanced strings of brackets is:
Move #

State

Input

Stack
top

Move

q0

Z0

(q1, [Z0)

q1

(q1, [[)

q1

(q1, )

q1

Z0

(q0, Z0)

(all other combinations)


04/22/16

Lecture 5 COSC 3302

none
16

Deterministic Pushdown
Automata (contd.)

The language Pal of palindromes over {a, b}


can be accepted by a PDA M that saves
symbols on the stack until it guesses that it
has reached the middle of the string, then
cancels stack symbols with input symbols.
Note: Guessing implies nondeterminism.

The initial state of M, in which it stays while it is


processing the first half of the string, is q0.

The state it enters when it is ready to begin the


second half is q1, and the accepting state is q2.

04/22/16

Lecture 5 COSC 3302

17

Deterministic Pushdown
Automata (contd.)
Two typical lines from the transition table are
1.
(q0, a, Z0) = {(q0, aZ0), (q1, Z0)}
2.

(q0, a, b) = {(q0, ab), (q1, b)}

In both lines, the first move is the right one if the input
symbol a is still in the first half of the string.
The second move is the right one if the a is the middle
symbol in an (odd-length) palindrome.
M decides which move to make by guessing.
The other guess it can make is that it has reached the
middle of an even-length palindrome.

In this case, M enters q1 by a -transition; for example,


(q0, , b) = {(q1, b)}.
04/22/16

Lecture 5 COSC 3302

18

The pushdown automaton


M
= ({q , q , q },Pal
{a, b}, {a, b, Z }, q , Z , {q }, ),
accepting
pal

where is:
1.

(q0, a, Z0) = {(q0, aZ0), (q1, Z0)}

2.

(q0, a, a) = {(q0, aa), (q1, a)}

3.

(q0, a, b) = {(q0, ab), (q1, b)}

4.

(q0, b, Z0) = {(q0, bZ0), (q1, Z0)}

5.

(q0, b, a) = {(q0, ba), (q1, a)}

6.

(q0, b, b) = {(q0, bb), (q1, b)}

7.

(q0, , Z0) = {(q1, Z0)}

8.
9.

(q0, , a) = {(q1, a)}


(q0, , b) = {(q1, b)}

04/22/16

10.

(q1, , Z0) = {(q2, Z0)}

11.

(q1, a, a) = {(q1, )}

12.

(q1, b, b) = {(q1, )}

Lecture 5 COSC 3302

19

Example of accepting abba

Let us see whether abba is accepted by M


(even length).
(q0, abba, Z0) 1a (q0, bba, aZ0)
5a(q0, ba, baZ
0) (q , ba, baZ )
9
1
0
12(q1, a, aZ0) (q , , Z )
11
1
0
10(q2, , Z0).

Since q2 is an accepting state, it follows


that the string abba is accepted.

04/22/16

Lecture 5 COSC 3302

20

Example of accepting ababa

Let us see now whether ababa is accepted


by M (odd length).
(q0, ababa, Z0) 1a (q0, baba, aZ0)
5a(q0, aba, baZ0)3(q1, ba, baZ0)
12(q1, a, aZ0) 11(q1, , Z0)
10(q2, , Z0).
Similarly, since q2 is an accepting state,
then the string ababa is accepted.

04/22/16

Lecture 5 COSC 3302

21

Other PDA accepting even


M
Pal = ({q , q , q , q }, {a, b}, {A, B, Z }, q , Z ,
even_pal

{q3}, ), where is:

1.

(q0, , Z0) = {(q1, Z0)}

2.

(q1, a, ) = {(q1, A)}

3.

(q1, b, ) = {(q1, B)}

4.

(q1, , ) = {(q2, )}

5.

(q2, a, A) = {(q2, )}

6.

(q2, b, B) = {(q2, )}

7.

(q2, , Z0) = {(q3, Z0)}

(get a star) Show the transitions of (q0, abba,


Z0)

04/22/16

Lecture 5 COSC 3302

22

Other PDA accepting odd Pal

Modd_pal = ({q0, q1, q2, q3}, {a, b}, {A, B, Z0}, q0, Z0,
{q3}, ), where is:
1.

(q0, , Z0) = {(q1, Z0)}

2.

(q1, a, ) = {(q1, A), (q2, )}

3.

(q1, b, ) = {(q1, B), (q2, )}

4.

(q2, a, A) = {(q2, )}

5.

(q2, b, B) = {(q2, )}

6.

(q2, , Z0) = {(q3, Z0)}

(get a star) Show the transitions of (q0,


ababa, Z0)

04/22/16

Lecture 5 COSC 3302

23

Other PDA accepting both even


and odd Pal

Meven_odd_pal = ({q0, q1, q2, q3}, {a, b}, {A, B, Z0},


q0, Z0, {q3}, ), where is:
1.

(q0, , Z0) = {(q1, Z0)}

2.

(q1, a, ) = {(q1, A), (q2, )}

3.

(q1, b, ) = {(q1, B), (q2, )}

4.

(q1, , ) = {(q2, )}

5.

(q2, a, A) = {(q2, )}

6.

(q2, b, B) = {(q2, )}

7.

(q2, , Z0) = {(q3, Z0)}

04/22/16

Lecture 5 COSC 3302

24

How about a CFG


Even Pal: S aSa | bSb |
generating
Pal?
Odd Pal: S aSa | bSb | a | b
(get a star)

Both even and odd Pal: S aSa | bSb | | a | b


The standard construction of an equivalent NPDA for Pal (both
even and odd) is:
M
pal = ({q0, q1, q2}, {a, b}, {a, b, S, Z0}, q0, Z0, {q2}, ), where
is:
(q , , Z ) = {(q , SZ )}
0
0
1
0

(q1, , S) = {(q1, aSa), (q1, bSb), (q1, a), (q1, b), (q1,
)}
(q1, a, a) = {(q1, )}

(q1, b, b) = {(q1, )}

(q1, , Z0) = {(q2, Z0)}

04/22/16

Lecture 5 COSC 3302

25

Deterministic Pushdown
Automata (contd.)

Here are the moves by which the palindromes aba and


aa are accepted:
(q0, aba, Z0) (q0, ba, aZ0) (q1, a, aZ0) (q1, , Z0)
(q2, , Z0)
(q0, aa, Z0) (q0, a, aZ0) (q1, a, aZ0) (q1, , Z0)
(q2, , Z0)
The only nondeterminism is in the transition from q0 to
q1 .
Once M makes the transition, only two strings can be
accepted:

the even-length string whose second half is the reverse of the


string read so far, and
the odd-length string whose second half is the reverse of the
string read before the previous symbol.

04/22/16

Lecture 5 COSC 3302

26

Deterministic Pushdown
Automata (contd.)

Theorem 5.16: The language Pal cannot be


accepted by a DPDA (i.e., cannot be accepted
without guessing).
Proof sketch: Suppose, for the sake of
contradiction, that M is a DPDA that accepts Pal.

We can modify M if necessary so that every move is


either of the form (p, , X) = {(q, )} or (p, , X) =
{(q, X)}.
Observe that M must read every string in its entirety,
because every string x is a prefix of the palindrome
xxr.

04/22/16

Lecture 5 COSC 3302

27

Deterministic Pushdown
Automata (contd.)

The idea of the proof is to find two different


strings r and s such that for every suffix z, M
treats both rz and sz the same way.

This will produce a contradiction, because for every r


and s with r s, r and s are distinguishable w.r.t. Pal.

Definition: For every string x, there is a string


yx such that of all strings xy, xyx is one resulting
in a configuration with minimum stack height.

Note: This means that if x represents the stack


contents when xyx has been processed, no symbols
of x are ever removed in further processing.

04/22/16

Lecture 5 COSC 3302

28

Deterministic Pushdown
Automata (contd.)

Now choose the strings r and s.


There are infinitely many different strings xyx.
Therefore, because there are only a finite number
of combinations of state and stack symbol, there
must be two different strings r = uyu and s = vyv
so that the configurations resulting from Ms
processing r and s have both the same state and
the same top stack symbol.
M wont be able to tell the difference between
these.
Therefore, for every z, the results of processing rz
and sz must be the same.

04/22/16

Lecture 5 COSC 3302

29

A PDA from a given CFG

Well examine two ways of constructing a PDA


from an arbitrary CFG.

In both cases the PDA will be nondeterministic.

In both approaches, the PDA attempts to


simulate a derivation in the grammar, using
the stack to hold portions of the current string.

The PDA terminates the computation if it finds


that the derivation-in-progress is not consistent
with the input string.
It attempts to construct a derivation tree from
the input string.

04/22/16

Lecture 5 COSC 3302

30

A PDA from a Given CFG


(contd.)
The two approaches are top-down and bottom-up,

which refer to the way the tree is constructed.

Well describe for each one the PDA obtained from


the grammar and indicate why it accepts the
language.

The top-down PDA:

Begins by placing the start symbol of the grammar


on the stack.
From this point, each step in the construction of the
derivation tree consists of replacing a variable A
that is currently on top of the stack by the right
side of a grammar production A .

04/22/16

Lecture 5 COSC 3302

31

A PDA from a Given CFG


(contd.)
The top-down PDA: (contd.)

This step corresponds to building the portion of the


tree containing that variable-node and its children.
The intermediate moves of the PDA are to remove
terminal symbols from the stack as they are
produced and match them with input symbols.
To the extent that they continue to match, the
derivation being simulated is consistent with the
input string.
Because the variable replaced in each step is the
leftmost one, we are simulating a leftmost
derivation.

04/22/16

Lecture 5 COSC 3302

32

A PDA from a Given CFG


(contd.)

Definition 5.17: Let G = (V, , S, P) be a CFG.

The nondeterministic top-down PDA


corresponding to G is NT(G)=(Q, , , q0, Z0, A,
), defined as follows:

Q = {q0, q1, q2}, A = {q2}, = V {Z0}, Z0 V.

The initial move is (q0, , Z0) = {(q1, SZ0)}.

The only move to the accepting state is (q1, , Z0) =


{(q2, Z0)}.

The moves from q1 are the following:

04/22/16

For every A V, (q1, , A) = {(q1, ) | A P}.

For every , (q1, , ) = {(q1, )}.

Lecture 5 COSC 3302

33

A PDA from a Given CFG


(contd.)
After the initial move, before the final
move, the PDA remains in state q1.

The only moves are to replace a variable


with the right side of a production and to
match a terminal symbol on the stack with
an input symbol and discard both.

Theorem 5.18: If G is a CFG, then the


nondeterministic top-down PDA NT(G)
accepts the language L(G).

Proof: see book.

04/22/16

Lecture 5 COSC 3302

34

A PDA from a Given CFG


(contd.)

The bottom-up PDA:

The tree is constructed from the bottom up.


The PDA simulates the reverse of a derivation
starting with the last move and ending with the
first.
Another thing thats reversed is the order in which
we read the symbols on the stack when doing a
reduction: the reduction corresponding to A
is performed when the string r appears on the
top of the stack; it is replaced by A.
The simulated derivation is a rightmost derivation.

04/22/16

Lecture 5 COSC 3302

35

A PDA from a Given CFG

(contd.)
Definition 5.22: Let G=(V, , S, P) be a CFG.

The nondeterministic bottom-up PDA


corresponding to G is NB(G)=(Q, , , q0, Z0, {q2},
), defined as follows:

04/22/16

For every and every X , (q0, , X) = {(q0, X)},


the shift moves.
For every production B , and every nonnull string
*, (q0, , r ) * (q0, B ), where r is the reverse
of , so this reduction is a sequence of one or more
moves in which, if there is more than one, the
intermediate configurations involve other states that are
specific to this sequence and appear in no other moves
of NB(G).
(q1, ) (q0, , S) and (q1, , Z0) = {(q2, Z0)}.
Lecture 5 COSC 3302

36

A PDA from a Given CFG


(contd.)

Theorem 5.23: If G is a CFG, then the nondeterministic bottom-up PDA NB(G) accepts
L(G).

Proof: see book.

04/22/16

Lecture 5 COSC 3302

37

A CFG from a Given PDA

Definition 5.27: If M is a PDA with input


alphabet , initial state q1, and initial stack
symbol Z1, then M accepts a language L by
empty stack if L = Le(M),
where Le(M) = {x * | (q1, x, Z1) * (q, ,
)}.
Theorem 5.28: If M = (Q, , , q0, Z0, A, )
is a PDA, then there is another PDA M1 such
that Le(M1) = L(M).

Proof: see book.

04/22/16

Lecture 5 COSC 3302

38

A CFG from a Given PDA


(contd.)

Theorem 5.29: If M=(Q, , , q0, Z0, , )


is a PDA accepting L by empty stack, then
there is a CFG G such that L = L(G).

04/22/16

Lecture 5 COSC 3302

39

A CFG from a Given PDA


(contd.)
Proof: define G=(V, , S, P) as follows:

V contains S and all possible variables of the


form
[p, A, q], where A and p, q Q. P
contains:

For every q Q, the production S [q0, Z0, q]

For every q, q1 Q, every {}, and every A ,


if (q, , A) contains (q1, ) then [q, A, q1] is in P

For every q, q1 Q, every {}, every A ,


and every m 1, if (q, , A) contains (q1, B1B2Bm) for
some B1, B2, , Bm in , then for every choice of q2,
q3, .. , qm+1 Q, the production [q, A, qm+1] [q1, B1, q2]
[q2, B2, q3][qm, Bm, qm+1] is in P.

See book for details.

04/22/16

Lecture 5 COSC 3302

40

Example of getting a CFG


The first transition corresponds
from
M
= ({qa, qPDA
, q }, {a,

anbn

1.

2.

3.

4.

5.

b}, {a, Z0}, q0, Z0, ,


), where is:
(q0, a, Z0) = {(q1,
aZ0)}
(q1,
aa)}
(q1,
)}
(q2,
)}
(q2,
)}

04/22/16

a, a) = {(q1,
b, a) = {(q2,
b, a) = {(q2,
, Z0) = {(q0,

to nine productions:
1.

[q0, Z0, q0] a [q1, a, q0] [q0, Z0, q0]

2.

[q0, Z0, q0] a [q1, a, q1] [q1, Z0, q0]

3.

[q0, Z0, q0] a [q1, a, q2] [q2, Z0, q0]

4.

[q0, Z0, q1] a [q1, a, q0] [q0, Z0, q1]

5.

[q0, Z0, q1] a [q1, a, q1] [q1, Z0, q1]

6.

[q0, Z0, q1] a [q1, a, q2] [q2, Z0, q1]

7.

[q0, Z0, q2] a [q1, a, q0] [q0, Z0, q2]

8.

[q0, Z0, q2] a [q1, a, q1] [q1, Z0, q2]

9.

[q0, Z0, q2] a [q1, a, q2] [q2, Z0, q2]

Lecture 5 COSC 3302

41

Example of getting a CFG from a PDA


(contd)

M
= ({q , q , q }, {a,

anbn

1.

2.

3.

4.

5.

b}, {a, Z0}, q0, Z0, ,


), where is:
(q0, a, Z0) = {(q1,
aZ0)}
(q1,
aa)}
(q1,
)}
(q2,
)}
(q2,
)}

04/22/16

The second transition


corresponds to nine
productions:

a, a) = {(q1,
b, a) = {(q2,
b, a) = {(q2,
, Z0) = {(q0,

1.

[q1, a, q0] a [q1, a, q0] [q0, a, q0]

2.

[q1, a, q0] a [q1, a, q1] [q1, a, q0]

3.

[q1, a, q0] a [q1, a, q2] [q2, a, q0]

4.

[q1, a, q1] a [q1, a, q0] [q0, a, q1]

5.

[q1, a, q1] a [q1, a, q1] [q1, a, q1]

6.

[q1, a, q1] a [q1, a, q2] [q2, a, q1]

7.

[q1, a, q2] a [q1, a, q0] [q0, a, q2]

8.

[q1, a, q2] a [q1, a, q1] [q1, a, q2]

9.

[q1, a, q2] a [q1, a, q2] [q2, a, q2]

Lecture 5 COSC 3302

42

Example of getting a CFG from a PDA


(contd)
The third transition corresponds to
M
= ({q , q , q }, {a,

anbn

1.

2.

3.

4.

5.

b}, {a, Z0}, q0, Z0, ,


), where is:
(q0, a, Z0) = {(q1,
aZ0)}
(q1,
aa)}
(q1,
)}
(q2,
)}
(q2,
)}

04/22/16

a, a) = {(q1,

one production:

1.

1.

b, a) = {(q2,
1.

b, a) = {(q2,
, Z0) = {(q0,

1.

[q1, a, q2] b

The fourth transition corresponds


to one production:
[q2, a, q2] b

The fifth transition corresponds to


one production:
[q2, a, q0]

Here are the starting productions,


not associated to any transition:
S [q0, Z0, q0] | [q0, Z0, q1] | [q0, Z0, q2]

Lecture 5 COSC 3302

43

Parsing

To parse a string relative to a CFG G means to


determine an appropriate derivation for the
string, or to determine that there is none.
Parsing a sentence is necessary to understand
it.
We can sometimes eliminate the
nondeterminism from our top-down or bottomup PDAs, yielding a rudimentary parser.
This is not always possible, and even if it is,
the process is not always easy.

04/22/16

Lecture 5 COSC 3302

44

Parsing (contd.)

Consider the CFG with rules S [S] | SS |

The way we have formulated the top-down PDA


involves inherent nondeterminism.
Each time a variable appears on top of the stack,
the machine replaces it by the right side of a
production.
These moves depend only on the variable; they are
-transitions and are chosen nondeterministically.
The natural approach to eliminating the
nondeterminism is to consider whether consulting
the next input symbol would allow the PDA to
choose correctly.

04/22/16

Lecture 5 COSC 3302

45

Parsing (contd.) S [S] | SS


| The
first few moves made for the input string [[]

[]] are enough to show that this is not enough:


1.

(q0, [[][]], Z0)

3.

(q1, [[][]], SZ0)


(q1, [[][]], [S]Z0)

4.

(q1, [][]], S]Z0)

5.

(q1, [][]], SS]Z0)

2.

1.

S
[S]
[SS]

In lines 2 and 4, S is on top of the stack and the


next symbol is a left parenthesis, but S is
replaced by different strings in these two places.

04/22/16

Lecture 5 COSC 3302

46

Parsing (contd.)

We might have predicted this; not only is


the grammar ambiguous, but there are
more productions than there are terminal
symbols for the right sides to start with.
Lets try a different grammar for the same
language:
S [S ]S |

This is unambiguous and generates the same


language.
In the non-deterministic top-down PDA
NT(G), with the input string [], there is still a
problem, but it is not as serious.

04/22/16

Lecture 5 COSC 3302

47

Parsing (contd.) S [S]S |

The derivation goes as follows:


1.
2.
3.
4.
5.
6.
7.

(q0, [], Z0)


(q1, [], SZ0)
S
(q1, [], [S]SZ0)
[S ]S
(q1, ], S]SZ0)
(q1, ], ]SZ0)
[]S
(q1, , SZ0)
(q1, , Z0)
[]

1.During these moves, there are three

times when S is the top stack symbol.

04/22/16

Lecture 5 COSC 3302

48

Parsing (contd.)

The first two are fine; if the input symbol is [,


then replace S by [S]S, and if it is ], then replace
S by .
Unfortunately, S is also replaced by at the end of
the computation.

The PDA must make a -transition in this case, and


if it can choose to do this, then its not
deterministic.

A similar problem arises for many CFGs and there


is a common solution.

We introduce an end-marker $ into the alphabet,


use it only to mark the end of the input, and change
the language (and the CFG) slightly.

04/22/16

Lecture 5 COSC 3302

49

Parsing (contd.)

Our new CFG G has the productions S S1$


and
S1 [S1]S1 |
The corresponding transition table for NT(G) is:
Move #

State

Input

Stack top

Move(s)

q0

Z0

(q1, SZ0)

q1

(q1, S1$)

q1

S1

2 choices

q1

(q1, )

q1

(q1, )

q1

(q1, )

q1

Z0

(q2, )

04/22/16

Lecture 5 COSC 3302

50

Parsing (contd.)

The only non-determinism is in line 3, which


allows both the moves (q1, [S1]S1) and (q1, ).

We can fix this by replacing line 3 with these


6 lines:
State

Input

Stack top

Move

q1

S1

(q1,[, [S1]S1)

q1,[

(q1, )

q1

S1

(q1,], )

q1,]

(q1, )

q1

S1

(q1,$, )

q1,$

(q1, )

04/22/16

Lecture 5 COSC 3302

51

Parsing (contd.)

The resulting PDA is clearly deterministic.


To check that it is equivalent to the original it
suffices to verify that our assumptions about
the correct -transition made by NT(G1) in each
case where S1 is on the stack are correct.
See book for details.
In some simple grammars, we can do
something similar to eliminate nondeterminism
from the bottom-up PDA NB(G).

04/22/16

Lecture 5 COSC 3302

52

Summary

Pushdown Automata

1.

Definitions and Examples


Deterministic Pushdown Automata
A PDA from a Given CFG
A CFG from a Given PDA
Parsing

2.
3.
4.
5.

04/22/16

Lecture 5 COSC 3302

53

Reading suggestions

From [Martin; 2011]


Chapter 5 (Pushdown Automata)

04/22/16

Lecture 5 COSC 3302

54

Coming up next

From [Martin; 2011]:


Chapter 6 (Context-Free and Non-Context-Free
Languages)
Exam 2 (Topics: Lectures 4, 5, and 6)

04/22/16

Lecture 5 COSC 3302

55

Thank you for your


attention!
Questions?

04/22/16

Lecture 5 COSC 3302

56

Você também pode gostar