Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Left recursion
Left recursion [message #651053] Thu, 27 January 2011 15:36 Go to next message
adrian  is currently offline adrian Friend
Messages: 93
Registered: August 2009
Member
Hi
to solve left rcursion in my grammar I use this technique :
f
InfixExpression returns dsl::Expression:
OperatorExp1
({dsl::InfixExpression.codeElement+=current} leftOperand=ActionRelationshipNRef
operator='||'
((codeElement+=InfixExpression rightOperand=ActionRelationshipNRef)))?
;
OperatorExp1 returns dsl::Expression:
OperatorExp2
({dsl::InfixExpression.codeElement+=current} leftOperand=ActionRelationshipNRef
operator='&&'
((codeElement+=OperatorExp1 rightOperand=ActionRelationshipNRef)))? ;


OperatorExp2 returns dsl::Expression:
OperatorExp3
({dsl::InfixExpression.codeElement+=current} leftOperand=ActionRelationshipNRef
operator=CompOperator
((codeElement+=OperatorExp2 rightOperand=ActionRelationshipNRef)))?;

OperatorExp3 returns dsl::Expression:
OperatorExp4
({dsl::InfixExpression.codeElement+=current} leftOperand=ActionRelationshipNRef
operator=PlusOp
((codeElement+=OperatorExp3 rightOperand=ActionRelationshipNRef)))?;

OperatorExp4 returns dsl::Expression:
Expression
({dsl::InfixExpression.codeElement+=current} leftOperand=ActionRelationshipNRef
operator=StarOp
((codeElement+=ExpressionForInfix rightOperand=ActionRelationshipNRef)))?;

Expression : name=ID;

That 's works fine but I have a smal problem :
a sample expression : 'a' is considered as InfixExpression
Any one has an idea how to change the grammar a bit to let only the expressions ' a operator b ' and 'a' must be not valid.
thanks in advance
Re: Left recursion [message #651060 is a reply to message #651053] Thu, 27 January 2011 16:00 Go to previous message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
In the grammars I have written, I handle that with validation. There is
typically some container where the expression is stored. Simply scan
this container during validation and make sure there are no invalid
expressions like 'a' or '1' etc.

In my grammars, there are LiteralExpressions as PrimaryExpressions, i.e.

PrimaryExpression : LiteralExpression | OtherPrimaries... ;
LiteralExpression :
LiteralName | LiteralSring | LiteralNumber ... ;
LiteralName : name = ID ;
// etc.

- henrik

On 1/27/11 4:36 PM, piko wrote:
> Hi to solve left rcursion in my grammar I use this technique :
> f
> InfixExpression returns dsl::Expression: OperatorExp1
> ({dsl::InfixExpression.codeElement+=current}
> leftOperand=ActionRelationshipNRef operator='||'
> ((codeElement+=InfixExpression rightOperand=ActionRelationshipNRef)))?
> ;
> OperatorExp1 returns dsl::Expression:
> OperatorExp2 ({dsl::InfixExpression.codeElement+=current}
> leftOperand=ActionRelationshipNRef operator='&&'
> ((codeElement+=OperatorExp1 rightOperand=ActionRelationshipNRef)))? ;
>
>
> OperatorExp2 returns dsl::Expression:
> OperatorExp3 ({dsl::InfixExpression.codeElement+=current}
> leftOperand=ActionRelationshipNRef
> operator=CompOperator ((codeElement+=OperatorExp2
> rightOperand=ActionRelationshipNRef)))?;
>
> OperatorExp3 returns dsl::Expression:
> OperatorExp4 ({dsl::InfixExpression.codeElement+=current}
> leftOperand=ActionRelationshipNRef
> operator=PlusOp ((codeElement+=OperatorExp3
> rightOperand=ActionRelationshipNRef)))?;
>
> OperatorExp4 returns dsl::Expression:
> Expression ({dsl::InfixExpression.codeElement+=current}
> leftOperand=ActionRelationshipNRef
> operator=StarOp ((codeElement+=ExpressionForInfix
> rightOperand=ActionRelationshipNRef)))?;
>
> Expression : name=ID;
>
> That 's works fine but I have a smal problem :
> a sample expression : 'a' is considered as InfixExpression Any one has
> an idea how to change the grammar a bit to let only the expressions ' a
> operator b ' and 'a' must be not valid.
> thanks in advance
Previous Topic:Exploiting types in a DSL: creating new types and referring to existing ones
Next Topic:AbstractXtextTests in 2.0
Goto Forum:
  


Current Time: Fri Apr 26 08:43:18 GMT 2024

Powered by FUDForum. Page generated in 0.02366 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top