ANTLR: Multiplication omitting the "*" character

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?

+2
source share
1 answer

Here is the way:

 grammar Test; options { backtrack=true; output=AST; } tokens { MUL; DIV; } parse : expr* EOF ; expr : (atom -> atom) ( '/' a=atom -> ^(DIV $expr $a) | a=atom -> ^(MUL $expr $a) )* ; atom : Number | '(' expr ')' -> expr ; Number : '0'..'9'+ ; Space : (' ' | '\t' | '\r' | '\n') {skip();} ; 

Tested with

 import org.antlr.runtime.*; import org.antlr.runtime.tree.Tree; public class Main { public static void main(String[] args) throws Exception { String source = "1 2 / 3 4"; ANTLRStringStream in = new ANTLRStringStream(source); TestLexer lexer = new TestLexer(in); CommonTokenStream tokens = new CommonTokenStream(lexer); TestParser parser = new TestParser(tokens); TestParser.parse_return result = parser.parse(); Tree tree = (Tree)result.getTree(); System.out.println(tree.toStringTree()); } } 

produced by:

 (MUL (DIV (MUL 1 2) 3) 4) 
+1
source

All Articles