--- a/regexp3.scala Wed Oct 10 08:50:07 2012 +0100
+++ b/regexp3.scala Wed Oct 10 14:08:49 2012 +0100
@@ -26,7 +26,7 @@
case EMPTY => true
case CHAR(_) => false
case ALT(r1, r2) => nullable(r1) || nullable(r2)
- case SEQ(r1, r2) => nullable(r1) && nullable(r2)
+ case SEQ(r1, r2) => if (nullable(r1)) (zeroable(r1) && zeroable(r2)) else zeroable(r1)
case STAR(_) => true
case NOT(r) => !(nullable(r))
}
@@ -39,7 +39,7 @@
case CHAR(_) => false
case ALT(r1, r2) => zeroable(r1) && zeroable(r2)
case SEQ(r1, r2) => if (nullable(r1)) (zeroable(r1) && zeroable(r2)) else zeroable(r1)
- //zeroable(r1) || zeroable(r2)
+ //case SEQ(r1, r2) => zeroable(r1) || zeroable(r2)
case STAR(_) => false
case NOT(r) => !(zeroable(r))
}
@@ -90,6 +90,7 @@
val IDENT = SEQ(LETTER, STAR(ALT(LETTER,DIGITS)))
val NUMBER = ALT(SEQ(NONZERODIGITS, STAR(DIGITS)), "0")
val WHITESPACE = RANGE(" \n".toList)
+val WHITESPACES = PLUS(WHITESPACE)
val ALL = ALT(ALT(LETTER, DIGITS), WHITESPACE)
@@ -102,6 +103,7 @@
case class T_OP(s: String) extends Token
case class T_NUM(n: Int) extends Token
case class T_KEYWORD(s: String) extends Token
+case object T_COMMENT extends Token
// an example list of rules
@@ -114,7 +116,7 @@
("+", (s) => T_OP(s.mkString)),
(IDENT, (s) => T_IDENT(s.mkString)),
(NUMBER, (s) => T_NUM(s.mkString.toInt)),
- (WHITESPACE, (s) => T_WHITESPACE))
+ (WHITESPACES, (s) => T_WHITESPACE))
def error (s: String) = throw new IllegalArgumentException ("Could not lex " + s)
@@ -140,9 +142,34 @@
}
}
+val rules: List[Rule]=
+ List(("if", (s) => T_KEYWORD(s.mkString)),
+ ("then", (s) => T_KEYWORD(s.mkString)),
+ ("else", (s) => T_KEYWORD(s.mkString)),
+ ("+", (s) => T_OP(s.mkString)),
+ (IDENT, (s) => T_IDENT(s.mkString)),
+ (NUMBER, (s) => T_NUM(s.mkString.toInt)),
+ (WHITESPACES, (s) => T_WHITESPACE),
+ (COMMENT, (s) => T_COMMENT))
+
+println(lex_all(rules, "/*ifff if */ hhjj /*34 */".toList))
-println(lex_all(rules, "if true then 42 else +".toList))
+munch(COMMENT, (s) => T_COMMENT , "/*ifff if */ hhjj /*34 */".toList, Nil)
+val COMMENT2 = NOT(SEQ(SEQ(STAR(ALL), "*/"), STAR(ALL)))
+
+der('/', COMMENT)
+zeroable(der('/', COMMENT))
+zeroable(der('a', COMMENT2))
+
+matcher(COMMENT2, "ifff if 34")
+munch(COMMENT2, "ifff if 34".toList, Nil)
+starts_with(COMMENT2, 'i')
+lex_all(regs, "ifff if 34".toList)
+lex_all(regs, "ifff $ if 34".toList)
+
+println(lex_all(rules, "/* if true then */ then 42 else +".toList))
+println(lex_all(rules, "if true then then 42 else +".toList))
println(lex_all(rules, "ifff if 34 34".toList))
println(lex_all(rules, "ifff $ if 34".toList))