// Shunting Yard Algorithm // including Associativity for Operators // =====================================object CW9b {// type of tokenstype Toks = List[String]// helper function for splitting strings into tokensdef split(s: String) : Toks = s.split(" ").toList// left- and right-associativityabstract class Assoccase object LA extends Assoccase object RA extends Assoc// power is right-associative,// everything else is left-associativedef assoc(s: String) : Assoc = s match { case "^" => RA case _ => LA}// the precedences of the operatorsval precs = Map("+" -> 1, "-" -> 1, "*" -> 2, "/" -> 2, "^" -> 4)// the operations in the basic version of the algorithmval ops = List("+", "-", "*", "/", "^")// (3) Implement the extended version of the shunting yard algorithm.// This version should properly account for the fact that the power // operation is right-associative. Apart from the extension to include// the power operation, you can make the same assumptions as in // basic version.// def syard(toks: Toks, st: Toks = Nil, out: Toks = Nil) : Toks = ...// test cases// syard(split("3 + 4 * 8 / ( 5 - 1 ) ^ 2 ^ 3")) // 3 4 8 * 5 1 - 2 3 ^ ^ / +// (4) Implement a compute function that produces a Long(!) for an// input list of tokens in postfix notation.//def compute(toks: Toks, st: List[Long] = Nil) : Long = ...// test cases// compute(syard(split("3 + 4 * ( 2 - 1 )"))) // 7// compute(syard(split("10 + 12 * 33"))) // 406// compute(syard(split("( 5 + 7 ) * 2"))) // 24// compute(syard(split("5 + 7 / 2"))) // 8// compute(syard(split("5 * 7 / 2"))) // 17// compute(syard(split("9 + 24 / ( 7 - 3 )"))) // 15// compute(syard(split("4 ^ 3 ^ 2"))) // 262144// compute(syard(split("4 ^ ( 3 ^ 2 )"))) // 262144// compute(syard(split("( 4 ^ 3 ) ^ 2"))) // 4096// compute(syard(split("( 3 + 1 ) ^ 2 ^ 3"))) // 65536}