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