diff -r 3b8e3a156200 -r 20a57552d722 exps/both.scala --- a/exps/both.scala Sat Feb 23 21:52:06 2019 +0000 +++ b/exps/both.scala Wed Mar 13 10:36:29 2019 +0000 @@ -4,6 +4,7 @@ import scala.annotation.tailrec import scala.util.Try +// for escaping strings def escape(raw: String) : String = { import scala.reflect.runtime.universe._ Literal(Constant(raw)).toString @@ -121,7 +122,7 @@ // nullable function: tests whether the regular // expression can recognise the empty string -def nullable (r: Rexp) : Boolean = r match { +def nullable(r: Rexp) : Boolean = r match { case ZERO => false case ONE => true case PRED(_, _) => false @@ -132,7 +133,7 @@ } // derivative of a regular expression w.r.t. a character -def der (c: Char, r: Rexp) : Rexp = r match { +def der(c: Char, r: Rexp) : Rexp = r match { case ZERO => ZERO case ONE => ZERO case PRED(f, _) => if (f(c)) ONE else ZERO @@ -236,9 +237,9 @@ val (r2s, f2s) = simp(r2) (r1s, r2s) match { case (ZERO, _) => (ZERO, F_ERROR) - case (_, ZERO) => (ZERO, F_ERROR) + //case (_, ZERO) => (ZERO, F_ERROR) case (ONE, _) => (r2s, F_SEQ_Empty1(f1s, f2s)) - case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s)) + //case (_, ONE) => (r1s, F_SEQ_Empty2(f1s, f2s)) case _ => (SEQ(r1s,r2s), F_SEQ(f1s, f2s)) } } @@ -304,6 +305,20 @@ //-------------------------------------------------------------------- // BITCODED PART +def retrieve(r: ARexp, v: Val) : List[Boolean] = (r, v) match { + case (AONE(bs), Empty) => bs + case (ACHAR(bs, c), Chr(d)) => bs + case (AALTS(bs, r::Nil), v) => bs ++ retrieve(r, v) + case (AALTS(bs, r::rs), Left(v)) => bs ++ retrieve(r, v) + case (AALTS(bs, r::rs), Right(v)) => bs ++ retrieve(AALTS(Nil, rs), v) + case (ASEQ(bs, r1, r2), Sequ(v1, v2)) => + bs ++ retrieve(r1, v1) ++ retrieve(r2, v2) + case (ASTAR(bs, r), Stars(Nil)) => bs ++ List(S) + case (ASTAR(bs, r), Stars(v::vs)) => + bs ++ List(Z) ++ retrieve(r, v) ++ retrieve(ASTAR(Nil, r), Stars(vs)) +} + + def fuse(bs: Bits, r: ARexp) : ARexp = r match { case AZERO => AZERO @@ -464,6 +479,41 @@ def btokenise_simp(r: Rexp, s: String) = env(blexing_simp(r, s)).map(esc2) +// Quick example + +val r : Rexp = ZERO | "a" + +lexing(r, "a") + +val a0 = internalise(r) +val a1 = bder('a', a0) +val a1s = bsimp(bder('a', a0)) + +val a2 = bmkeps(a1) +val a2s = bmkeps(a1s) + +val v = decode(r, a2) +val vs = decode(r, a2s) + + + +val Rr : Rexp = ONE ~ "a" + +lexing(Rr, "a") + +val Ra0 = internalise(Rr) +astring(Ra0) +val Ra1 = bder('a', Ra0) +astring(Ra1) +val Ra1s = bsimp(bder('a', Ra0)) +astring(Ra1s) + +val Ra2 = bmkeps(Ra1) +val Ra2s = bmkeps(Ra1s) + +val Rv = decode(Rr, Ra2) +val Rvs = decode(Rr, Ra2s) + // INCLUDING SIMPLIFICATION UNDER STARS @@ -530,6 +580,11 @@ env(blexing2_simp(r, s)).map(esc2) + + + + +//============================================ // Parser for regexes case class Parser(s: String) { @@ -594,7 +649,7 @@ -System.exit(0) +//System.exit(0) // Testing //============