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