384
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// Shunting Yard Algorithm
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
// including Associativity for Operators
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
// =====================================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
|
386
|
5 |
object CW8b {
|
384
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
// type of tokens
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
type Toks = List[String]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
// helper function for splitting strings into tokens
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
def split(s: String) : Toks = s.split(" ").toList
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
// left- and right-associativity
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
abstract class Assoc
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
case object LA extends Assoc
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
case object RA extends Assoc
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
// power is right-associative,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
// everything else is left-associative
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
def assoc(s: String) : Assoc = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
case "^" => RA
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
case _ => LA
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
// the precedences of the operators
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
val precs = Map("+" -> 1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
"-" -> 1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
"*" -> 2,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
"/" -> 2,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
"^" -> 4)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
// the operations in the basic version of the algorithm
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
val ops = List("+", "-", "*", "/", "^")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
// (8) Implement the extended version of the shunting yard algorithm.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
// This version should properly account for the fact that the power
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
// operation is right-associative. Apart from the extension to include
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
// the power operation, you can make the same assumptions as in
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
// basic version.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
def is_op(op: String) : Boolean = ops.contains(op)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
def prec(op1: String, op2: String) : Boolean = assoc(op1) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
case LA => precs(op1) <= precs(op2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
case RA => precs(op1) < precs(op2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
def syard(toks: Toks, st: Toks = Nil, out: Toks = Nil) : Toks = (toks, st, out) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
case (Nil, _, _) => out.reverse ::: st
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
case (num::in, st, out) if (num.forall(_.isDigit)) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
syard(in, st, num :: out)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
case (op1::in, op2::st, out) if (is_op(op1) && is_op(op2) && prec(op1, op2)) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
syard(op1::in, st, op2 :: out)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
case (op1::in, st, out) if (is_op(op1)) => syard(in, op1::st, out)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
case ("("::in, st, out) => syard(in, "("::st, out)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
case (")"::in, op2::st, out) =>
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
if (op2 == "(") syard(in, st, out) else syard(")"::in, st, op2 :: out)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
case (in, st, out) => {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
println(s"in: ${in} st: ${st} out: ${out.reverse}")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
Nil
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
64 |
def op_comp(s: String, n1: Long, n2: Long) = s match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
case "+" => n2 + n1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
case "-" => n2 - n1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
case "*" => n2 * n1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
case "/" => n2 / n1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
case "^" => Math.pow(n2, n1).toLong
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
71 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
72 |
def compute(toks: Toks, st: List[Long] = Nil) : Long = (toks, st) match {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
73 |
case (Nil, st) => st.head
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
74 |
case (op::in, n1::n2::st) if (is_op(op)) => compute(in, op_comp(op, n1, n2)::st)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
case (num::in, st) => compute(in, num.toInt::st)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
77 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
78 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
79 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
80 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
81 |
//compute(syard(split("3 + 4 * ( 2 - 1 )"))) // 7
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
//compute(syard(split("10 + 12 * 33"))) // 406
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
83 |
//compute(syard(split("( 5 + 7 ) * 2"))) // 24
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
84 |
//compute(syard(split("5 + 7 / 2"))) // 8
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
//compute(syard(split("5 * 7 / 2"))) // 17
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
86 |
//compute(syard(split("9 + 24 / ( 7 - 3 )"))) // 15
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
//compute(syard(split("4 ^ 3 ^ 2"))) // 262144
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
//compute(syard(split("4 ^ ( 3 ^ 2 )"))) // 262144
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
//compute(syard(split("( 4 ^ 3 ) ^ 2"))) // 4096
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
//compute(syard(split("( 3 + 1 ) ^ 2 ^ 3"))) // 65536
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
//syard(split("3 + 4 * 8 / ( 5 - 1 ) ^ 2 ^ 3")) // 3 4 8 * 5 1 - 2 3 ^ ^ / +
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
//compute(syard(split("3 + 4 * 8 / ( 5 - 1 ) ^ 2 ^ 3"))) // 3
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
//compute(syard(split("( 3 + 1 ) ^ 2 ^ 3"))) // 65536
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
}
|