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