I am trying to create a grammar for multiplying and dividing numbers in which the "*" symbol does not need to be included. I need him to give AST. So for input:
1 2/3 4
I want AST to be
(* (/ (* 1 2) 3) 4)
I applied the following, which uses java code to create the appropriate nodes:
grammar TestProd; options { output = AST; } tokens { PROD; } DIV : '/'; multExpr: (INTEGER -> INTEGER) ( {div = null;} div=DIV? b=INTEGER -> ^({$div == null ? (Object)adaptor.create(PROD, "*") : (Object)adaptor.create(DIV, "/")} $multExpr $b))* ; INTEGER: ('0' | '1'..'9' '0'..'9'*); WHITESPACE: (' ' | '\t')+ { $channel = HIDDEN; };
It works. But is there a better / easier way?
source share