| 220 |      1 | // Shunting Yard Algorithm 
 | 
|  |      2 | // including Associativity for Operators 
 | 
|  |      3 | // =====================================
 | 
| 219 |      4 | 
 | 
| 220 |      5 | // type of tokens
 | 
| 219 |      6 | type Toks = List[String]
 | 
|  |      7 | 
 | 
| 220 |      8 | // helper function for splitting strings into tokens
 | 
|  |      9 | def split(s: String) : Toks = s.split(" ").toList
 | 
|  |     10 | 
 | 
|  |     11 | // left- and right-associativity
 | 
|  |     12 | abstract class Assoc
 | 
|  |     13 | case object LA extends Assoc
 | 
|  |     14 | case object RA extends Assoc
 | 
| 219 |     15 | 
 | 
|  |     16 | 
 | 
| 220 |     17 | // power is right-associative,
 | 
|  |     18 | // everything else is left-associative
 | 
| 219 |     19 | def assoc(s: String) : Assoc = s match {
 | 
|  |     20 |   case "^" => RA
 | 
|  |     21 |   case _ => LA
 | 
|  |     22 | }
 | 
|  |     23 | 
 | 
|  |     24 | 
 | 
| 220 |     25 | // the precedences of the operators
 | 
| 219 |     26 | val precs = Map("+" -> 1,
 | 
| 220 |     27 |   		"-" -> 1,
 | 
|  |     28 | 		"*" -> 2,
 | 
|  |     29 | 		"/" -> 2,
 | 
|  |     30 |                 "^" -> 4)
 | 
| 219 |     31 | 
 | 
| 220 |     32 | // the operations in the basic version of the algorithm
 | 
| 219 |     33 | val ops = List("+", "-", "*", "/", "^")
 | 
|  |     34 | 
 | 
| 220 |     35 | // (8) Implement the extended version of the shunting yard algorithm.
 | 
|  |     36 | // This version should properly account for the fact that the power 
 | 
|  |     37 | // operation is right-associative. Apart from the extension to include
 | 
|  |     38 | // the power operation, you can make the same assumptions as in 
 | 
|  |     39 | // basic version.
 | 
| 219 |     40 | 
 | 
| 220 |     41 | // def syard(toks: Toks, st: Toks = Nil, out: Toks = Nil) : Toks = ...
 | 
|  |     42 | 
 | 
| 219 |     43 | 
 | 
| 220 |     44 | // test cases
 | 
|  |     45 | // syard(split("3 + 4 * 8 / ( 5 - 1 ) ^ 2 ^ 3"))  // 3 4 8 * 5 1 - 2 3 ^ ^ / +
 | 
| 219 |     46 | 
 | 
|  |     47 | 
 | 
| 220 |     48 | // (9) Implement a compute function that produces a Long(!) for an
 | 
|  |     49 | // input list of tokens in postfix notation.
 | 
|  |     50 | 
 | 
|  |     51 | //def compute(toks: Toks, st: List[Long] = Nil) : Long = ...
 | 
| 219 |     52 | 
 | 
|  |     53 | 
 | 
| 220 |     54 | // test cases
 | 
|  |     55 | // compute(syard(split("3 + 4 * ( 2 - 1 )")))   // 7
 | 
|  |     56 | // compute(syard(split("10 + 12 * 33")))       // 406
 | 
|  |     57 | // compute(syard(split("( 5 + 7 ) * 2")))      // 24
 | 
|  |     58 | // compute(syard(split("5 + 7 / 2")))          // 8
 | 
|  |     59 | // compute(syard(split("5 * 7 / 2")))          // 17
 | 
|  |     60 | // compute(syard(split("9 + 24 / ( 7 - 3 )"))) // 15
 | 
|  |     61 | // compute(syard(split("4 ^ 3 ^ 2")))      // 262144
 | 
|  |     62 | // compute(syard(split("4 ^ ( 3 ^ 2 )")))  // 262144
 | 
|  |     63 | // compute(syard(split("( 4 ^ 3 ) ^ 2")))  // 4096
 | 
|  |     64 | // compute(syard(split("( 3 + 1 ) ^ 2 ^ 3")))   // 65536
 | 
| 219 |     65 | 
 |