progs/scala/re.scala
changeset 13 62fe79ee2726
parent 3 94824659f6d7
child 34 33065bde3bbd
equal deleted inserted replaced
12:232ea10bed6f 13:62fe79ee2726
    13 
    13 
    14 abstract class Val
    14 abstract class Val
    15 case object Void extends Val
    15 case object Void extends Val
    16 case class Chr(c: Char) extends Val
    16 case class Chr(c: Char) extends Val
    17 case class Sequ(v1: Val, v2: Val) extends Val
    17 case class Sequ(v1: Val, v2: Val) extends Val
    18 case class Left(n: Int, v: Val) extends Val
    18 case class Left(v: Val) extends Val
    19 case class Right(n: Int, v: Val) extends Val
    19 case class Right(v: Val) extends Val
    20 case class Stars(vs: List[Val]) extends Val
    20 case class Stars(vs: List[Val]) extends Val
    21 case class Rec(x: String, v: Val) extends Val
    21 case class Rec(x: String, v: Val) extends Val
    22    
    22    
    23 // some convenience for typing in regular expressions
    23 // some convenience for typing in regular expressions
    24 def charlist2rexp(s : List[Char]): Rexp = s match {
    24 def charlist2rexp(s : List[Char]): Rexp = s match {
    87 
    87 
    88 // extracts a string from value
    88 // extracts a string from value
    89 def flatten(v: Val) : String = v match {
    89 def flatten(v: Val) : String = v match {
    90   case Void => ""
    90   case Void => ""
    91   case Chr(c) => c.toString
    91   case Chr(c) => c.toString
    92   case Left(n, v) => flatten(v)
    92   case Left(v) => flatten(v)
    93   case Right(n, v) => flatten(v)
    93   case Right(v) => flatten(v)
    94   case Sequ(v1, v2) => flatten(v1) + flatten(v2)
    94   case Sequ(v1, v2) => flatten(v1) + flatten(v2)
    95   case Stars(vs) => vs.map(flatten).mkString
    95   case Stars(vs) => vs.map(flatten).mkString
    96   case Rec(_, v) => flatten(v)
    96   case Rec(_, v) => flatten(v)
    97 }
    97 }
    98 
    98 
    99 // extracts an environment from a value
    99 // extracts an environment from a value
   100 def env(v: Val) : List[(String, String)] = v match {
   100 def env(v: Val) : List[(String, String)] = v match {
   101   case Void => Nil
   101   case Void => Nil
   102   case Chr(c) => Nil
   102   case Chr(c) => Nil
   103   case Left(n, v) => env(v)
   103   case Left(v) => env(v)
   104   case Right(n, v) => env(v)
   104   case Right(v) => env(v)
   105   case Sequ(v1, v2) => env(v1) ::: env(v2)
   105   case Sequ(v1, v2) => env(v1) ::: env(v2)
   106   case Stars(vs) => vs.flatMap(env)
   106   case Stars(vs) => vs.flatMap(env)
   107   case Rec(x, v) => (x, flatten(v))::env(v)
   107   case Rec(x, v) => (x, flatten(v))::env(v)
   108 }
   108 }
   109 
   109 
   110 def left_inc(v: Val) = v match {
       
   111   case Left(v, n) => Left(v, n + 1)
       
   112   case v => Left(v, 1)
       
   113 }
       
   114 
       
   115 def right_inc(v: Val) = v match {
       
   116   case Right(v, n) => Right(v, n + 1)
       
   117   case v => Right(v, 1)
       
   118 }
       
   119 
   110 
   120 def mkeps(r: Rexp) : Val = r match {
   111 def mkeps(r: Rexp) : Val = r match {
   121   case EMPTY => Void
   112   case EMPTY => Void
   122   case ALT(r1, r2) => 
   113   case ALT(r1, r2) => 
   123     if (nullable(r1)) left_inc(mkeps(r1)) else right_inc(mkeps(r2))
   114     if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
   124   case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
   115   case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
   125   case STAR(r) => Stars(Nil)
   116   case STAR(r) => Stars(Nil)
   126   case RECD(x, r) => Rec(x, mkeps(r))
   117   case RECD(x, r) => Rec(x, mkeps(r))
   127 }
   118 }
   128 
   119 
   129 def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
   120 def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match {
   130   case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
   121   case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs)
   131   case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
   122   case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
   132   case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
   123   case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
   133   case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
   124   case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
   134   case (ALT(r1, r2), Left(v1), 1) => Left(inj(r1, c, v1), 1)
   125   case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
   135   case (ALT(r1, r2), Left(v1), n) => inc_left(inj(r1, c, Left(v1, n - 1)))
       
   136   case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
   126   case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
   137   case (CHAR(d), Void) => Chr(d) 
   127   case (CHAR(d), Void) => Chr(d) 
   138   case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
   128   case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
   139 }
   129 }
   140 
   130 
   145   case c::cs => inj(r, c, lex(der(c, r), cs))
   135   case c::cs => inj(r, c, lex(der(c, r), cs))
   146 }
   136 }
   147 
   137 
   148 def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
   138 def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
   149 
   139 
       
   140 
       
   141 val r = (("1" $ "a") | (("2" $ "b") | ("3" $ "ab"))).% 
       
   142 env(lexing(r, "ba"))
       
   143 
       
   144 val r = "a" | "b"
       
   145 lexing(r,"a")
   150 
   146 
   151 // Lexing Rules for a Small While Language
   147 // Lexing Rules for a Small While Language
   152 
   148 
   153 def PLUS(r: Rexp) = r ~ r.%
   149 def PLUS(r: Rexp) = r ~ r.%
   154 val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
   150 val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"