diff -r 82a99eec5b73 -r 6e2cef17a9b3 exps/both.scala --- a/exps/both.scala Mon Feb 04 13:10:26 2019 +0000 +++ b/exps/both.scala Thu Feb 07 10:52:41 2019 +0000 @@ -31,7 +31,7 @@ abstract class Rexp case object ZERO extends Rexp case object ONE extends Rexp -case class PRED(f: Char => Boolean) extends Rexp +case class PRED(f: Char => Boolean, s: String = "_") extends Rexp case class ALTS(rs: List[Rexp]) extends Rexp case class SEQ(r1: Rexp, r2: Rexp) extends Rexp case class STAR(r: Rexp) extends Rexp @@ -39,7 +39,7 @@ // abbreviations -def CHAR(c: Char) = PRED(_ == c) +def CHAR(c: Char) = PRED(_ == c, c.toString) def ALT(r1: Rexp, r2: Rexp) = ALTS(List(r1, r2)) def PLUS(r: Rexp) = SEQ(r, STAR(r)) @@ -47,7 +47,7 @@ abstract class ARexp case object AZERO extends ARexp case class AONE(bs: Bits) extends ARexp -case class APRED(bs: Bits, f: Char => Boolean) extends ARexp +case class APRED(bs: Bits, f: Char => Boolean, s: String = "_") extends ARexp case class AALTS(bs: Bits, rs: List[ARexp]) extends ARexp case class ASEQ(bs: Bits, r1: ARexp, r2: ARexp) extends ARexp case class ASTAR(bs: Bits, r: ARexp) extends ARexp @@ -92,15 +92,27 @@ // string of a regular expressions - for testing purposes - def string(r: Rexp): String = r match { - case ZERO => "0" - case ONE => "1" - case PRED(_) => "_" - case ALTS(rs) => rs.map(string).mkString("[", "|", "]") - case SEQ(r1, r2) => s"(${string(r1)} ~ ${string(r2)})" - case STAR(r) => s"{${string(r)}}*" - case RECD(x, r) => s"(${x}! ${string(r)})" - } +def string(r: Rexp): String = r match { + case ZERO => "0" + case ONE => "1" + case PRED(_, s) => s + case ALTS(rs) => rs.map(string).mkString("[", "|", "]") + case SEQ(r1, r2) => s"(${string(r1)} ~ ${string(r2)})" + case STAR(r) => s"{${string(r)}}*" + case RECD(x, r) => s"(${x}! ${string(r)})" +} + +// string of an annotated regular expressions - for testing purposes + +def astring(a: ARexp): String = a match { + case AZERO => "0" + case AONE(_) => "1" + case APRED(_, _, s) => s + case AALTS(_, rs) => rs.map(astring).mkString("[", "|", "]") + case ASEQ(_, r1, r2) => s"(${astring(r1)} ~ ${astring(r2)})" + case ASTAR(_, r) => s"{${astring(r)}}*" +} + //-------------------------------------------------------------------------------------------------------- // START OF NON-BITCODE PART @@ -111,7 +123,7 @@ def nullable (r: Rexp) : Boolean = r match { case ZERO => false case ONE => true - case PRED(_) => false + case PRED(_, _) => false case ALTS(rs) => rs.exists(nullable) case SEQ(r1, r2) => nullable(r1) && nullable(r2) case STAR(_) => true @@ -122,7 +134,7 @@ def der (c: Char, r: Rexp) : Rexp = r match { case ZERO => ZERO case ONE => ZERO - case PRED(f) => if (f(c)) ONE else ZERO + case PRED(f, _) => if (f(c)) ONE else ZERO case ALTS(List(r1, r2)) => ALTS(List(der(c, r1), der(c, r2))) case SEQ(r1, r2) => if (nullable(r1)) ALTS(List(SEQ(der(c, r1), r2), der(c, r2))) @@ -171,7 +183,7 @@ case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) case (ALTS(List(r1, r2)), Left(v1)) => Left(inj(r1, c, v1)) case (ALTS(List(r1, r2)), Right(v2)) => Right(inj(r2, c, v2)) - case (PRED(_), Empty) => Chr(c) + case (PRED(_, _), Empty) => Chr(c) case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) } @@ -264,7 +276,7 @@ def fuse(bs: Bits, r: ARexp) : ARexp = r match { case AZERO => AZERO case AONE(cs) => AONE(bs ++ cs) - case APRED(cs, f) => APRED(bs ++ cs, f) + case APRED(cs, f, s) => APRED(bs ++ cs, f, s) case AALTS(cs, rs) => AALTS(bs ++ cs, rs) case ASEQ(cs, r1, r2) => ASEQ(bs ++ cs, r1, r2) case ASTAR(cs, r) => ASTAR(bs ++ cs, r) @@ -274,7 +286,7 @@ def internalise(r: Rexp) : ARexp = r match { case ZERO => AZERO case ONE => AONE(Nil) - case PRED(f) => APRED(Nil, f) + case PRED(f, s) => APRED(Nil, f, s) case ALTS(List(r1, r2)) => AALTS(Nil, List(fuse(List(Z), internalise(r1)), fuse(List(S), internalise(r2)))) case ALTS(r1::rs) => { @@ -291,7 +303,7 @@ // decoding of values from bit sequences def decode_aux(r: Rexp, bs: Bits) : (Val, Bits) = (r, bs) match { case (ONE, bs) => (Empty, bs) - case (PRED(f), C(c)::bs) => (Chr(c), bs) + case (PRED(f, _), C(c)::bs) => (Chr(c), bs) case (ALTS(r::Nil), bs) => decode_aux(r, bs) case (ALTS(rs), bs) => bs match { case Z::bs1 => { @@ -330,7 +342,7 @@ def erase(r: ARexp) : Rexp = r match{ case AZERO => ZERO case AONE(_) => ONE - case APRED(bs, f) => PRED(f) + case APRED(bs, f, s) => PRED(f, s) case AALTS(bs, rs) => ALTS(rs.map(erase(_))) case ASEQ(bs, r1, r2) => SEQ (erase(r1), erase(r2)) case ASTAR(cs, r)=> STAR(erase(r)) @@ -342,7 +354,7 @@ def bnullable (r: ARexp) : Boolean = r match { case AZERO => false case AONE(_) => true - case APRED(_,_) => false + case APRED(_,_,_) => false case AALTS(_, rs) => rs.exists(bnullable) case ASEQ(_, r1, r2) => bnullable(r1) && bnullable(r2) case ASTAR(_, _) => true @@ -362,7 +374,7 @@ def bder(c: Char, r: ARexp) : ARexp = r match { case AZERO => AZERO case AONE(_) => AZERO - case APRED(bs, f) => if (f(c)) AONE(bs:::List(C(c))) else AZERO + case APRED(bs, f, _) => if (f(c)) AONE(bs:::List(C(c))) else AZERO case AALTS(bs, rs) => AALTS(bs, rs.map(bder(c, _))) case ASEQ(bs, r1, r2) => if (bnullable(r1)) AALT(bs, ASEQ(Nil, bder(c, r1), r2), fuse(bmkeps(r1), bder(c, r2))) @@ -378,7 +390,6 @@ case c::s => bders(s, bder(c, r)) } - def flats(rs: List[ARexp]): List[ARexp] = rs match { case Nil => Nil case AZERO :: rs1 => flats(rs1) @@ -404,6 +415,7 @@ + def bders_simp (s: List[Char], r: ARexp) : ARexp = s match { case Nil => r case c::s => bders_simp(s, bsimp(bder(c, r))) @@ -485,7 +497,7 @@ def size(r: Rexp) : Int = r match { case ZERO => 1 case ONE => 1 - case PRED(_) => 1 + case PRED(_,_) => 1 case SEQ(r1, r2) => 1 + size(r1) + size(r2) case ALTS(rs) => 1 + rs.map(size).sum case STAR(r) => 1 + size(r) @@ -498,9 +510,9 @@ // Lexing Rules for a Small While Language //symbols -val SYM = PRED("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".contains(_)) +val SYM = PRED("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".contains(_), "SYM") //digits -val DIGIT = PRED("0123456789".contains(_)) +val DIGIT = PRED("0123456789".contains(_), "NUM") //identifiers val ID = SYM ~ (SYM | DIGIT).% //numbers @@ -578,13 +590,14 @@ println(" Bit full simp: " + time(btokenise_simp_full(WHILE_REGS, fib_prog * i))) } + println("Original " + size(WHILE_REGS)) -println("Size Bit " + asize(bders_simp((fib_prog * 10).toList, internalise(WHILE_REGS)))) -println("Size Bitf " + asize(bders_simp_full((fib_prog * 10).toList, internalise(WHILE_REGS)))) -println("Size Old " + size(ders_simp((fib_prog * 10).toList, WHILE_REGS))) +println("Size Bit " + asize(bders_simp((fib_prog * 1).toList, internalise(WHILE_REGS)))) +println("Size Bitf " + asize(bders_simp_full((fib_prog * 1).toList, internalise(WHILE_REGS)))) +println("Size Old " + size(ders_simp((fib_prog * 1).toList, WHILE_REGS))) -//System.exit(0) +System.exit(0) println("Internal sizes test OK or strange") @@ -662,3 +675,4 @@ +