progs/lexer.scala
author Christian Urban <urbanc@in.tum.de>
Sun, 28 Jul 2019 16:15:03 +0100
changeset 625 6709fa87410b
parent 624 progs/token.scala@8d0af38389bc
child 642 064afa8fc1d9
permissions -rw-r--r--
updated to 2.13
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
     1
// A Simple Lexer according to Sulzmann & Lu
581
19de761b69e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 580
diff changeset
     2
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
     3
import scala.language.implicitConversions    
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
import scala.language.reflectiveCalls
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
     6
abstract class Rexp 
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
     7
case object ZERO extends Rexp
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
     8
case object ONE extends Rexp
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
case class CHAR(c: Char) extends Rexp
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    10
case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    11
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    12
case class STAR(r: Rexp) extends Rexp 
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    13
case class RECD(x: String, r: Rexp) extends Rexp
581
19de761b69e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 580
diff changeset
    14
   
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    15
abstract class Val
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
    16
case object Empty extends Val
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    17
case class Chr(c: Char) extends Val
520
2849c305b12d updated
cu
parents: 450
diff changeset
    18
case class Sequ(v1: Val, v2: Val) extends Val
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    19
case class Left(v: Val) extends Val
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    20
case class Right(v: Val) extends Val
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    21
case class Stars(vs: List[Val]) extends Val
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    22
case class Rec(x: String, v: Val) extends Val
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    23
   
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    24
// some convenience for typing in regular expressions
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    25
def charlist2rexp(s : List[Char]): Rexp = s match {
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    26
  case Nil => ONE
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  case c::Nil => CHAR(c)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
}
581
19de761b69e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 580
diff changeset
    30
