Scala RegexParser计算器示例右关联性

时间:2015-03-07 18:10:49

标签: scala parsing standard-library parser-combinators

RegexParsers特征的Javadoc包含以下示例:

object Calculator extends RegexParsers {
     def number: Parser[Double] = """\d+(\.\d*)?""".r ^^ { _.toDouble }
     def factor: Parser[Double] = number | "(" ~> expr <~ ")"
     def term  : Parser[Double] = factor ~ rep( "*" ~ factor | "/" ~ factor) ^^ {
       case number ~ list => (number /: list) {
         case (x, "*" ~ y) => x * y
         case (x, "/" ~ y) => x / y
       }
     }
     def expr  : Parser[Double] = term ~ rep("+" ~ log(term)("Plus term") | "-" ~ log(term)("Minus term")) ^^ {
       case number ~ list => list.foldLeft(number) { // same as before, using alternate name for /:
         case (x, "+" ~ y) => x + y
         case (x, "-" ~ y) => x - y
       }
     }

     def apply(input: String): Double = parseAll(expr, input) match {
       case Success(result, _) => result
       case failure : NoSuccess => scala.sys.error(failure.msg)
     }
   }

解析表达式5 - 4 - 2时,它将其视为(5 - 4) - 2并返回-1。如何将此解析器更改为右关联,以便它实际评估5 - (4 - 2)并返回3?

1 个答案:

答案 0 :(得分:0)

我设法使语法成为正确的关联:

trait Tree
case class Node(op: String, left: Tree, right: Tree) extends Tree
case class Leaf(value: BigInt) extends Tree

class ExpressionParser extends RegexParsers {
  var result : BigInt = 0

  def number: Parser[Tree] = """-?\d+""".r ^^ { s => Leaf(BigInt(s))}

  def expr : Parser[Tree] = (term ~ ("+" | "-") ~ expr ^^ {
    case ((x ~ op) ~ y) => Node(op, x, y)
  }) | term

  def term : Parser[Tree] = (factor ~ ("*" | "/") ~ term ^^ {
    case ((x ~ op) ~ y) => Node(op, x, y)
  }) | factor

  def factor : Parser[Tree] = number | "(" ~> expr <~ ")"

}