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