1 // A parser and evaluator for teh while language |
1 // A parser and evaluator for the WHILE language |
2 // |
2 // |
3 //:load matcher.scala |
3 import matcher._ |
4 //:load parser3.scala |
4 import parser._ |
|
5 |
5 |
6 |
6 // some regular expressions |
7 // some regular expressions |
7 val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_") |
8 val SYM = RANGE("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz_") |
8 val DIGIT = RANGE("0123456789") |
9 val DIGIT = RANGE("0123456789") |
9 val ID = SEQ(SYM, STAR(ALT(SYM, DIGIT))) |
10 val ID = SEQ(SYM, STAR(ALT(SYM, DIGIT))) |
30 case class T_ID(s: String) extends Token |
31 case class T_ID(s: String) extends Token |
31 case class T_OP(s: String) extends Token |
32 case class T_OP(s: String) extends Token |
32 case class T_NUM(s: String) extends Token |
33 case class T_NUM(s: String) extends Token |
33 case class T_KWD(s: String) extends Token |
34 case class T_KWD(s: String) extends Token |
34 |
35 |
35 val lexing_rules: List[Rule[Token]] = |
36 val lexing_rules: List[(Rexp, List[Char] => Token)] = |
36 List((KEYWORD, (s) => T_KWD(s.mkString)), |
37 List((KEYWORD, (s) => T_KWD(s.mkString)), |
37 (ID, (s) => T_ID(s.mkString)), |
38 (ID, (s) => T_ID(s.mkString)), |
38 (OP, (s) => T_OP(s.mkString)), |
39 (OP, (s) => T_OP(s.mkString)), |
39 (NUM, (s) => T_NUM(s.mkString)), |
40 (NUM, (s) => T_NUM(s.mkString)), |
40 (SEMI, (s) => T_SEMI), |
41 (SEMI, (s) => T_SEMI), |