progs/toks.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 14 Nov 2019 13:17:02 +0000
changeset 687 8865f4f2be59
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
687
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
abstract class Token
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
case object T_SEMI extends Token
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
case object T_LPAREN extends Token
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
case object T_RPAREN extends Token
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
case class T_ID(s: String) extends Token
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
case class T_OP(s: String) extends Token
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
case class T_NUM(n: Int) extends Token
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
case class T_KWD(s: String) extends Token
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
case class T_STR(s: String) extends Token
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
// Loops program
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
//===============
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
/*
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
start := 1000; 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
x := start;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
y := start;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
z := start;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
while 0 < x do {
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
 while 0 < y do {
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  while 0 < z do { z := z - 1 };
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  z := start;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  y := y - 1
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
 };     
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
 y := start;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
 x := x - 1
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
}
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
*/
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
val loops =
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  List(T_ID("start"), T_OP(":="), T_NUM(1000), T_SEMI, T_ID("x"), T_OP(":="), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
       T_ID("start"), T_SEMI, T_ID("y"), T_OP(":="), T_ID("start"), T_SEMI, 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
       T_ID("z"), T_OP(":="), T_ID("start"), T_SEMI, T_KWD("while"), T_NUM(0), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
       T_OP("<"), T_ID("x"), T_KWD("do"), T_LPAREN, T_KWD("while"), T_NUM(0), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
       T_OP("<"), T_ID("y"), T_KWD("do"), T_LPAREN, T_KWD("while"), T_NUM(0), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
       T_OP("<"), T_ID("z"), T_KWD("do"), T_LPAREN, T_ID("z"), T_OP(":="), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
       T_ID("z"), T_OP("-"), T_NUM(1), T_RPAREN, T_SEMI, T_ID("z"), T_OP(":="),
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
       T_ID("start"), T_SEMI, T_ID("y"), T_OP(":="), T_ID("y"), T_OP("-"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
       T_NUM(1), T_RPAREN, T_SEMI, T_ID("y"), T_OP(":="), T_ID("start"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
       T_SEMI, T_ID("x"), T_OP(":="), T_ID("x"), T_OP("-"), T_NUM(1), T_RPAREN) 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
// Fib program
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
//=============
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
/*
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
write "Fib";
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
read n;  
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
minus1 := 0;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
minus2 := 1;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
while n > 0 do {
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
       temp := minus2;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
       minus2 := minus1 + minus2;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
       minus1 := temp;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
       n := n - 1
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
};
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
write "Result";
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
write minus2
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
*/
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
val fib =
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  List(T_KWD("write"), T_STR("Fib"), T_SEMI, T_KWD("read"), T_ID("n"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
       T_SEMI, T_ID("minus1"), T_OP(":="), T_NUM(0), T_SEMI, T_ID("minus2"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
       T_OP(":="), T_NUM(1), T_SEMI, T_KWD("while"), T_ID("n"), T_OP(">"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
       T_NUM(0), T_KWD("do"), T_LPAREN, T_ID("temp"), T_OP(":="), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
       T_ID("minus2"), T_SEMI, T_ID("minus2"), T_OP(":="), T_ID("minus1"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
       T_OP("+"), T_ID("minus2"), T_SEMI, T_ID("minus1"), T_OP(":="), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
       T_ID("temp"), T_SEMI, T_ID("n"), T_OP(":="), T_ID("n"), T_OP("-"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
       T_NUM(1), T_RPAREN, T_SEMI, T_KWD("write"), T_STR("Result"), T_SEMI, 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
       T_KWD("write"), T_ID("minus2"))
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
// Factors program
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
//=================
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
/*
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
write "Input n please";
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
read n;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
write "The factors of n are";
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
f := 2;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
while n != 1 do {
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
    while (n / f) * f == n do {
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
        write f;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
        n := n / f
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
    };
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
    f := f + 1
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
}
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
*/
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
val factors = 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
  List(T_KWD("write"), T_STR("Input n please"), T_SEMI, T_KWD("read"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
       T_ID("n"), T_SEMI, T_KWD("write"), T_STR("The factors of n are"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
       T_SEMI, T_ID("f"), T_OP(":="), T_NUM(2), T_SEMI, T_KWD("while"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
       T_ID("n"), T_OP("!="), T_NUM(1), T_KWD("do"), T_LPAREN, 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
       T_KWD("while"), T_ID("n"), T_OP("/"), T_ID("f"), T_OP("*"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
       T_ID("f"), T_OP("=="), T_ID("n"), T_KWD("do"), T_LPAREN, 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
       T_KWD("write"), T_ID("f"), T_SEMI, T_ID("n"), T_OP(":="), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
       T_ID("n"), T_OP("/"), T_ID("f"), T_RPAREN, T_SEMI, T_ID("f"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
       T_OP(":="), T_ID("f"), T_OP("+"), T_NUM(1), T_RPAREN)
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
// Primes program
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
//================
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
/*
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
end := 100;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
n := 2;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
while (n < end) do {
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
  f := 2;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  tmp := 0;
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
  while ((f < n / 2 + 1) && (tmp == 0)) do {
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
    if ((n / f) * f == n) then  { tmp := 1 } else { skip };
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
    f := f + 1
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
  };
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
  if (tmp == 0) then { write(n) } else { skip };
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
  n  := n + 1
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
}
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
*/
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
val primes =
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
  List(T_ID("end"), T_OP(":="), T_NUM(100), T_SEMI, T_ID("n"), T_OP(":="), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
       T_NUM(2), T_SEMI, T_KWD("while"), T_ID("n"), T_OP("<"), T_ID("end"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
       T_KWD("do"), T_LPAREN, T_ID("f"), T_OP(":="), T_NUM(2), T_SEMI, 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
       T_ID("tmp"), T_OP(":="), T_NUM(0), T_SEMI, T_KWD("while"), T_ID("f"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
       T_OP("<"), T_ID("n"), T_OP("/"), T_NUM(2), T_OP("+"), T_NUM(1), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
       T_OP("&&"), T_ID("tmp"), T_OP("=="), T_NUM(0), T_KWD("do"), T_LPAREN, 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
       T_KWD("if"), T_ID("n"), T_OP("/"), T_ID("f"), T_OP("*"), T_ID("f"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
       T_OP("=="), T_ID("n"), T_KWD("then"), T_LPAREN, T_ID("tmp"), T_OP(":="),
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
       T_NUM(1), T_RPAREN, T_KWD("else"), T_LPAREN, T_KWD("skip"), T_RPAREN, 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
       T_SEMI, T_ID("f"), T_OP(":="), T_ID("f"), T_OP("+"), T_NUM(1), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
       T_RPAREN, T_SEMI, T_KWD("if"), T_ID("tmp"), T_OP("=="), T_NUM(0), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
       T_KWD("then"), T_LPAREN, T_KWD("write"), T_ID("n"), T_RPAREN, 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
       T_KWD("else"), T_LPAREN, T_KWD("skip"), T_RPAREN, T_SEMI, T_ID("n"), 
8865f4f2be59 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
       T_OP(":="), T_ID("n"), T_OP("+"), T_NUM(1), T_RPAREN)