regexp2.scala
changeset 28 f63ba92a7d78
parent 26 06be91bbb1cd
child 29 774007c4b1b3
--- a/regexp2.scala	Wed Oct 10 08:50:07 2012 +0100
+++ b/regexp2.scala	Wed Oct 10 14:08:49 2012 +0100
@@ -38,7 +38,7 @@
   case EMPTY => false
   case CHAR(_) => false
   case ALT(r1, r2) => zeroable(r1) && zeroable(r2)
-  case SEQ(r1, r2) => zeroable(r1) || zeroable(r2)
+  case SEQ(r1, r2) => if (nullable(r1)) (zeroable(r1) && zeroable(r2)) else zeroable(r1)
   case STAR(_) => false
   case NOT(r) => !(zeroable(r))
 }
@@ -89,6 +89,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)
 
@@ -113,7 +114,7 @@
 println(matcher(COMMENT, "/* test */ test */"))
 
 // an example list of regular expressions
-val regs: List[Rexp]=  List("if", "then", "else", "+", IDENT, NUMBER, WHITESPACE) 
+val regs: List[Rexp]=  List("if", "then", "else", "+", IDENT, NUMBER, WHITESPACES) 
 
 def error (s: String) = throw new IllegalArgumentException ("Could not lex " + s)
 
@@ -137,7 +138,11 @@
   }
 }
 
-println(lex_all(rules, "if true then 42 else +".toList))
-println(lex_all(rules, "ifff if     34 34".toList))
-println(lex_all(rules, "ifff $ if 34".toList))
+val regs: List[Rexp]=  List("if", "then", "else", "+", IDENT, NUMBER, WHITESPACES) 
 
+println(lex_all(regs, "if true then 42 else +".toList))
+println(lex_all(regs, "ifff if     34 34".toList))
+println(lex_all(regs, "ifff +if+     34 34".toList))
+println(lex_all(regs, "1+x+3+4+foo".toList))
+println(lex_all(regs, "ifff $ if 34".toList))
+