core_templates3/postfix.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 02 Nov 2023 12:37:58 +0000
changeset 473 ac79c2e534bd
parent 428 cdfa6a293453
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
396
3ffe978a5664 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
     5
object C3a {
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
     7
// type of tokens
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
type Toks = List[String]
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    10
// the operations in the basic version of the algorithm
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
val ops = List("+", "-", "*", "/")
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
// the precedences of the operators
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
val precs = Map("+" -> 1,
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
		"-" -> 1,
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
		"/" -> 2)
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
// helper function for splitting strings into tokens
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
def split(s: String) : Toks = s.split(" ").toList
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
    22
// ADD YOUR CODE BELOW
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
    23
//======================
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
    26
// (1) 
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    27
def is_op(op: String) : Boolean = ???
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    28
def prec(op1: String, op2: String) : Boolean = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    30
def syard(toks: Toks, st: Toks = Nil, out: Toks = Nil) : Toks = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
// test cases
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
//syard(split("3 + 4 * ( 2 - 1 )"))  // 3 4 2 1 - * +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
//syard(split("10 + 12 * 33"))       // 10 12 33 * +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
//syard(split("( 5 + 7 ) * 2"))      // 5 7 + 2 *
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
//syard(split("5 + 7 / 2"))          // 5 7 2 / +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
//syard(split("5 * 7 / 2"))          // 5 7 * 2 /
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
//syard(split("9 + 24 / ( 7 - 3 )")) // 9 24 7 3 - / +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
//syard(split("3 + 4 + 5"))           // 3 4 + 5 +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
//syard(split("( ( 3 + 4 ) + 5 )"))    // 3 4 + 5 +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
//syard(split("( 3 + ( 4 + 5 ) )"))    // 3 4 5 + +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
//syard(split("( ( ( 3 ) ) + ( ( 4 + ( 5 ) ) ) )")) // 3 4 5 + +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
 
428
cdfa6a293453 updated solutions and templates
Christian Urban <christian.urban@kcl.ac.uk>
parents: 396
diff changeset
    47
// (2) 
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    48
def compute(toks: Toks, st: List[Int] = Nil) : Int = ???
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
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
// test cases
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
// compute(syard(split("3 + 4 * ( 2 - 1 )")))  // 7
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
// compute(syard(split("10 + 12 * 33")))       // 406
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
// compute(syard(split("( 5 + 7 ) * 2")))      // 24
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
// compute(syard(split("5 + 7 / 2")))          // 8
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
// compute(syard(split("5 * 7 / 2")))          // 17
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
// compute(syard(split("9 + 24 / ( 7 - 3 )"))) // 15
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
288
65731df141a5 updated
Christian Urban <urbanc@in.tum.de>
parents: 220
diff changeset
    59
}
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61