415
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 |
// by Edsger Dijkstra
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
// ========================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
object C3a {
|
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 |
// the operations in the basic version of the algorithm
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
val ops = List("+", "-", "*", "/")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
// the precedences of the operators
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
val precs = Map("+" -> 1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
"-" -> 1,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
"*" -> 2,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
"/" -> 2)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
// helper function for splitting strings into tokens
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
def split(s: String) : Toks = s.split(" ").toList
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
// (1) Implement below the shunting yard algorithm. The most
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
// convenient way to this in Scala is to implement a recursive
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
// function and to heavily use pattern matching. The function syard
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
// takes some input tokens as first argument. The second and third
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
// arguments represent the stack and the output of the shunting yard
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
// algorithm.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
// In the marking, you can assume the function is called only with
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
// an empty stack and an empty output list. You can also assume the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
// input os only properly formatted (infix) arithmetic expressions
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
// (all parentheses will be well-nested, the input only contains
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
// operators and numbers).
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
// You can implement any additional helper function you need. I found
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
// it helpful to implement two auxiliary functions for the pattern matching:
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
//
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
def is_op(op: String) : Boolean = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
def prec(op1: String, op2: String) : Boolean = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
def syard(toks: Toks, st: Toks = Nil, out: Toks = Nil) : Toks = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
// test cases
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
//syard(split("3 + 4 * ( 2 - 1 )")) // 3 4 2 1 - * +
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
//syard(split("10 + 12 * 33")) // 10 12 33 * +
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
//syard(split("( 5 + 7 ) * 2")) // 5 7 + 2 *
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
//syard(split("5 + 7 / 2")) // 5 7 2 / +
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
//syard(split("5 * 7 / 2")) // 5 7 * 2 /
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
//syard(split("9 + 24 / ( 7 - 3 )")) // 9 24 7 3 - / +
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
//syard(split("3 + 4 + 5")) // 3 4 + 5 +
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
//syard(split("( ( 3 + 4 ) + 5 )")) // 3 4 + 5 +
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
//syard(split("( 3 + ( 4 + 5 ) )")) // 3 4 5 + +
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
//syard(split("( ( ( 3 ) ) + ( ( 4 + ( 5 ) ) ) )")) // 3 4 5 + +
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
// (2) Implement a compute function that evaluates an input list
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
// in postfix notation. This function takes a list of tokens
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
// and a stack as argumenta. The function should produce the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
64 |
// result as an integer using the stack. You can assume
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
// this function will be only called with proper postfix
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
// expressions.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
def compute(toks: Toks, st: List[Int] = Nil) : Int = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
71 |
// test cases
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
72 |
// compute(syard(split("3 + 4 * ( 2 - 1 )"))) // 7
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
73 |
// compute(syard(split("10 + 12 * 33"))) // 406
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
74 |
// compute(syard(split("( 5 + 7 ) * 2"))) // 24
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
// compute(syard(split("5 + 7 / 2"))) // 8
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
// compute(syard(split("5 * 7 / 2"))) // 17
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
77 |
// compute(syard(split("9 + 24 / ( 7 - 3 )"))) // 15
|
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 |
|