core_templates3/postfix.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 05 Nov 2021 16:47:55 +0000
changeset 396 3ffe978a5664
parent 346 pre_templates3/postfix.scala@663c2a9108d1
child 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
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
288
65731df141a5 updated
Christian Urban <urbanc@in.tum.de>
parents: 220
diff changeset
    23
// (1) Implement below the shunting yard algorithm. The most
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
// convenient way to this in Scala is to implement a recursive 
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    25
// function and to heavily use pattern matching. The function syard 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    26
// takes some input tokens as first argument. The second and third 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    27
// arguments represent the stack and the output of the shunting yard 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    28
// algorithm.
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
//
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
// In the marking, you can assume the function is called only with 
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    31
// an empty stack and an empty output list. You can also assume the
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    32
// input os  only properly formatted (infix) arithmetic expressions
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    33
// (all parentheses will be well-nested, the input only contains 
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
// operators and numbers).
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    36
// You can implement any additional helper function you need. I found 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    37
// it helpful to implement two auxiliary functions for the pattern matching:  
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    38
// 
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    39
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    40
def is_op(op: String) : Boolean = ???
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    41
def prec(op1: String, op2: String) : Boolean = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
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 = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
// test cases
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
//syard(split("3 + 4 * ( 2 - 1 )"))  // 3 4 2 1 - * +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
//syard(split("10 + 12 * 33"))       // 10 12 33 * +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
//syard(split("( 5 + 7 ) * 2"))      // 5 7 + 2 *
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
//syard(split("5 + 7 / 2"))          // 5 7 2 / +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
//syard(split("5 * 7 / 2"))          // 5 7 * 2 /
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
//syard(split("9 + 24 / ( 7 - 3 )")) // 9 24 7 3 - / +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
//syard(split("3 + 4 + 5"))           // 3 4 + 5 +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
//syard(split("( ( 3 + 4 ) + 5 )"))    // 3 4 + 5 +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
//syard(split("( 3 + ( 4 + 5 ) )"))    // 3 4 5 + +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
//syard(split("( ( ( 3 ) ) + ( ( 4 + ( 5 ) ) ) )")) // 3 4 5 + +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
 
288
65731df141a5 updated
Christian Urban <urbanc@in.tum.de>
parents: 220
diff changeset
    61
// (2) Implement a compute function that evaluates an input list
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    62
// in postfix notation. This function takes a list of tokens
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    63
// and a stack as argumenta. The function should produce the 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    64
// result as an integer using the stack. You can assume 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    65
// this function will be only called with proper postfix 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    66
// expressions.    
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 288
diff changeset
    68
def compute(toks: Toks, st: List[Int] = Nil) : Int = ???
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
// test cases
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
// compute(syard(split("3 + 4 * ( 2 - 1 )")))  // 7
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
// compute(syard(split("10 + 12 * 33")))       // 406
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
// compute(syard(split("( 5 + 7 ) * 2")))      // 24
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
// compute(syard(split("5 + 7 / 2")))          // 8
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
// compute(syard(split("5 * 7 / 2")))          // 17
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
// compute(syard(split("9 + 24 / ( 7 - 3 )"))) // 15
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
288
65731df141a5 updated
Christian Urban <urbanc@in.tum.de>
parents: 220
diff changeset
    79
}
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81