implicit def string2rexp(s : String) : Rexp = 
19de761b69e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 580
diff changeset
    31
  charlist2rexp(s.toList)
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
implicit def RexpOps(r: Rexp) = new {
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  def | (s: Rexp) = ALT(r, s)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  def % = STAR(r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  def ~ (s: Rexp) = SEQ(r, s)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
implicit def stringOps(s: String) = new {
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  def | (r: Rexp) = ALT(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  def | (r: String) = ALT(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  def % = STAR(s)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
  def ~ (r: Rexp) = SEQ(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  def ~ (r: String) = SEQ(s, r)
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    45
  def $ (r: Rexp) = RECD(s, r)
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
581
19de761b69e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 580
diff changeset
    48
// A test for more conveninet syntax
19de761b69e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 580
diff changeset
    49
val re : Rexp = ("ab" | "a") ~ ("b" | ONE)
19de761b69e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 580
diff changeset
    50
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
    51
// the nullable function: tests whether the regular 
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    52
// expression can recognise the empty string
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
def nullable (r: Rexp) : Boolean = r match {
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    54
  case ZERO => false
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    55
  case ONE => true
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
  case CHAR(_) => false
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    57
  case ALT(r1, r2) => nullable(r1) || nullable(r2)
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
  case SEQ(r1, r2) => nullable(r1) && nullable(r2)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
  case STAR(_) => true
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    60
  case RECD(_, r1) => nullable(r1)
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
    63
// the derivative of a regular expression w.r.t. a character
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
def der (c: Char, r: Rexp) : Rexp = r match {
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    65
  case ZERO => ZERO
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    66
  case ONE => ZERO
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    67
  case CHAR(d) => if (c == d) ONE else ZERO
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    68
  case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
  case SEQ(r1, r2) => 
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
    if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
    else SEQ(der(c, r1), r2)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
  case STAR(r) => SEQ(der(c, r), STAR(r))
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    73
  case RECD(_, r1) => der(c, r1)
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    74
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    75
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
    76
// the derivative w.r.t. a string (iterates der)
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    77
def ders (s: List[Char], r: Rexp) : Rexp = s match {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    78
  case Nil => r
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    79
  case c::s => ders(s, der(c, r))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    80
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    81
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    82
// extracts a string from value
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    83
def flatten(v: Val) : String = v match {
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
    84
  case Empty => ""
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    85
  case Chr(c) => c.toString
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    86
  case Left(v) => flatten(v)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    87
  case Right(v) => flatten(v)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    88
  case Sequ(v1, v2) => flatten(v1) + flatten(v2)
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    89
  case Stars(vs) => vs.map(flatten).mkString
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    90
  case Rec(_, v) => flatten(v)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    91
}
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    92
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
    93
// extracts an environment from a value;
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
    94
// used for lexing a string
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    95
def env(v: Val) : List[(String, String)] = v match {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    96
  case Empty => Nil
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    97
  case Chr(c) => Nil
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    98
  case Left(v) => env(v)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    99
  case Right(v) => env(v)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   100
  case Sequ(v1, v2) => env(v1) ::: env(v2)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   101
  case Stars(vs) => vs.flatMap(env)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   102
  case Rec(x, v) => (x, flatten(v))::env(v)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   103
}
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   104
625
6709fa87410b updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   105
// The Injection Part of the Lexer
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
   106
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
   107
// calculates a value for how a nullable regex 
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
   108
// matches the empty string 
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   109
def mkeps(r: Rexp) : Val = r match {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   110
  case ONE => Empty
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   111
  case ALT(r1, r2) => 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   112
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   113
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   114
  case STAR(r) => Stars(Nil)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   115
  case RECD(x, r) => Rec(x, mkeps(r))
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   116
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   117
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
   118
// injects back a character into a value
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   119
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
520
2849c305b12d updated
cu
parents: 450
diff changeset
   120
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
2849c305b12d updated
cu
parents: 450
diff changeset
   121
  case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   122
  case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   123
  case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   124
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   125
  case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   126
  case (CHAR(d), Empty) => Chr(c) 
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   127
  case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   128
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   129
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
   130
// the main lexing function (produces a value)
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   131
def lex(r: Rexp, s: List[Char]) : Val = s match {
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   132
  case Nil => if (nullable(r)) mkeps(r) 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   133
              else throw new Exception("Not matched")
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   134
  case c::cs => inj(r, c, lex(der(c, r), cs))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   135
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   136
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   137
def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   138
581
19de761b69e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 580
diff changeset
   139
// a simple test for extracting an environment
19de761b69e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 580
diff changeset
   140
val re1 : Rexp = ("first" $ ("a" | "ab")) ~ ("second" $ ("b" | ONE))
19de761b69e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 580
diff changeset
   141
env(lexing(re1, "ab"))
450
d91a1748a9be updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 426
diff changeset
   142
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   143
// some "rectification" functions for simplification
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   144
def F_ID(v: Val): Val = v
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   145
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   146
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   147
def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   148
  case Right(v) => Right(f2(v))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   149
  case Left(v) => Left(f1(v))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   150
}
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   151
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
520
2849c305b12d updated
cu
parents: 450
diff changeset
   152
  case Sequ(v1, v2) => Sequ(f1(v1), f2(v2))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   153
}
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   154
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
520
2849c305b12d updated
cu
parents: 450
diff changeset
   155
  (v:Val) => Sequ(f1(Empty), f2(v))
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   156
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
520
2849c305b12d updated
cu
parents: 450
diff changeset
   157
  (v:Val) => Sequ(f1(v), f2(Empty))
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   158
def F_RECD(f: Val => Val) = (v:Val) => v match {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   159
  case Rec(x, v) => Rec(x, f(v))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   160
}
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   161
def F_ERROR(v: Val): Val = throw new Exception("error")
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   162
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
   163
// simplification of regular expressions returns now also 
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
   164
// an rectification function; no simplification under STAR 
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   165
def simp(r: Rexp): (Rexp, Val => Val) = r match {
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   166
  case ALT(r1, r2) => {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   167
    val (r1s, f1s) = simp(r1)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   168
    val (r2s, f2s) = simp(r2)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   169
    (r1s, r2s) match {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   170
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   171
      case (_, ZERO) => (r1s, F_LEFT(f1s))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   172
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   173
                else (ALT (r1s, r2s), F_ALT(f1s, f2s)) 
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   174
    }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   175
  }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   176
  case SEQ(r1, r2) => {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   177
    val (r1s, f1s) = simp(r1)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   178
    val (r2s, f2s) = simp(r2)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   179
    (r1s, r2s) match {
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   180
      case (ZERO, _) => (ZERO, F_ERROR)
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   181
      case (_, ZERO) => (ZERO, F_ERROR)
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   182
      case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s))
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   183
      case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   184
      case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s))
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   185
    }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   186
  }
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   187
  case RECD(x, r1) => {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   188
    val (r1s, f1s) = simp(r1)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   189
    (RECD(x, r1s), F_RECD(f1s))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   190
  }
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   191
  case r => (r, F_ID)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   192
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   193
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
   194
// lexing functions including simplification
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   195
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   196
  case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   197
  case c::cs => {
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   198
    val (r_simp, f_simp) = simp(der(c, r))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   199
    inj(r, c, f_simp(lex_simp(r_simp, cs)))
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   200
  }
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   201
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   202
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   203
def lexing_simp(r: Rexp, s: String) : Val = lex_simp(r, s.toList)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   204
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   205
lexing_simp(("a" | "ab") ~ ("b" | ""), "ab")
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   206
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
   207
// The Lexing Rules for a Small While Language
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   208
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   209
def PLUS(r: Rexp) = r ~ r.%
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   210
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   211
val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   212
val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   213
val ID = SYM ~ (SYM | DIGIT).% 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   214
val NUM = PLUS(DIGIT)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   215
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false"
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   216
val SEMI: Rexp = ";"
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   217
val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   218
val WHITESPACE = PLUS(" " | "\n" | "\t")
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   219
val RPAREN: Rexp = ")"
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   220
val LPAREN: Rexp = "("
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   221
val BEGIN: Rexp = "{"
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   222
val END: Rexp = "}"
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   223
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   224
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   225
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   226
val WHILE_REGS = (("k" $ KEYWORD) | 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   227
                  ("i" $ ID) | 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   228
                  ("o" $ OP) | 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   229
                  ("n" $ NUM) | 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   230
                  ("s" $ SEMI) | 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   231
                  ("str" $ STRING) |
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   232
                  ("p" $ (LPAREN | RPAREN)) | 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   233
                  ("b" $ (BEGIN | END)) | 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   234
                  ("w" $ WHITESPACE)).%
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   235
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   236
//   Testing
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   237
//============
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   238
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   239
def time[T](code: => T) = {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   240
  val start = System.nanoTime()
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   241
  val result = code
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   242
  val end = System.nanoTime()
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   243
  println((end - start)/1.0e9)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   244
  result
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   245
}
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   246
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   247
val r1 = ("a" | "ab") ~ ("bcd" | "c")
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   248
println(lexing(r1, "abcd"))
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   249
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   250
val r2 = ("" | "a") ~ ("ab" | "b")
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   251
println(lexing(r2, "ab"))
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   252
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   253
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   254
// Two Simple While Tests
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   255
//========================
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   256
println("prog0 test")
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   257
581
19de761b69e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 580
diff changeset
   258
val prog0 = """read if"""
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   259
println(env(lexing_simp(WHILE_REGS, prog0)))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   260
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   261
println("prog1 test")
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   262
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   263
val prog1 = """read  n; write (n)"""
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   264
println(env(lexing_simp(WHILE_REGS, prog1)))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   265
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   266
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   267
// Bigger Test
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   268
//=============
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   269
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   270
val prog2 = """
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   271
write "fib";
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   272
read n;
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   273
minus1 := 0;
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   274
minus2 := 1;
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   275
while n > 0 do {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   276
  temp := minus2;
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   277
  minus2 := minus1 + minus2;
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   278
  minus1 := temp;
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   279
  n := n - 1
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   280
};
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   281
write "result";
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   282
write minus2
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   283
"""
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   284
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   285
println("Tokens")
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   286
println(env(lexing_simp(WHILE_REGS, prog2)))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   287
println(env(lexing_simp(WHILE_REGS, prog2)).filterNot{_._1 == "w"}.mkString("\n"))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   288
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   289
// some more timing tests with
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   290
// i copies of the program
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   291
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
   292
for (i <- 0 to 20 by 10) {
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   293
  print(i.toString + ":  ")
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   294
  time(lexing_simp(WHILE_REGS, prog2 * i))
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   295
}
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   296
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   297
617
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   298
val fib = """
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   299
write "Fib";
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   300
read n;
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   301
minus1 := 0;
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   302
minus2 := 1;
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   303
while n > 0 do {
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   304
temp := minus2;
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   305
minus2 := minus1 + minus2;
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   306
minus1 := temp;
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   307
n := n - 1
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   308
};
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   309
write "Result";
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   310
write minus2
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   311
"""
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   312
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   313
println(env(lexing_simp(WHILE_REGS, prog2)).filterNot{_._1 == "w"})