zeroable by Chunhan
authorChristian Urban <urbanc@in.tum.de>
Wed, 10 Oct 2012 14:08:49 +0100
changeset 28 f63ba92a7d78
parent 27 0f05e90b960d
child 29 774007c4b1b3
zeroable by Chunhan
regexp2.scala
regexp3.scala
--- 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))
+
--- 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))