templates4/postfix.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 28 Nov 2018 17:13:40 +0000
changeset 219 44161f2c3226
child 220 3020f8c76baa
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Shunting Yard Algorithm
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
// by Edsger Dijkstra
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
// ========================
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
type Toks = List[String]
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
// the operations in the simple version
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
val ops = List("+", "-", "*", "/")
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
// the precedences of the operators
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
val precs = Map("+" -> 1,
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
		"-" -> 1,
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
		"*" -> 2,
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
		"/" -> 2)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
// helper function for splitting strings into tokens
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
def split(s: String) : Toks = s.split(" ").toList
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
// (6) Implement below the shunting yard algorithm. The most
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
// convenient way to this in Scala is to implement a recursive 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
// function using pattern matching. The function takes some input 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
// tokens as first argument. The second and third arguments represent 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
// the stack and the output or the shunting yard algorithm.
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
//
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
// In the marking, you can assume the function is called only with 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
// an empty stack and empty output list. You can also assume the
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
// input are only properly formated (infix) arithmetic expressions
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
// (for example all parentheses are well-nested, the input only contains 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
// operators and numbers).
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
// You can implement any helper function you need. I found it helpful 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
// to implement auxiliary functions:  
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
def is_op(op: String) : Boolean = ops.contains(op)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
def prec(op1: String, op2: String) : Boolean = precs(op1) <= precs(op2)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
def syard(toks: Toks, st: Toks = Nil, out: Toks = Nil) : Toks = (toks, st, out) match {
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  case (Nil, _, _) => out.reverse ::: st
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
  case (num::in, st, out) if (num.forall(_.isDigit)) => 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
    syard(in, st, num :: out)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
  case (op1::in, op2::st, out)  if (is_op(op1) && is_op(op2) && prec(op1, op2)) =>
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
    syard(op1::in, st, op2 :: out) 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  case (op1::in, st, out) if (is_op(op1)) => syard(in, op1::st, out)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  case ("("::in, st, out) => syard(in, "("::st, out)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  case (")"::in, op2::st, out) =>
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
    if (op2 == "(") syard(in, st, out) else syard(")"::in, st, op2 :: out)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  case (in, st, out) => {
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
    println(s"in: ${in}   st: ${st}   out: ${out.reverse}")
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
    Nil
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
  }  
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
} 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
// test cases
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
//syard(split("3 + 4 * ( 2 - 1 )"))  // 3 4 2 1 - * +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
//syard(split("10 + 12 * 33"))       // 10 12 33 * +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
//syard(split("( 5 + 7 ) * 2"))      // 5 7 + 2 *
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
//syard(split("5 + 7 / 2"))          // 5 7 2 / +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
//syard(split("5 * 7 / 2"))          // 5 7 * 2 /
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
//syard(split("9 + 24 / ( 7 - 3 )")) // 9 24 7 3 - / +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
//syard(split("3 + 4 + 5"))           // 3 4 + 5 +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
//syard(split("( ( 3 + 4 ) + 5 )"))    // 3 4 + 5 +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
//syard(split("( 3 + ( 4 + 5 ) )"))    // 3 4 5 + +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
//syard(split("( ( ( 3 ) ) + ( ( 4 + ( 5 ) ) ) )")) // 3 4 5 + +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
// (7) Implement a compute function that evaluates an input list
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
// in postfix notation. This function takes an input list of tokens
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
// and a stack as argument. The function should produce the 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
// result in form of an integer using the stack. You can assume 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
// this function will be only called with proper postfix expressions.    
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
def op_comp(s: String, n1: Int, n2: Int) = s match {
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  case "+" => n2 + n1
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  case "-" => n2 - n1
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
  case "*" => n2 * n1
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
  case "/" => n2 / n1
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
} 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
def compute(toks: Toks, st: List[Int] = Nil) : Int = (toks, st) match {
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
  case (Nil, st) => st.head
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  case (op::in, n1::n2::st) if (is_op(op)) => compute(in, op_comp(op, n1, n2)::st)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  case (num::in, st) => compute(in, num.toInt::st)  
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
}
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
// test cases
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
// compute(syard(split("3 + 4 * ( 2 - 1 )")))  // 7
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
// compute(syard(split("10 + 12 * 33")))       // 406
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
// compute(syard(split("( 5 + 7 ) * 2")))      // 24
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
// compute(syard(split("5 + 7 / 2")))          // 8
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
// compute(syard(split("5 * 7 / 2")))          // 17
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
// compute(syard(split("9 + 24 / ( 7 - 3 )"))) // 15
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102