I have a fairly simple mathematical expression grammar for ANTLR, and an interest in handling the implied operator *between parentheses, for example. (2-3)(4+5)(6*7)should be (2-3)*(4+5)*(6*7).
Given the input (2-3)(4+5)(6*7), I try to add the missing operator *to the AST tree when parsing, in the following grammar, I think I managed to achieve this, but I wonder the correct, most elegant way?
grammar G;
options {
language = Java;
output=AST;
ASTLabelType=CommonTree;
}
tokens {
ADD = '+' ;
SUB = '-' ;
MUL = '*' ;
DIV = '/' ;
OPARN = '(' ;
CPARN = ')' ;
}
start
: expression EOF!
;
expression
: mult (( ADD^ | SUB^ ) mult)*
;
mult
: atom (( MUL^ | DIV^) atom)*
;
atom
: INTEGER
| (
OPARN expression CPARN -> expression
)
(
OPARN expression CPARN -> ^(MUL expression)+
)*
;
INTEGER : ('0'..'9')+ ;
WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel = HIDDEN;};
This grammar appears to display the correct AST tree in ANTLRworks:

I'm just starting to understand parsing and ANTLR, I don't have much experience, so the reviews are really appreciated!
Thanks in advance! Charles