[antlr-interest] How to define multi and div?

Bryan Ewbank ewbank at gmail.com
Sun Dec 11 07:03:53 PST 2005


It cannot be *split*, because of the ambiguity.  It can be joined into
one rule, though.  Since MULTI and DIV are at the same precedence
level, you need to build the grammar to accept either one:

    mulitdivExpr
	:  parenExpr ( ( MULTI^ | DIV^ ) parenExpr )* ;

This says to match a parenExpr, with zero or more following MULTI or
DIV operation.  Perhaps it's easier to see if it's written this
(equivalent) way:

    mulitdivExpr
	:  parenExpr			// A
	    ( MULTI^ parenExpr		// * B
	    | DIV^ parenExpr		// / B
	    )*
	;

Hope this helps,
- Bryan

On 12/10/05, °p <inshua at gmail.com> wrote:
> I 'd found this way in samples ,it did not define MULTI and DIV but define
>
> MULTI_DIV : * | /
>
> but in this way , the multi expr and div expr cannot be recognized in the
> parser, in the following step, I should test the text of the AST node to
> decide how to run the whole expression.
>
> A same rule cannot be split ?


More information about the antlr-interest mailing list