progs/lexer.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 01 Oct 2019 23:40:25 +0100
changeset 642 064afa8fc1d9
parent 625 6709fa87410b
child 670 551d018cbbac
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
     1
// A simple lexer inspired by work of Sulzmann & Lu
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
     2
//==================================================
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
     3
581
19de761b69e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 580
diff changeset
     4
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
     5
import scala.language.implicitConversions    
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
import scala.language.reflectiveCalls
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
     8
// regular expressions including records
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
     9
abstract class Rexp 
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    10
case object ZERO extends Rexp
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    11
case object ONE extends Rexp
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
case class CHAR(c: Char) extends Rexp
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    13
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
    14
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
    15
case class STAR(r: Rexp) extends Rexp 
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    16
case class RECD(x: String, r: Rexp) extends Rexp
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    17
  
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    18
// values  
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    19
abstract class Val
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
    20
case object Empty extends Val
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    21
case class Chr(c: Char) extends Val
520
2849c305b12d updated
cu
parents: 450
diff changeset
    22
case class Sequ(v1: Val, v2: Val) extends Val
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    23
case class Left(v: Val) extends Val
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    24
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
    25
case class Stars(vs: List[Val]) extends Val
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    26
case class Rec(x: String, v: Val) extends Val
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    27
   
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    28
// some convenience for typing in regular expressions
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    29
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
    30
  case Nil => ONE
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
  case c::Nil => CHAR(c)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
  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
    33
}
581
19de761b69e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 580
diff changeset
    34
