我正在使用log
解析器实用程序来跟踪解析。
斯卡拉代码:
import scala.util.parsing.combinator.JavaTokenParsers
object Arith extends JavaTokenParsers with App {
def expr: Parser[Any] = log(term~rep("+"~term | "-"~term))("expr")
def term: Parser[Any] = factor~rep("*"~factor | "/"~factor)
def factor: Parser[Any] = floatingPointNumber | "("~expr~")"
println(parseAll(expr, "2 * (3 + 7)"))
}
输出:
trying expr at scala.util.parsing.input.CharSequenceReader@13a317a
trying expr at scala.util.parsing.input.CharSequenceReader@14c1103
expr --> [1.11] parsed: ((3~List())~List((+~(7~List()))))
expr --> [1.12] parsed: ((2~List((*~(((~((3~List())~List((+~(7~List())))))~)))))~List())
[1.12] parsed: ((2~List((*~(((~((3~List())~List((+~(7~List())))))~)))))~List())
输入打印为scala.util.parsing.input.CharSequenceReader@13a317a
。有没有办法打印输入的字符串表示形式,如“2 * (3 + 7)”?