diff -r e6868bd2942b -r 7717f20f0504 parser2a.scala --- a/parser2a.scala Wed Nov 21 09:04:11 2012 +0000 +++ b/parser2a.scala Fri Nov 23 14:08:31 2012 +0000 @@ -1,18 +1,22 @@ -:load matcher.scala +// Parser combinators including semantic actions +// parses lists of tokens +// +// Needs +// :load matcher.scala // some regular expressions -val LETTER = RANGE("abcdefghijklmnopqrstuvwxyz".toList) +val LETTER = RANGE("abcdefghijklmnopqrstuvwxyz") val ID = PLUS(LETTER) -val DIGIT = RANGE("0123456789".toList) -val NONZERODIGIT = RANGE("123456789".toList) +val DIGIT = RANGE("0123456789") +val NONZERODIGIT = RANGE("123456789") val NUMBER = ALT(SEQ(NONZERODIGIT, STAR(DIGIT)), "0") val LPAREN = CHAR('(') val RPAREN = CHAR(')') -val WHITESPACE = PLUS(RANGE(" \n".toList)) -val OPS = RANGE("+-*".toList) +val WHITESPACE = PLUS(RANGE(" \n")) +val OPS = RANGE("+-*") // for classifying the strings that have been recognised abstract class Token @@ -27,13 +31,6 @@ case object T_THEN extends Token case object T_ELSE extends Token -def tokenizer(rs: List[Rule[Token]], s: String) : List[Token] = - tokenize(rs, s.toList).filterNot(_ match { - case T_WHITESPACE => true - case _ => false - }) - - // lexing rules for arithmetic expressions val lexing_rules: List[Rule[Token]]= List(("if", (s) => T_IF), @@ -46,6 +43,7 @@ (RPAREN, (s) => T_RPAREN), (OPS, (s) => T_OP(s.mkString))) +val Tok = Tokenizer(lexing_rules, List(T_WHITESPACE)) // parser combinators with return type T abstract class Parser[T] { @@ -98,9 +96,10 @@ lazy val T: Parser[Int] = (F ~ T_OP("*") ~ T) ==> { case ((x, y), z) => x * z } || F lazy val F: Parser[Int] = (T_LPAREN ~> E <~ T_RPAREN) || NumParser -println(E.parse_all(tokenizer(lexing_rules, "1 + 2 + 3"))) -println(E.parse_all(tokenizer(lexing_rules, "1 + 2 * 3"))) -println(E.parse_all(tokenizer(lexing_rules, "(1 + 2) * 3"))) +println(E.parse_all(Tok.fromString("1 + 2 + 3"))) +println(E.parse_all(Tok.fromString("1 + 2 * 3"))) +println(E.parse_all(Tok.fromString("(1 + 2) * 3"))) -println(E.parse_all(tokenizer(lexing_rules, "(1 - 2) * 3"))) -println(E.parse_all(tokenizer(lexing_rules, "(1 + 2) * - 3"))) +// Excercise: implement minus +println(E.parse_all(Tok.fromString("(1 - 2) * 3"))) +println(E.parse_all(Tok.fromString("(1 + 2) * - 3")))