implicit def string2rexp(s : String) : Rexp = 
19de761b69e9 updated
Christian Urban <urbanc@in.tum.de>
parents: 580
diff changeset
    35
  charlist2rexp(s.toList)
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
implicit def RexpOps(r: Rexp) = new {
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  def | (s: Rexp) = ALT(r, s)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  def % = STAR(r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  def ~ (s: Rexp) = SEQ(r, s)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
implicit def stringOps(s: String) = new {
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
  def | (r: Rexp) = ALT(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  def | (r: String) = ALT(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  def % = STAR(s)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  def ~ (r: Rexp) = SEQ(s, r)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
  def ~ (r: String) = SEQ(s, r)
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    49
  def $ (r: Rexp) = RECD(s, r)
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    52
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
    53
  case ZERO => false
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    54
  case ONE => true
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
  case CHAR(_) => false
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    56
  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
    57
  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
    58
  case STAR(_) => true
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    59
  case RECD(_, r1) => nullable(r1)
164
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    62
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
    63
  case ZERO => ZERO
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    64
  case ONE => ZERO
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
    65
  case CHAR(d) => if (c == d) ONE else ZERO
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    66
  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
    67
  case SEQ(r1, r2) => 
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
    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
    69
    else SEQ(der(c, r1), r2)
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
  case STAR(r) => SEQ(der(c, r), STAR(r))
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    71
  case RECD(_, r1) => der(c, r1)
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    72
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    73
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
// extracts a string from value
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    76
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
    77
  case Empty => ""
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
    78
  case Chr(c) => c.toString
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    79
  case Left(v) => flatten(v)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    80
  case Right(v) => flatten(v)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    81
  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
    82
  case Stars(vs) => vs.map(flatten).mkString
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    83
  case Rec(_, v) => flatten(v)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    84
}
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    85
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    86
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
    87
// extracts an environment from a value;
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    88
// used for tokenise a string
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    89
def env(v: Val) : List[(String, String)] = v match {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    90
  case Empty => Nil
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    91
  case Chr(c) => Nil
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    92
  case Left(v) => env(v)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    93
  case Right(v) => env(v)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    94
  case Sequ(v1, v2) => env(v1) ::: env(v2)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    95
  case Stars(vs) => vs.flatMap(env)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    96
  case Rec(x, v) => (x, flatten(v))::env(v)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    97
}
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
    98
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
    99
// The Injection Part of the lexer
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
   100
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   101
def mkeps(r: Rexp) : Val = r match {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   102
  case ONE => Empty
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   103
  case ALT(r1, r2) => 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   104
    if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   105
  case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   106
  case STAR(r) => Stars(Nil)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   107
  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
   108
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   109
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   110
def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
520
2849c305b12d updated
cu
parents: 450
diff changeset
   111
  case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
2849c305b12d updated
cu
parents: 450
diff changeset
   112
  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
   113
  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
   114
  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
   115
  case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   116
  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
   117
  case (CHAR(d), Empty) => Chr(c) 
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   118
  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
   119
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   120
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   121
// some "rectification" functions for simplification
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   122
def F_ID(v: Val): Val = v
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   123
def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   124
def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   125
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
   126
  case Right(v) => Right(f2(v))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   127
  case Left(v) => Left(f1(v))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   128
}
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   129
def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match {
520
2849c305b12d updated
cu
parents: 450
diff changeset
   130
  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
   131
}
354
86b2aeae3e98 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 352
diff changeset
   132
def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = 
520
2849c305b12d updated
cu
parents: 450
diff changeset
   133
  (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
   134
def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = 
520
2849c305b12d updated
cu
parents: 450
diff changeset
   135
  (v:Val) => Sequ(f1(v), f2(Empty))
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   136
def F_RECD(f: Val => Val) = (v:Val) => v match {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   137
  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
   138
}
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   139
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
   140
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   141
def simp(r: Rexp): (Rexp, Val => Val) = r match {
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   142
  case ALT(r1, r2) => {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   143
    val (r1s, f1s) = simp(r1)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   144
    val (r2s, f2s) = simp(r2)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   145
    (r1s, r2s) match {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   146
      case (ZERO, _) => (r2s, F_RIGHT(f2s))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   147
      case (_, ZERO) => (r1s, F_LEFT(f1s))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   148
      case _ => if (r1s == r2s) (r1s, F_LEFT(f1s))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   149
                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
   150
    }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   151
  }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   152
  case SEQ(r1, r2) => {
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   153
    val (r1s, f1s) = simp(r1)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   154
    val (r2s, f2s) = simp(r2)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   155
    (r1s, r2s) match {
426
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   156
      case (ZERO, _) => (ZERO, F_ERROR)
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   157
      case (_, ZERO) => (ZERO, F_ERROR)
0debe6f41396 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 354
diff changeset
   158
      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
   159
      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
   160
      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
   161
    }
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   162
  }
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   163
  case RECD(x, r1) => {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   164
    val (r1s, f1s) = simp(r1)
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   165
    (RECD(x, r1s), F_RECD(f1s))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   166
  }
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   167
  case r => (r, F_ID)
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   168
}
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   169
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 617
diff changeset
   170
// lexing functions including simplification
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   171
def lex_simp(r: Rexp, s: List[Char]) : Val = s match {
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   172
  case Nil => if (nullable(r)) mkeps(r) else 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   173
    { throw new Exception("lexing error") } 
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   174
  case c::cs => {
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   175
    val (r_simp, f_simp) = simp(der(c, r))
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   176
    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
   177
  }
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   178
}
6c1d214c39ef added progs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   179
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   180
def lexing_simp(r: Rexp, s: String) = 
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   181
  env(lex_simp(r, s.toList))
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   182
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   183
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   184
// The Lexing Rules for the Fun Language
352
1e1b0fe66107 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 165
diff changeset
   185
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   186
def PLUS(r: Rexp) = r ~ r.%
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   187
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   188
def Range(s : List[Char]) : Rexp = s match {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   189
  case Nil => ZERO
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   190
  case c::Nil => CHAR(c)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   191
  case c::s => ALT(CHAR(c), Range(s))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   192
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   193
def RANGE(s: String) = Range(s.toList)
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   194
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   195
val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_")
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   196
val DIGIT = RANGE("0123456789")
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   197
val ID = SYM ~ (SYM | DIGIT).% 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   198
val NUM = PLUS(DIGIT)
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   199
val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" 
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   200
val SEMI: Rexp = ";"
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   201
val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">"
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   202
val WHITESPACE = PLUS(" " | "\n" | "\t")
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   203
val RPAREN: Rexp = "{"
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   204
val LPAREN: Rexp = "}"
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   205
val STRING: Rexp = "\"" ~ SYM.% ~ "\""
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   206
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   207
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   208
val WHILE_REGS = (("k" $ KEYWORD) | 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   209
                  ("i" $ ID) | 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   210
                  ("o" $ OP) | 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   211
                  ("n" $ NUM) | 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   212
                  ("s" $ SEMI) | 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   213
                  ("str" $ STRING) |
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   214
                  ("p" $ (LPAREN | RPAREN)) | 
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   215
                  ("w" $ WHITESPACE)).%
549
352d15782d35 updated
Christian Urban <urbanc@in.tum.de>
parents: 542
diff changeset
   216
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   217
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   218
// Two Simple While Tests
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   219
//========================
542
37a3db7cd655 updated
Christian Urban <urbanc@in.tum.de>
parents: 529
diff changeset
   220
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   221
println("test: read n")
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   222
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   223
val prog0 = """read n"""
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   224
println(lexing_simp(WHILE_REGS, prog0))
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   225
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   226
println("test: read  n; write n ")
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   227
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   228
val prog1 = """read  n; write n"""
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   229
println(lexing_simp(WHILE_REGS, prog1))
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   230
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   231
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   232
// Bigger Tests
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   233
//==============
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   234
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   235
// escapes strings and prints them out as "", "\n" and so on
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   236
def esc(raw: String): String = {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   237
  import scala.reflect.runtime.universe._
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   238
  Literal(Constant(raw)).toString
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   239
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   240
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   241
def escape(tks: List[(String, String)]) =
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   242
  tks.map{ case (s1, s2) => (s1, esc(s2))}
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   243
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   244
val prog2 = """
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   245
write "Fib";
579
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   246
read n;
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   247
minus1 := 0;
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   248
minus2 := 1;
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   249
while n > 0 do {
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   250
  temp := minus2;
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   251
  minus2 := minus1 + minus2;
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   252
  minus1 := temp;
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   253
  n := n - 1
1a521448d211 updated
Christian Urban <urbanc@in.tum.de>
parents: 552
diff changeset
   254
};
617
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   255
write "Result";
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   256
write minus2
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   257
"""
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 581
diff changeset
   258
642
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   259
println("lexing Fib")
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   260
println(escape(lexing_simp(WHILE_REGS, prog2)).mkString("\n"))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   261
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   262
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   263
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   264
val prog3 = """
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   265
start := 1000;
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   266
x := start;
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   267
y := start;
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   268
z := start;
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   269
while 0 < x do {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   270
 while 0 < y do {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   271
  while 0 < z do {
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   272
    z := z - 1
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   273
  };
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   274
  z := start;
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   275
  y := y - 1
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   276
 };     
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   277
 y := start;
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   278
 x := x - 1
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   279
}
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   280
"""
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   281
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   282
println("lexing Loops")
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   283
println(escape(lexing_simp(WHILE_REGS, prog3)).mkString("\n"))
064afa8fc1d9 updated
Christian Urban <urbanc@in.tum.de>
parents: 625
diff changeset
   284