parser2.scala
changeset 64 2d625418c011
parent 62 5988e44ea048
child 71 7717f20f0504
equal deleted inserted replaced
63:dff4b062a8a9 64:2d625418c011
   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)