Shevach
Shevach

Reputation: 737

Eliminate left recursion in XText

This is part of the grammar of the NuSMV language:

BasicExpression:
Constant | '(' BasicExpression ')' | '!' BasicExpression | BasicExpression '&' BasicExpression;
Constant:
BooleanConstant
BooleanConstant: 
'TRUE' | 'FALSE';

Unfortunately XText is throwing an exception that states there is left recursion in this grammar. How can I fix it?

Thanks.

Upvotes: 1

Views: 275

Answers (2)

Raven
Raven

Reputation: 3526

You could simply introduce a new rule (a new tier) like that:

BasicExpression:
    firstContent=ExpressionContent ("&" secondContent=ExpressionContent)?
;

ExpressionContent:
    Constant 
    | '(' BasicExpression ')' 
    | '!' BasicExpression 
;

That way the rule is not left recursive anymore.

Greeting Krzmbrzl

Upvotes: 1

AdrieanKhisbe
AdrieanKhisbe

Reputation: 4058

Have a look at this article or the official documentation, it will explain in detail how to handle recursion in language, and taking into account operator precedence.

Upvotes: 0

Related Questions