theory of computation (fall 2014): stack machines: non-determinism, construction, formalization,...

Post on 15-Dec-2014

143 Views

Category:

Science

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Theory of Computation

Stack Machines

Vladimir Kulyukin

www.vkedco.blogspot.com

Outline

Stack Machines Stack Machine & Non-Determinism Stack Machine Construction Formalization of Stack Machines Stack Machines & Robot Control

Stack Machines

Stack Machine

FiniteControl

Input Tape

Stack

Stack Machine Move

read pop push

a C efC

If the current input symbol is a and the symbol on top of the stack is c, pop C off the stack, push efC onto the stack, and advance to the next symbol (move right). Reading a symbol means seeing it in the input and advancing to the next symbol.

Stack Machine Move: Example

read pop push

a c b

a cd

Input Tape Stacka

Stack Machine Move: Example

read pop push

a c b

a cd

Input Tape Stacka

Read a; Move right

Stack Machine Move: Example

read pop push

a c b

ad

Input Tape Stacka

Pop c off the Stack

Stack Machine Move: Example

read pop push

a c b

a bd

Input Tape Stacka

Push b on to the Stack

Stack Machines &

Non-Determinism

Stack Machine & Non-Determinism

A stack machine is like NFA, because it may have multiple sequences of moves on the same input

A stack machine accepts if and only if the input has been read and the stack is empty

Acceptance can also be defined in terms of final states

Non-Deterministic Stack Machine: Example

read pop push

ε S ab

a S ef

a S ε

Non-Deterministic Stack Machine: Example

Suppose the input is a The initial stack contains S There are three possible move sequences: Sequence 1: no input is read, pop S, push ab Sequence 2: read a, pop S, push ef Sequence 3: read a, pop S, push ε

Stack Machine Construction

Construct a stack machine for {anbn}

Sample Problem

Stack Machine Construction: Basic Insight

If a is read, pop off S, push 1, and push S In the middle of the string, pop off S If b is read, pop 1 off the stack If the stack is empty, that means we have read some number of

a’s (pushing) followed by the same number of b’s (popping)

Stack Machine Construction

read pop push

a S S1

ε S ε

b 1 ε

How Stack Machines Work: Example

Sa a b b b

Read a; Pop S; Push S1 (i.e., Push 1, then Push S); Move Right

a

Stack

a a a b b b S1

Stack

Before Move

After Move

How Stack Machines Work: Example

a a b b b

Read a; Pop S; Push S1 (i.e., Push 1, then Push S); Move Right

a

Stack

a a a b b b S11

S1

Stack

Before Move

After Move

How Stack Machines Work: Example

a a b b b

Read a; Pop S; Push S1 (i.e., Push 1, then Push S); Move Right

a

Stack

a a a b b bS111

S11

Stack

Before Move

After Move

How Stack Machines Work: Example

a a b b b

Read ε; Pop S; Push ε ; Stay in place (the machine has not read anything)

a

Stack

a a a b b b 111

S111

Stack

Before Move

After Move

How Stack Machines Work: Example

a a b b b

Read b ; Pop 1; Push ε ; Move right

a

Stack

a a a b b b

11

111

Stack

Before Move

After Move

How Stack Machines Work: Example

a a b b b

Read b ; Pop 1; Push ε ; Move right

a

Stack

a a a b b b

1

11

Stack

Before Move

After Move

How Stack Machines Work: Example

a a b b b

Read b ; Pop 1; Push ε ; Move right

a

Stack

a a a b b b

1

Stack

Before Move

After Move

How Stack Machines Work: Example

a a b b b

End of input is reached; Stack is empty; Accept

a

Stack

Formalization of Stack Machines

Formal Definition

tic.determinis-non is because strings,

ofset a isoutput thei.e., , ofset power the

is where,

symbol;alphabet initial theis

alphabet;input theis

alphabet;stack theis

where,S,,, tuple-4 a is machinestack A

*

**

M

Γ

ΓPΓPΓ

Γ S

Γ

ΓM

Example

read pop push

ε S ab

a S ef

a S ε

efSaefSa

abSabS

push or push , pop , read- ,,

push , pop , read- ,

Instantaneous Description of Stack Machine

stack theof on top is ofcharacter

leftmost thecontents,stack current theis and

input theofpart unread theis where,*

*

y

y

xyx

Stack Machine Derivation

Stack Machine Derivation

→* is a relation on instantaneous descriptions such thatif A & B are two instantaneous descriptions, then A →* B iff there is sequence of zero or more → transitions that starts at A and ends at B.

Acceptance by the Empty Stack

L(M) = {x is in Σ* | (x, S) →* (ε, ε)}

CFGs → Stack Machines:

Construction of Stack Machines from

Context-Free Grammars

CFG → Stack Machine

nothing.push , pop , read ;, , allfor .2

. of side hand-right the

push , pop , read ;|, , allfor 1.

:follows as is where,,,

:Sketch Proof

.such that machinestack

a is thereCFG, a is ,,, If :Theorem

aaaaa

xv

vPxvxvVv

S,VM

MLGLM

PSV G

CFG → Stack Machine

S → aSaS → bSbS → ε

read pop push

ε S aSa

ε S bSb

ε S ε

a a ε

b b ε

CFG Stack Machine

Stack Machines → CFGs:

Construction of Context-Free Grammars

fromStack Machines

Stack Machine CFG→

.in production a is |...|Then . where

,,...,,n transitioa has If :Insight Basic

.

such that ,,, CFG a is then there

machine,stack a is ,,, If .

that)generality of loss (w/o Assume :Theorem

1

1

PttA

ttAM

MLGL

PSΓG

SΓ M Γ

n

n

Example: Stack Machine CFG

S a0S

Stack MachineREAD POP PUSH

a S 0S

a 0 00

b 0 ɛ

b S 1S

b 1 11

a 1 ɛ

ɛ S ɛ

CFG Productions

0 a00

0 b

S b1S

1 b11

1 a

S ɛ

Stack Machines & Robot Control

Stack Machines for Robots Stack machines can be implemented in various robot control

mechanisms Behaviors can be described as context-free grammars The robot is constructed to parse the signals it receives from

the world CFGs are more powerful than FAs in that they allow the robot

to perceive signal dependencies but they do require memory (i.e., stack)

Robots & Stack Machines

Recall the line following problem for this robot. A control mechanism can be constructed so that the robot pushes and pulls its states onto and off the stack and executes specific behaviors in the following loop:

read (sense X); pop Y (Y is a memory state); execute Z

References & Reading Suggestions

Davis, Weyuker, Sigal. Computability, Complexity, and Languages, 2nd Edition, Academic Press

Brooks Webber. Formal Language: A Practical Introduction, Franklin, Beedle & Associates, Inc

Hopcroft & Ullman. Introduction to Automata Theory, Languages, and Computation, Narosa Publishing House

Moll, Arbib, & Kfoury. An Introduction to Formal Language Theory

top related