diff options
author | Martin Odersky <odersky@gmail.com> | 2007-09-16 18:01:02 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2007-09-16 18:01:02 +0000 |
commit | 7fe7bace8a7a55d888f4ebdce8d778ceb4384546 (patch) | |
tree | 3d1041a517a92f2a05f48ff80be116f316aadef1 /docs/examples/parsing/ArithmeticParsers.scala | |
parent | 0b2f65aa6c6b3bb3e7628720893fb8116fdc8f71 (diff) | |
download | scala-7fe7bace8a7a55d888f4ebdce8d778ceb4384546.tar.gz scala-7fe7bace8a7a55d888f4ebdce8d778ceb4384546.tar.bz2 scala-7fe7bace8a7a55d888f4ebdce8d778ceb4384546.zip |
combinator1 parser changes + examples added
Diffstat (limited to 'docs/examples/parsing/ArithmeticParsers.scala')
-rwxr-xr-x | docs/examples/parsing/ArithmeticParsers.scala | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/docs/examples/parsing/ArithmeticParsers.scala b/docs/examples/parsing/ArithmeticParsers.scala new file mode 100755 index 0000000000..8fb3af7acb --- /dev/null +++ b/docs/examples/parsing/ArithmeticParsers.scala @@ -0,0 +1,70 @@ +package examples.parsing + +import scala.util.parsing.combinator1.syntactical.StandardTokenParsers + +object ArithmeticParsers extends StandardTokenParsers { + lexical.delimiters ++= List("(", ")", "+", "-", "*", "/") + + def expr: Parser[Any] = term ~ rep("+" ~ term | "-" ~ term) + def term = factor ~ rep("*" ~ factor | "/" ~ factor) + def factor: Parser[Any] = "(" ~ expr ~ ")" | numericLit + + def main(args: Array[String]) { + val tokens = new lexical.Scanner(args(0)) + println(args(0)) + println(phrase(expr)(tokens)) + } +} + +object ArithmeticParsers1 extends StandardTokenParsers { + lexical.delimiters ++= List("(", ")", "+", "-", "*", "/") + + val reduceList: Int ~ List[String ~ Int] => Int = { + case i ~ ps => (i /: ps)(reduce) + } + + def reduce(x: Int, r: String ~ Int) = (r: @unchecked) match { + case "+" ~ y => x + y + case "-" ~ y => x - y + case "*" ~ y => x * y + case "/" ~ y => x / y + } + + def expr : Parser[Int] = term ~ rep ("+" ~ term | "-" ~ term) ^^ reduceList + def term : Parser[Int] = factor ~ rep ("*" ~ factor | "/" ~ factor) ^^ reduceList + def factor: Parser[Int] = "(" ~> expr <~ ")" | numericLit ^^ (_.toInt) + + def main(args: Array[String]) { + val tokens = new lexical.Scanner(args(0)) + println(args(0)) + println(phrase(expr)(tokens)) + } +} + +class Expr +case class BinOp(op: String, l: Expr, r: Expr) extends Expr +case class Num(n: Int) extends Expr + +object ArithmeticParsers2 extends StandardTokenParsers { + lexical.delimiters ++= List("(", ")", "+", "-", "*", "/") + + val reduceList: Expr ~ List[String ~ Expr] => Expr = { + case i ~ ps => (i /: ps)(reduce) + } + + def reduce(l: Expr, r: String ~ Expr) = BinOp(r._1, l, r._2) + def mkNum(s: String) = Num(s.toInt) + + def expr : Parser[Expr] = term ~ rep ("+" ~ term | "-" ~ term) ^^ reduceList + def term : Parser[Expr] = factor ~ rep ("*" ~ factor | "/" ~ factor) ^^ reduceList + def factor: Parser[Expr] = "(" ~> expr <~ ")" | numericLit ^^ ((s: String) => Num(s.toInt)) + + def main(args: Array[String]) { + val parse = phrase(expr) + val tokens = new lexical.Scanner(args(0)) + println(args(0)) + println(parse(tokens)) + } +} + + |