progs/scala/re-ext.scala
changeset 234 18d19d039ac9
parent 231 02fd56e2019e
child 235 f476c98cad28
equal deleted inserted replaced
233:654b542ce8db 234:18d19d039ac9
   155 def lex(r: Rexp, s: List[Char]) : Val = s match {
   155 def lex(r: Rexp, s: List[Char]) : Val = s match {
   156   case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
   156   case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
   157   case c::cs => inj(r, c, lex(der(c, r), cs))
   157   case c::cs => inj(r, c, lex(der(c, r), cs))
   158 }
   158 }
   159 
   159 
   160 def lexing(r: Rexp, s: String) : Val = lex(r, s.toList)
   160 import scala.util._
       
   161 def lexing(r: Rexp, s: String) : Try[Val] = Try(lex(r, s.toList))
   161 
   162 
   162 
   163 
   163 // some "rectification" functions for simplification
   164 // some "rectification" functions for simplification
   164 def F_ID(v: Val): Val = v
   165 def F_ID(v: Val): Val = v
   165 def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
   166 def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
   241 println(lexing_simp((("" | "a") ~ ("ab" | "b")), "ab"))
   242 println(lexing_simp((("" | "a") ~ ("ab" | "b")), "ab"))
   242 println(lexing_simp((("" | "a") ~ ("b" | "ab")), "ab"))
   243 println(lexing_simp((("" | "a") ~ ("b" | "ab")), "ab"))
   243 println(lexing_simp((("" | "a") ~ ("c" | "ab")), "ab"))
   244 println(lexing_simp((("" | "a") ~ ("c" | "ab")), "ab"))
   244 
   245 
   245 
   246 
       
   247 // Some more Tests
       
   248 //=================
       
   249 val ALL = CHARS((c) => true)
       
   250 val reg = ALL.% ~ "a" ~ NTIMES(ALL, 3) ~ "bc"
       
   251 
       
   252 println(lexing(reg, "axaybzbc"))     // true
       
   253 println(lexing(reg, "aaaaxaybzbc"))  // true
       
   254 println(nullable(ders("axaybzbd".toList, reg)))     // false
       
   255 println(lexing(reg, "axaybzbd"))     // false
       
   256 
       
   257 
       
   258 
   246 
   259 
   247 // Two Simple Tests for the While Language
   260 // Two Simple Tests for the While Language
   248 //========================================
   261 //========================================
   249 
   262 
   250 // Lexing Rules 
   263 // Lexing Rules