core_templates3/postfix2.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 06 Jun 2024 22:18:15 +0100
changeset 490 4778fefecd0c
parent 428 cdfa6a293453
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
     1
// Shunting Yard Algorithm 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
     2
// including Associativity for Operators 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
     3
// =====================================
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
     5
object C3b {
288
65731df141a5 updated
Christian Urban <urbanc@in.tum.de>
parents: 220
diff changeset
     6
65731df141a5 updated
Christian Urban <urbanc@in.tum.de>
parents: 220
diff changeset
     7
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
     8
// type of tokens
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
type Toks = List[String]
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    11
// helper function for splitting strings into tokens
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    12
def split(s: String) : Toks = s.split(" ").toList
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    13
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    14
// left- and right-associativity
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    15
abstract class Assoc
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    16
case object LA extends Assoc
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    17
case object RA extends Assoc
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    20
// power is right-associative,
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    21
// everything else is left-associative
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
def assoc(s: String) : Assoc = s match {
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  case "^" => RA
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  case _ => LA
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
}
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    28
// the precedences of the operators
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
val precs = Map("+" -> 1,
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    30
  		"-" -> 1,
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    31
		"*" -> 2,
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    32
		"/" -> 2,
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    33
                "^" -> 4)
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    35
// the operations in the basic version of the algorithm
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
val ops = List("+", "-", "*", "/", "^")
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
    38
// ADD YOUR CODE BELOW
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
    39
//======================
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
    41
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
    42
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
    43
// (3) 
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    44
def syard(toks: Toks, st: Toks = Nil, out: Toks = Nil) : Toks = ???
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    45
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    47
// test cases
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    48
// syard(split("3 + 4 * 8 / ( 5 - 1 ) ^ 2 ^ 3"))  // 3 4 8 * 5 1 - 2 3 ^ ^ / +
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
    51
// (4)
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    52
def compute(toks: Toks, st: List[Int] = Nil) : Int = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    55
// test cases
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    56
// compute(syard(split("3 + 4 * ( 2 - 1 )")))   // 7
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    57
// compute(syard(split("10 + 12 * 33")))       // 406
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    58
// compute(syard(split("( 5 + 7 ) * 2")))      // 24
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    59
// compute(syard(split("5 + 7 / 2")))          // 8
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    60
// compute(syard(split("5 * 7 / 2")))          // 17
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    61
// compute(syard(split("9 + 24 / ( 7 - 3 )"))) // 15
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    62
// compute(syard(split("4 ^ 3 ^ 2")))      // 262144
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    63
// compute(syard(split("4 ^ ( 3 ^ 2 )")))  // 262144
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    64
// compute(syard(split("( 4 ^ 3 ) ^ 2")))  // 4096
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    65
// compute(syard(split("( 3 + 1 ) ^ 2 ^ 3")))   // 65536
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
288
65731df141a5 updated
Christian Urban <urbanc@in.tum.de>
parents: 220
diff changeset
    67
}