templates4/postfix.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 07 Aug 2019 16:15:42 +0100
changeset 273 b19227660752
parent 220 3020f8c76baa
child 288 65731df141a5
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
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
     6
// type of tokens
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
type Toks = List[String]
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
     9
// the operations in the basic version of the algorithm
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
val ops = List("+", "-", "*", "/")
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
// the precedences of the operators
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
val precs = Map("+" -> 1,
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
		"-" -> 1,
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
		"*" -> 2,
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
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
// helper function for splitting strings into tokens
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
def split(s: String) : Toks = s.split(" ").toList
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
// (6) Implement below the shunting yard algorithm. The most
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
// convenient way to this in Scala is to implement a recursive 
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    24
// function and to heavily use pattern matching. The function syard 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    25
// takes some input tokens as first argument. The second and third 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    26
// arguments represent the stack and the output of the shunting yard 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    27
// algorithm.
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
//
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
// 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
    30
// 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
    31
// input os  only properly formatted (infix) arithmetic expressions
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    32
// (all parentheses will be well-nested, the input only contains 
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
// operators and numbers).
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
// You can implement any additional helper function you need. I found 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    36
// it helpful to implement two auxiliary functions for the pattern matching:  
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    37
// 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    38
// def is_op(op: String) : Boolean = ...
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    39
// def prec(op1: String, op2: String) : Boolean = ...
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    42
// def syard(toks: Toks, st: Toks = Nil, out: Toks = Nil) : Toks = ...
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
// test cases
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
//syard(split("3 + 4 * ( 2 - 1 )"))  // 3 4 2 1 - * +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
//syard(split("10 + 12 * 33"))       // 10 12 33 * +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
//syard(split("( 5 + 7 ) * 2"))      // 5 7 + 2 *
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("9 + 24 / ( 7 - 3 )")) // 9 24 7 3 - / +
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
//syard(split("3 + 4 + 5"))           // 3 4 + 5 +
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
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
 
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
// (7) Implement a compute function that evaluates an input list
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    60
// in postfix notation. This function takes a list of tokens
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    61
// and a stack as argumenta. The function should produce the 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    62
// result as an integer using the stack. You can assume 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    63
// this function will be only called with proper postfix 
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    64
// expressions.    
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
220
3020f8c76baa updated
Christian Urban <urbanc@in.tum.de>
parents: 219
diff changeset
    66
// def compute(toks: Toks, st: List[Int] = Nil) : Int = ...
219
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
// test cases
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
// compute(syard(split("3 + 4 * ( 2 - 1 )")))  // 7
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
// compute(syard(split("10 + 12 * 33")))       // 406
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
// compute(syard(split("( 5 + 7 ) * 2")))      // 24
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
// compute(syard(split("5 + 7 / 2")))          // 8
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
// compute(syard(split("5 * 7 / 2")))          // 17
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
// compute(syard(split("9 + 24 / ( 7 - 3 )"))) // 15
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
44161f2c3226 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79