Skip to content

Instantly share code, notes, and snippets.

@linqing
Created June 30, 2012 02:22
Show Gist options
  • Save linqing/3021867 to your computer and use it in GitHub Desktop.
Save linqing/3021867 to your computer and use it in GitHub Desktop.
what's wrong with the var?
package ch19.sec02
import scala.util.parsing.combinator._
class ExprParser extends RegexParsers {
val number = "[0-9]+".r
def expr: Parser[Any] = term ~ opt("+" | "-") ~ expr
def term: Parser[Any] = factor ~ rep("*" ~ factor)
def factor: Parser[Any] = number | "(" ~ expr ~ ")"
}
object Main1 extends App {
// here parser is define as var, can't compile
var parser = new ExprParser
val result = parser.parseAll(parser.expr, "3-4*5")
if (result.successful) println(result.get)
}
object Main2 extends App {
val parser = new ExprParser
val result = parser.parseAll(parser.expr, "3-4*5")
if (result.successful) println(result.get)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment