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