42
Theory of Computation Stack Machines Vladimir Kulyukin www.vkedco.blogspot.com

Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Theory of Computation

Stack Machines

Vladimir Kulyukin

www.vkedco.blogspot.com

Page 2: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Outline

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

Page 3: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Stack Machines

Page 4: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Stack Machine

FiniteControl

Input Tape

Stack

Page 5: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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.

Page 6: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Stack Machine Move: Example

read pop push

a c b

a cd

Input Tape Stacka

Page 7: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Stack Machine Move: Example

read pop push

a c b

a cd

Input Tape Stacka

Read a; Move right

Page 8: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Stack Machine Move: Example

read pop push

a c b

ad

Input Tape Stacka

Pop c off the Stack

Page 9: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Stack Machine Move: Example

read pop push

a c b

a bd

Input Tape Stacka

Push b on to the Stack

Page 10: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Stack Machines &

Non-Determinism

Page 11: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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

Page 12: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Non-Deterministic Stack Machine: Example

read pop push

ε S ab

a S ef

a S ε

Page 13: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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 ε

Page 14: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Stack Machine Construction

Page 15: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Construct a stack machine for {anbn}

Sample Problem

Page 16: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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)

Page 17: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Stack Machine Construction

read pop push

a S S1

ε S ε

b 1 ε

Page 18: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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

Page 19: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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

Page 20: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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

Page 21: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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

Page 22: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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

Page 23: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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

Page 24: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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

Page 25: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

How Stack Machines Work: Example

a a b b b

End of input is reached; Stack is empty; Accept

a

Stack

Page 26: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Formalization of Stack Machines

Page 27: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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

Page 28: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Example

read pop push

ε S ab

a S ef

a S ε

efSaefSa

abSabS

push or push , pop , read- ,,

push , pop , read- ,

Page 29: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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

Page 30: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Stack Machine Derivation

Page 31: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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.

Page 32: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Acceptance by the Empty Stack

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

Page 33: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

CFGs → Stack Machines:

Construction of Stack Machines from

Context-Free Grammars

Page 34: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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

Page 35: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

CFG → Stack Machine

S → aSaS → bSbS → ε

read pop push

ε S aSa

ε S bSb

ε S ε

a a ε

b b ε

CFG Stack Machine

Page 36: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Stack Machines → CFGs:

Construction of Context-Free Grammars

fromStack Machines

Page 37: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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

Page 38: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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 ɛ

Page 39: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

Stack Machines & Robot Control

Page 40: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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)

Page 41: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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

Page 42: Theory of Computation (Fall 2014): Stack Machines: Non-Determinism, Construction, Formalization, Robot Control Use

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