main_templates3/re.scala
changeset 424 daf561a83ba6
parent 418 fa7f7144f2bb
child 428 cdfa6a293453
equal deleted inserted replaced
423:e9d14d58be3c 424:daf561a83ba6
    10 case class CHAR(c: Char) extends Rexp
    10 case class CHAR(c: Char) extends Rexp
    11 case class ALTs(rs: List[Rexp]) extends Rexp      // alternatives 
    11 case class ALTs(rs: List[Rexp]) extends Rexp      // alternatives 
    12 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence
    12 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence
    13 case class STAR(r: Rexp) extends Rexp             // star
    13 case class STAR(r: Rexp) extends Rexp             // star
    14 
    14 
    15 import scala.language.implicitConversions    
       
    16 import scala.language.reflectiveCalls 
       
    17 
       
    18 def charlist2rexp(s: List[Char]): Rexp = s match {
       
    19   case Nil => ONE
       
    20   case c::Nil => CHAR(c)
       
    21   case c::s => SEQ(CHAR(c), charlist2rexp(s))
       
    22 }
       
    23 implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
       
    24 
       
    25 
       
    26 // "a|b"
       
    27 ALT(CHAR('a'), CHAR('b'))
       
    28 
       
    29 val reg : Rexp = "a" | "b" // CHAR('a')
       
    30 
       
    31 // some convenience for typing regular expressions
       
    32 
       
    33 //the usual binary choice can be defined in terms of ALTs
    15 //the usual binary choice can be defined in terms of ALTs
    34 def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
    16 def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2))
    35 
    17 
       
    18 
       
    19 // some convenience for typing regular expressions
    36 
    20 
    37 import scala.language.implicitConversions    
    21 import scala.language.implicitConversions    
    38 import scala.language.reflectiveCalls 
    22 import scala.language.reflectiveCalls 
    39 
    23 
    40 def charlist2rexp(s: List[Char]): Rexp = s match {
    24 def charlist2rexp(s: List[Char]): Rexp = s match {
    41   case Nil => ONE
    25   case Nil => ONE
    42   case c::Nil => CHAR(c)
    26   case c::Nil => CHAR(c)
    43   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    27   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    44 }
    28 }
    45 implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
    29 implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
    46 
       
    47 
       
    48 
       
    49 
    30 
    50 implicit def RexpOps (r: Rexp) = new {
    31 implicit def RexpOps (r: Rexp) = new {
    51   def | (s: Rexp) = ALT(r, s)
    32   def | (s: Rexp) = ALT(r, s)
    52   def % = STAR(r)
    33   def % = STAR(r)
    53   def ~ (s: Rexp) = SEQ(r, s)
    34   def ~ (s: Rexp) = SEQ(r, s)
    58   def | (r: String) = ALT(s, r)
    39   def | (r: String) = ALT(s, r)
    59   def % = STAR(s)
    40   def % = STAR(s)
    60   def ~ (r: Rexp) = SEQ(s, r)
    41   def ~ (r: Rexp) = SEQ(s, r)
    61   def ~ (r: String) = SEQ(s, r)
    42   def ~ (r: String) = SEQ(s, r)
    62 }
    43 }
       
    44 
       
    45 
       
    46 // ALT(CHAR('a'), CHAR('b'))
       
    47 // val reg : Rexp = "a" | "b" 
       
    48 
    63 
    49 
    64 // (1) Complete the function nullable according to
    50 // (1) Complete the function nullable according to
    65 // the definition given in the coursework; this 
    51 // the definition given in the coursework; this 
    66 // function checks whether a regular expression
    52 // function checks whether a regular expression
    67 // can match the empty string and Returns a boolean
    53 // can match the empty string and Returns a boolean