Reputation: 13846
Here are the errors I received when running sbt run
on this listing [3.5] from [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.
How do I fix these errors?
Upvotes: 0
Views: 92
Reputation: 24403
The (T,T) => T
is a type annotation, so you have to replace the =
with a :
.
def Add: (T,T) => T
That is also exactly how it is in the thesis you refer to.
The ===
also does not exist in the listing, it's just a ==
.
And you have to call parseAll
on the parser:
parser.parseAll(...)
It seems to me that you are missing the basics of scala programming, otherwise you could have easily fixed those errors yourself. My advice is, read a good scala book to get a basic understanding of the language before you start with advanced topics.
Upvotes: 1