Question

I'm using xText for writing a new eclipse plugin for a new language. But when I try to generate the code Antlr give me this warning:

Decision can match input such as "'?'" using multiple alternatives: 1, 2

I'm sure, after commenting much code, that the problem is in the following code snip:

...
Expression:
     operatorExpr=OperatorExpr (condExpr=CondExpr)?
     |exprPrimary=ExprPrimary (condExpr=CondExpr)?
;

CondExpr:
     '?'
;


ExprPrimary:
     Identifier
;

OperatorExpr:
     '+' expression=Expression
;
...

How can i solve the warning?

Était-ce utile?

La solution

If you have this expression: +x?, it can be analyzed two ways :

Expression
->  operatorExpr : OperatorExpr
    ->  +
    ->  expression : Expression
        ->  exprPrimary : ExprPrimary = x
->  condExpr : ?

Expression
->  operatorExpr : OperatorExpr
    ->  +
    ->  expression : Expression
        ->  exprPrimary : ExprPrimary = x
        ->  condExpr : ?

The condExpr may be set at the top Expression or the inner one.

To solve the problem, you may leave only the second condExpr in your main rule. Anyway, it is the default way xtext will solve your ambiguity (meaning that with your current grammar it will choose the second parsing).

If you do the other way, leaving only the first condExpr, you get two problems: 1) grammar is still ambiguous for an OperatorExpression with an inner OperatorExpression (++x? for example), and 2) x? will not match anymore.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top