Here are the errors我在sbt run
的此商家信息[3.5]上运行[1]
后收到:
import util.parsing.combinator.JavaTokenParsers
trait ArithParser extends JavaTokenParsers {
type T
def expr: Parser[T] = chainl1(term, "+" ^^^ Add | "-" ^^^ Sub)
def term = chainl1(factor, "*" ^^^ Mul | "/" ^^^ Div)
def factor = floatingPointNumber ^^ Num | "(" ~> expr <~ ")"
def Add = (T,T) => T
def Sub = (T,T) => T
def Mul = (T,T) => T
def Div = (T,T) => T
def Num = String => T
}
trait DirectEvaluation {
type T = Double
val Add = (_: Double) + (_:Double)
val Sub = (_: Double) - (_:Double)
val Mul = (_: Double) * (_:Double)
val Div = (_: Double) / (_:Double)
val Num = (_: String).toDouble
}
trait ASTBuilding {
type T = Expr
sealed abstract class Expr
case class Add(e1: Expr, e2: Expr) extends Expr
case class Sub(e1: Expr, e2: Expr) extends Expr
case class Mul(e1: Expr, e2: Expr) extends Expr
case class Div(e1: Expr, e2: Expr) extends Expr
case class Num(e: String) extends Expr
}
object Interpreter extends ArithParser with DirectEvaluation
object Compiler extends ArithParser with ASTBuilding
object Main extends ArithParser {
def main(args: Array[String]) {
val defArgs = if (args.isEmpty) Array("10.5 - 4*2") else args
val parser: ArithParser = if(defArgs.head === "eval") {
println("Interpreter!"); Interpreter
} else {
println("Compiler!"); Compiler
}
defArgs.tail foreach {arg =>
println("Input: " + arg)
println("Output: " + parseAll(parser.expr, arg))
}
}
}
[1]
E. Labun, “Combinator Parsing in Scala,” Technische Hochschule Mittelhessen, 2012.
如何修复这些错误?
答案 0 :(得分:1)
(T,T) => T
是一种类型注释,因此您必须将=
替换为:
。
def Add: (T,T) => T
这也正如你所提到的论文中的那样。
列表中不存在===
,它只是==
。
你必须在解析器上调用parseAll
:
parser.parseAll(...)
在我看来,您缺少scala编程的基础知识,否则您可以自己轻松修复这些错误。我的建议是,在开始使用高级主题之前,先阅读一本好的scala书,以便对该语言有一个基本的了解。