assignment2021scala/core3/postfix2.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 08 Nov 2021 23:17:51 +0000
changeset 415 fced9a61c881
permissions -rw-r--r--
update
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
415
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Shunting Yard Algorithm 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
// including Associativity for Operators 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
// =====================================
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
object C3b {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
// type of tokens
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
type Toks = List[String]
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
// helper function for splitting strings into tokens
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
def split(s: String) : Toks = s.split(" ").toList
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
// left- and right-associativity
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
abstract class Assoc
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
case object LA extends Assoc
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
case object RA extends Assoc
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
// power is right-associative,
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
// everything else is left-associative
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
def assoc(s: String) : Assoc = s match {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
  case "^" => RA
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
  case _ => LA
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
}
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
// the precedences of the operators
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
val precs = Map("+" -> 1,
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
  		"-" -> 1,
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
		"*" -> 2,
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
		"/" -> 2,
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
                "^" -> 4)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
// the operations in the basic version of the algorithm
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
val ops = List("+", "-", "*", "/", "^")
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
// (3) Implement the extended version of the shunting yard algorithm.
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
// This version should properly account for the fact that the power 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
// operation is right-associative. Apart from the extension to include
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
// the power operation, you can make the same assumptions as in 
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
// basic version.
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
def syard(toks: Toks, st: Toks = Nil, out: Toks = Nil) : Toks = ???
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
// test cases
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
// syard(split("3 + 4 * 8 / ( 5 - 1 ) ^ 2 ^ 3"))  // 3 4 8 * 5 1 - 2 3 ^ ^ / +
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
// (4) Implement a compute function that produces an Int for an
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
// input list of tokens in postfix notation.
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
def compute(toks: Toks, st: List[Int] = Nil) : Int = ???
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
// test cases
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
// compute(syard(split("3 + 4 * ( 2 - 1 )")))   // 7
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
// compute(syard(split("10 + 12 * 33")))       // 406
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
// compute(syard(split("( 5 + 7 ) * 2")))      // 24
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
// compute(syard(split("5 + 7 / 2")))          // 8
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
// compute(syard(split("5 * 7 / 2")))          // 17
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
// compute(syard(split("9 + 24 / ( 7 - 3 )"))) // 15
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
// compute(syard(split("4 ^ 3 ^ 2")))      // 262144
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
// compute(syard(split("4 ^ ( 3 ^ 2 )")))  // 262144
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
// compute(syard(split("( 4 ^ 3 ) ^ 2")))  // 4096
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
// compute(syard(split("( 3 + 1 ) ^ 2 ^ 3")))   // 65536
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
}