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