solutions/cw3/lexer.sc
changeset 920 7af2eea19646
parent 919 53f08d873e09
child 959 64ec1884d860
equal deleted inserted replaced
919:53f08d873e09 920:7af2eea19646
     1 import scala.language.implicitConversions    
     1 // Lexer from CW2
     2 import scala.language.reflectiveCalls
     2 //================
       
     3 
     3 
     4 
     4 // Rexp
     5 // Rexp
     5 abstract class Rexp
     6 abstract class Rexp
     6 case object ZERO extends Rexp
     7 case object ZERO extends Rexp
     7 case object ONE extends Rexp
     8 case object ONE extends Rexp
    25 case class Right(v: Val) extends Val
    26 case class Right(v: Val) extends Val
    26 case class Stars(vs: List[Val]) extends Val
    27 case class Stars(vs: List[Val]) extends Val
    27 case class Rec(x: String, v: Val) extends Val
    28 case class Rec(x: String, v: Val) extends Val
    28 
    29 
    29 
    30 
    30 // Convenience typing
    31 // Convenience for typing
    31 def charlist2rexp(s : List[Char]): Rexp = s match {
    32 def charlist2rexp(s : List[Char]): Rexp = s match {
    32   case Nil => ONE
    33   case Nil => ONE
    33   case c::Nil => CHAR(c)
    34   case c::Nil => CHAR(c)
    34   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    35   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    35 }
    36 }
   134   case (CHAR(d), Empty) => Chr(c) 
   135   case (CHAR(d), Empty) => Chr(c) 
   135   case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
   136   case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
   136 
   137 
   137   case (RANGE(_), Empty) => Chr(c)
   138   case (RANGE(_), Empty) => Chr(c)
   138   case (PLUS(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
   139   case (PLUS(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
   139   case (OPTIONAL(r), Left(v1)) => Stars(List(inj(r, c, v1)))
   140   case (OPTIONAL(r), v1) => Stars(List(inj(r, c, v1)))
   140   case (NTIMES(r, n), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
   141   case (NTIMES(r, n), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
   141 }
   142 }
   142 
   143 
   143 // Rectification functions
   144 // Rectification functions
   144 def F_ID(v: Val): Val = v
   145 def F_ID(v: Val): Val = v