progs/scala/re-annotated.sc
changeset 361 8bb064045b4e
parent 360 e752d84225ec
equal deleted inserted replaced
360:e752d84225ec 361:8bb064045b4e
    48 case class Sequ(v1: Val, v2: Val) extends Val
    48 case class Sequ(v1: Val, v2: Val) extends Val
    49 case class Left(v: Val) extends Val
    49 case class Left(v: Val) extends Val
    50 case class Right(v: Val) extends Val
    50 case class Right(v: Val) extends Val
    51 case class Stars(vs: List[Val]) extends Val
    51 case class Stars(vs: List[Val]) extends Val
    52 case class Recd(x: String, v: Val) extends Val
    52 case class Recd(x: String, v: Val) extends Val
       
    53 
       
    54 abstract class BTree
       
    55 case object LZ extends BTree
       
    56 case class L(bs: Bits) extends BTree
       
    57 case class LALTS(bs: Bits, bts: List[BTree]) extends BTree 
       
    58 case class LSEQ(bs: Bits, bt1: BTree, bt2: BTree) extends BTree 
       
    59 case class LSTAR(bs: Bits, bt: BTree) extends BTree
       
    60 
       
    61 def ext(r: ARexp): BTree = r match {
       
    62   case AZERO => LZ
       
    63   case AONE(bs) => L(bs)
       
    64   case ACHAR(bs, _) => L(bs)
       
    65   case AALTS(bs, rs) => LALTS(bs, rs.map(ext))
       
    66   case ASEQ(bs, r1, r2) => LSEQ(bs, ext(r1), ext(r2))
       
    67   case ASTAR(bs, r) => LSTAR(bs, ext(r))
       
    68 }
       
    69 
       
    70 // annotated regular expressions
       
    71 abstract class BRexp 
       
    72 case object BZERO extends BRexp
       
    73 case object BONE extends BRexp
       
    74 case class BCHAR(c: Char) extends BRexp
       
    75 case class BALTS(rs: List[BRexp]) extends BRexp 
       
    76 case class BSEQ(r1: BRexp, r2: BRexp) extends BRexp 
       
    77 case class BSTAR(r: BRexp) extends BRexp 
       
    78 
       
    79 def ex(r: ARexp): BRexp = r match {
       
    80   case AZERO => BZERO
       
    81   case AONE(_) => BONE
       
    82   case ACHAR(_, c) => BCHAR(c)
       
    83   case AALTS(_, rs) => BALTS(rs.map(ex))
       
    84   case ASEQ(_, r1, r2) => BSEQ(ex(r1), ex(r2))
       
    85   case ASTAR(_, r) => BSTAR(ex(r))
       
    86 }
       
    87 
    53    
    88    
    54 // some convenience for typing in regular expressions
    89 // some convenience for typing in regular expressions
    55 def charlist2rexp(s: List[Char]): Rexp = s match {
    90 def charlist2rexp(s: List[Char]): Rexp = s match {
    56   case Nil => ONE
    91   case Nil => ONE
    57   case c::Nil => CHAR(c)
    92   case c::Nil => CHAR(c)
   232   vdecode(r, blex(internalise(r), s.toList))
   267   vdecode(r, blex(internalise(r), s.toList))
   233 
   268 
   234 
   269 
   235 // example by Tudor
   270 // example by Tudor
   236 //val reg = (STAR("a") ~ ("b" | "c")).%
   271 //val reg = (STAR("a") ~ ("b" | "c")).%
   237 
       
   238 //println(blexing(reg, "aab"))
   272 //println(blexing(reg, "aab"))
   239 
   273 
   240 
   274 
   241 //=======================
   275 //=======================
   242 // simplification 
   276 // simplification