下面的代码示例崩溃由于解析深度嵌套在括号表达式时堆栈溢出。
解析器组合是标准库的一部分。 是否有利用图书馆避免的一种方式?
(我不是要求的,为什么它崩溃,而对于处理标准库的正确方法的原因。)
解析:((((((((... 1 + 1 ...)))))))))
码:
import scala.util.parsing.combinator.syntactical.StandardTokenParsers
object ArithmeticParser1 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 s = scala.io.Source.fromFile(args(0)).mkString
val tokens = new lexical.Scanner(s)
println(s)
println(phrase(expr)(tokens))
}
}