4. 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
}
5. 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.
6. CFL - samples
• Canonical example: anbn
o S → ε
o S → aSb
• Parentheses: ((()())())
o S → ε
o S → SS
o S → (S)
• Arithmetic expressions
8. (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*
9. (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×{λ}
10. 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.
11. 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)
12. 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
13. The most General Tool
Either Lambda-calculus, or Turing machine
they are equivalent
and define recursive functions
14. 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
15. 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
17. 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 Beaver
S(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
18. Halting Problem
Given a Turing machine, can we decide if it ever stops?
http://ro-che.info/ccc/03
19. Universal Turing Machine
• Models any other TM
• Can be used to calculate anything
Wolfram’s 2-state 3-symbol UTM
A B
0 P1,R,B P2,L,A
1 P2,L,A P2,R,B
2 P1,L,A P0,L,A