Formal methods 3 - languages and machines

21
Formal Methods in Software Lecture 3. Languages and Machines-2 Vlad Patryshev SCU 2014

description

My course of Formal Methods at Santa Clara University, Winter 2014. automata, Turing machine, languages hierarchy (part 2)

Transcript of Formal methods 3 - languages and machines

Page 1: Formal methods   3 - languages and machines

Formal Methods in Software

Lecture 3. Languages and Machines-2

Vlad PatryshevSCU2014

Page 2: Formal methods   3 - languages and machines

Machine with Stack

Page 3: Formal methods   3 - languages and machines

Stacktrait Stack[T] {

def push(T);

def pop:T;

def isEmpty;

}

Page 4: Formal methods   3 - languages and machines

Can Calculate Expressions val s = new Stack[String]

def doOp(y: Double, op: String, x: Double): String = {

"" + (op match {

case "+" => x + y

case "-" => x - y

case "*" => x * y

case "/" => x / y

})

}

def eval(expr:String): String = {

for (x <- expr.split(" ")) {

if (x == ")") s.push(doOp(s.pop.toDouble, s.pop, s.pop.toDouble))

else s.push(x)

}

s.pop

}

Page 5: Formal methods   3 - languages and machines

Context-Free Language

Given an alphabet A, a context-free grammar of a language L A*⊂ has the following form:

● A → w

where A is a nonterminal symbols, and w is a word formed by terminal and nonterminal symbols.

A context-free language is the one produced by a context-free grammar.

Regular languages are context-free.

Page 6: Formal methods   3 - languages and machines

CFL - samples

• Canonical example: anbn

o S → εo S → aSb

• Parentheses: ((()())())o S → εo S → SSo S → (S)

• Arithmetic expressions

Page 7: Formal methods   3 - languages and machines

Stack Machine aka PDA (push-down automaton)

FSM + Stack

E.g.Forth, JVM, Haskell...

Page 8: Formal methods   3 - languages and machines

(Deterministic) PDA, formally

• Alphabets:o In - input alphabet;o S - machine states;o Z - stack alphabet

• Transition function: (In ∪ {ε})×S×Z → S×(Z∪{ε})

meaning, it depends on top of stack, and the result may be pushed

More formally, states are S×Z*; transition is (In∪{ε})×S×Z* → S×Z*

Page 9: Formal methods   3 - languages and machines

(Nondeterministic) PDA, formally

• Alphabets:o In - input alphabet;o S - machine states (Sa - acceptable states);o Z - stack alphabet

• Transition function: (In∪{ε})×S×Z → P(S×(Z∪{ε}))

meaning, it depends on top of stack, and the result may be pushed

More formally, states are S×Z*; transition is (In∪{ε})×S×Z* → P(S×Z*)

Acceptable states are Sa×{λ}

Page 10: Formal methods   3 - languages and machines

DPDA vs NPDA

S → aSa

S → bSb

S → ε

Need a NPDA for this language.

Which means, DPDA and NPDA are not equivalent, unlike DFS and NFS.

Page 11: Formal methods   3 - languages and machines

PDA Example

Parse anbnε

Use deterministic

In={a,b,ε}

S={q0,good,bad}

Z={a}

transition t: t(q0,a,Z) → (q0,aZ)

t(q0,b,aZ) → (q0,Z)

t(q0,ε,<empty>) → (good,<empty>)

(all other transition go to bad)

Page 12: Formal methods   3 - languages and machines

Non-context-free languages

Example: anbncndn

In a context-sensitive grammar rules can look like

aXb → awb

In an arbitrary grammar there are no rules for rules

w1 → w2

Chomsky’s Language Hierarchy

Page 13: Formal methods   3 - languages and machines

The most General Tool

Either Lambda-calculus, or Turing machinethey are equivalent

and define recursive functions

Page 14: Formal methods   3 - languages and machines

Turing Machine

DFA + unlimited tape with symbols + commands:

• read symbol from tape, affecting its state

• write symbol to tape

• move tape left

• move tape right

• stop

Page 15: Formal methods   3 - languages and machines

Turing Machine - notation

In this example:

{A,B,C,H} - states

H - final state

0/P,L means: on ‘0’ Print ‘1’ and move tape Left

Page 16: Formal methods   3 - languages and machines

Example (Busy Beaver)

Page 17: Formal methods   3 - languages and machines

Busy Beaver, more

Busy Beaver machine is one that takes most steps, for a given number of states, using just 0 and 1.

We had a 4-state Busy BeaverS(n) grows faster than any recursive function

n (number of states) S(n) (number of steps)

1 1

2 6

3 21

4 107

5 >47176870

Page 18: Formal methods   3 - languages and machines

Halting Problem

Given a Turing machine, can we decide if it ever stops?

http://ro-che.info/ccc/03

Page 19: Formal methods   3 - languages and machines

Universal Turing Machine

• Models any other TM

• Can be used to calculate anything

Wolfram’s 2-state 3-symbol UTMA B

0 P1,R,B P2,L,A

1 P2,L,A P2,R,B

2 P1,L,A P0,L,A

Page 20: Formal methods   3 - languages and machines

Referenceshttp://www.liacs.nl/~hoogeboo/praatjes/tarragona/pda-intro.pdf

http://www.infor.uva.es/~teodoro/valladolid-beamer-version.pdf

http://www.vuse.vanderbilt.edu/~koutsoxd/www/Teaching/oldwww/CS252/CS252_lecture16.pdf

http://cstheory.stackexchange.com/questions/625/relationship-between-turing-machine-and-lambda-calculus

http://www.win.tue.nl/~wijers/shallit.pdf

Wikipediahttp://tinyurl.com/k45ngsc

Page 21: Formal methods   3 - languages and machines