templates4/postfix.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 30 Oct 2019 14:07:58 +0000
changeset 288 65731df141a5
parent 220 3020f8c76baa
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
288
65731df141a5 updated
Christian Urban <urbanc@in.tum.de>
parents: 220
diff changeset
     5
object CW9a {
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
// 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    39
// def is_op(op: String) : Boolean = ...
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    40
// def prec(op1: String, op2: String) : Boolean = ...
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    43
// def syard(toks: Toks, st: Toks = Nil, out: Toks = Nil) : Toks = ...
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
// test cases
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
//syard(split("3 + 4 * ( 2 - 1 )"))  // 3 4 2 1 - * +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
//syard(split("10 + 12 * 33"))       // 10 12 33 * +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
//syard(split("( 5 + 7 ) * 2"))      // 5 7 + 2 *
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("9 + 24 / ( 7 - 3 )")) // 9 24 7 3 - / +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
//syard(split("3 + 4 + 5"))           // 3 4 + 5 +
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
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
 
288
65731df141a5 updated
Christian Urban <urbanc@in.tum.de>
parents: 220
diff changeset
    60
// (2) Implement a compute function that evaluates an input list
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    61
// in postfix notation. This function takes a list of tokens
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    62
// and a stack as argumenta. The function should produce the 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    63
// result as an integer using the stack. You can assume 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    64
// this function will be only called with proper postfix 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    65
// expressions.    
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    67
// def compute(toks: Toks, st: List[Int] = Nil) : Int = ...
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
// test cases
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
// compute(syard(split("3 + 4 * ( 2 - 1 )")))  // 7
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
// compute(syard(split("10 + 12 * 33")))       // 406
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
// compute(syard(split("( 5 + 7 ) * 2")))      // 24
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
// compute(syard(split("5 + 7 / 2")))          // 8
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
// compute(syard(split("5 * 7 / 2")))          // 17
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
// compute(syard(split("9 + 24 / ( 7 - 3 )"))) // 15
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
288
65731df141a5 updated
Christian Urban <urbanc@in.tum.de>
parents: 220
diff changeset
    78
}
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80