equal
deleted
inserted
replaced
113 |
113 |
114 lazy val E: Parser = (T ~ T_OP("+") ~ E) || T // start symbol |
114 lazy val E: Parser = (T ~ T_OP("+") ~ E) || T // start symbol |
115 lazy val T: Parser = (F ~ T_OP("*") ~ T) || F |
115 lazy val T: Parser = (F ~ T_OP("*") ~ T) || F |
116 lazy val F: Parser = (T_LPAREN ~ E ~ T_RPAREN) || NumParser |
116 lazy val F: Parser = (T_LPAREN ~ E ~ T_RPAREN) || NumParser |
117 |
117 |
|
118 tokenizer(lexing_rules, "1 + 2 + 3") |
118 println(E.parse_all(tokenizer(lexing_rules, "1 + 2 + 3"))) |
119 println(E.parse_all(tokenizer(lexing_rules, "1 + 2 + 3"))) |
119 |
120 |
120 def eval(t: ParseTree) : Int = t match { |
121 def eval(t: ParseTree) : Int = t match { |
121 case Leaf(T_NUM(n)) => n.toInt |
122 case Leaf(T_NUM(n)) => n.toInt |
122 case Branch(List(t1, Leaf(T_OP("+")), t2)) => eval(t1) + eval(t2) |
123 case Branch(List(t1, Leaf(T_OP("+")), t2)) => eval(t1) + eval(t2) |