diff options
author | Adriaan Moors <adriaan.moors@epfl.ch> | 2007-07-17 20:49:24 +0000 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@epfl.ch> | 2007-07-17 20:49:24 +0000 |
commit | c5c6d4a8ce75993d2dcd80f6360b45de534a3f41 (patch) | |
tree | a4502a0bf95b9bf8904936b0bdb8eb4cd34b558f /docs/examples/parsing/ArithmeticParser.scala | |
parent | 31b6dbf1c50b4b6fe3664691403e694de6a01513 (diff) | |
download | scala-c5c6d4a8ce75993d2dcd80f6360b45de534a3f41.tar.gz scala-c5c6d4a8ce75993d2dcd80f6360b45de534a3f41.tar.bz2 scala-c5c6d4a8ce75993d2dcd80f6360b45de534a3f41.zip |
updated examples to new combinator library
Diffstat (limited to 'docs/examples/parsing/ArithmeticParser.scala')
-rw-r--r-- | docs/examples/parsing/ArithmeticParser.scala | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/docs/examples/parsing/ArithmeticParser.scala b/docs/examples/parsing/ArithmeticParser.scala new file mode 100644 index 0000000000..2aac4b6f6c --- /dev/null +++ b/docs/examples/parsing/ArithmeticParser.scala @@ -0,0 +1,57 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package examples.parsing + +import scala.util.parsing.combinator.lexical.StdLexical +import scala.util.parsing.combinator.syntactical.StdTokenParsers + +/** Parse and evaluate a numeric expression as a sequence of terms, separated by + or - + * a term is a sequence of factors, separated by * or / + * a factor is a parenthesized expression or a number + * + * @author Adriaan Moors + */ +object arithmeticParser extends StdTokenParsers { + type Tokens = StdLexical ; val lexical = new StdLexical + lexical.delimiters ++= List("(", ")", "+", "-", "*", "/") + + lazy val expr = term*("+" ^^ {(x: int, y: int) => x + y} | "-" ^^ {(x: int, y: int) => x - y}) + lazy val term = factor*("*" ^^ {(x: int, y: int) => x * y} | "/" ^^ {(x: int, y: int) => x / y}) + lazy val factor: Parser[int] = "(" ~ expr ~ ")" | numericLit ^^ (_.toInt) + + def main(args: Array[String]) { + println( + if (args.length == 1) { + expr(new lexical.Scanner(args(0))) + } + else + "usage: scala examples.parsing.arithmeticParser <expr-string>" + ) + } +} + + +object arithmeticParserDesugared extends StdTokenParsers { + type Tokens = StdLexical ; val lexical = new StdLexical + lexical.delimiters ++= List("(", ")", "+", "-", "*", "/") + + lazy val expr = chainl1(term, (keyword("+").^^{(x: int, y: int) => x + y}).|(keyword("-").^^{(x: int, y: int) => x - y})) + lazy val term = chainl1(factor, (keyword("*").^^{(x: int, y: int) => x * y}).|(keyword("/").^^{(x: int, y: int) => x / y})) + lazy val factor: Parser[int] = keyword("(").~(expr.~(keyword(")"))).|(numericLit.^^(x => x.toInt)) + + def main(args: Array[String]) { + println( + if (args.length == 1) { + expr(new lexical.Scanner(args(0))) + } + else + "usage: scala examples.parsing.arithmeticParser <expr-string>" + ) + } +}
\ No newline at end of file |