solutions/cw4/lexer.sc
changeset 961 c0600f8b6427
parent 959 64ec1884d860
equal deleted inserted replaced
960:c7009356ddd8 961:c0600f8b6427
     1 // Lexer from CW2 with additions to the rules
     1 // Lexer from CW2 with additions to the rules
     2 //============================================
     2 //============================================
       
     3 
       
     4 import scala.language.implicitConversions
     3 
     5 
     4 // Rexp
     6 // Rexp
     5 abstract class Rexp
     7 abstract class Rexp
     6 case object ZERO extends Rexp
     8 case object ZERO extends Rexp
     7 case object ONE extends Rexp
     9 case object ONE extends Rexp
    32   case Nil => ONE
    34   case Nil => ONE
    33   case c::Nil => CHAR(c)
    35   case c::Nil => CHAR(c)
    34   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    36   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    35 }
    37 }
    36 
    38 
    37 implicit def string2rexp(s : String) : Rexp = 
    39 given Conversion[String, Rexp] = (s => charlist2rexp(s.toList))
    38   charlist2rexp(s.toList)
    40 //implicit def string2rexp(s : String) : Rexp = 
       
    41 //  charlist2rexp(s.toList)
    39 
    42 
    40 extension (r: Rexp) {
    43 extension (r: Rexp) {
    41   def | (s: Rexp) = ALT(r, s)
    44   def | (s: Rexp) = ALT(r, s)
    42   def % = STAR(r)
    45   def % = STAR(r)
    43   def ~ (s: Rexp) = SEQ(r, s)
    46   def ~ (s: Rexp) = SEQ(r, s)
    44 }
    47 }
    45 
    48 
    46 extension (s: String) {
    49 extension (s: String) {
    47   def | (r: Rexp) = ALT(s, r)
    50   def | (r: Rexp) = ALT(s, r)
    48   def | (r: String) = ALT(s, r)
    51   //def | (r: String) = ALT(s, r)
    49   def % = STAR(s)
    52   def % = STAR(s)
    50   def ~ (r: Rexp) = SEQ(s, r)
    53   def ~ (r: Rexp) = SEQ(s, r)
    51   def ~ (r: String) = SEQ(s, r)
    54   //def ~ (r: String) = SEQ(s, r)
    52   def $ (r: Rexp) = RECD(s, r)
    55   infix def $ (r: Rexp) = RECD(s, r)
    53 }
    56 }
    54 
    57 
    55 // nullable
    58 // nullable
    56 def nullable(r: Rexp) : Boolean = r match {
    59 def nullable(r: Rexp) : Boolean = r match {
    57   case ZERO => false
    60   case ZERO => false