Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Left recursion (Cant reslove)
Left recursion [message #1012873] Fri, 22 February 2013 11:14 Go to next message
Rafael Bielen is currently offline Rafael Bielen
Messages: 5
Registered: February 2013
Junior Member
Hello everyone,
for this code i get a left recursion error, but i have no clue how to solve this.
I sit now on this problem over one week but no success.

...
Vardecl 		: 'int' name=ID ( ('[' size=INT ']') |  init=Init? ); 	
Objdecl 		: type=Objtyp name=ID   ( ('[' size=INT ']') | '('attrasslist=Attrasslist? ')' ); 

Assstmt 		: var=Var '=' expr=Expr;

Array			: '[' expr=Expr ']';
//Here starts now the Problem
Var			: var=[Objdecl] array = Array? ('.' attribute = Attribute) 
					| var=[Vardecl] array = Array?;
					
Relop     		: 	'<' | '<=' | '>' | '>=' | '==' | '!=';

Expr      		: orexpr=Orexpr;
Orexpr    		: Andexpr ('||' andexpr+=Andexpr)*;
Andexpr   		: Relexpr ('&&' relexpr+=Relexpr)*;
Relexpr   		: Addexpr (Relop addexpr+=Addexpr)?;
Addexpr   		: Multexpr (('+' | '-') multexpr+=Multexpr)*;
Multexpr  		: Unaryexpr (('*' | '/') unaryexr+=Unaryexpr)*;
Unaryexpr		: '!' Atom | Atom | '-' Atom ;
Atom			:  ref_obj1=[Objdecl] 'touches'  ref_obj2=[Objdecl]  
			| value=INT
			| ref_var=[Varname]
//If i delete this line it works 
			| '('expr=Orexpr ')'; 
	
Re: Left recursion [message #1012896 is a reply to message #1012873] Fri, 22 February 2013 12:10 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6385
Registered: July 2009
Senior Member
Hi,

this is explained here

http://blog.efftinge.de/2010/08/parsing-expressions-with-xtext.html

~Christian

Re: Left recursion [message #1012904 is a reply to message #1012896] Fri, 22 February 2013 12:32 Go to previous messageGo to next message
Rafael Bielen is currently offline Rafael Bielen
Messages: 5
Registered: February 2013
Junior Member
Hello Christian, thank you for you reply, i read the text and saw the video but i have no clue where is my failure, can you give me a little hint?
Re: Left recursion [message #1012912 is a reply to message #1012904] Fri, 22 February 2013 12:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6385
Registered: July 2009
Senior Member
I see basically 3 differences:

(1) assignment in '('expr=Orexpr ')' instead of '('Orexpr ')'
(2) dont use type rewrites/specificing return types at all expression rules
(3) dont use the pattern of assigning parsed stuff to a subtree

=> i simply recommend to use / adopt the pattern as it is in the blog
Re: Left recursion [message #1012931 is a reply to message #1012912] Fri, 22 February 2013 13:33 Go to previous messageGo to next message
Rafael Bielen is currently offline Rafael Bielen
Messages: 5
Registered: February 2013
Junior Member
Okay now i rewrite my code but still the same error:
Var				:	 var=[Objdecl] array = Array? ('.' attribute = Attribute) 
					|  var=[Vardecl] array = Array? ;
									

Expr returns Expression: 		KonjunktionExpr ({Or.left=current} '||' right=KonjunktionExpr)*;

KonjunktionExpr returns Expression:	RelationExpr ({Konjunktion.left=current} '&&' right=RelationExpr)*;

RelationExpr returns Expression:	AddExpr ({Relation.left=current} type=('==' | '<=' | '<') right=AddExpr)*;

AddExpr returns Expression:		MultExpr ({PlusMinus.left=current} type=('+' | '-') right=MultExpr)*;

MultExpr returns Expression:		UnExpr ({PointArr.left=current} type=('*' | '/') right=UnExpr)*;

UnExpr returns Expression:		type=('!' | '-')? atom=Atom;

Atom returns Expression:
	{NumberLiteral} value=INT |
	{TouchedVar} var=Var ('touches' touched=Var)? |
	{ExprVar} '(' expr=Expr ')';
Re: Left recursion [message #1012946 is a reply to message #1012931] Fri, 22 February 2013 13:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6385
Registered: July 2009
Senior Member
Can you share a complete paste able grammar

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Left recursion [message #1012953 is a reply to message #1012946] Fri, 22 February 2013 14:09 Go to previous messageGo to next message
Rafael Bielen is currently offline Rafael Bielen
Messages: 5
Registered: February 2013
Junior Member
Here is my full code, the recursion error is at the var statement:

Start			:	Prog; 

Prog 			:	'game' name=ID '(' (attrasslist=Attrasslist)? ')' (decl+=Decl)* stmtblock=Stmtblock (block+=Block)*;

Decl 			: 	name=Vardecl ';' | name=Objdecl ';';

Vardecl 		:  'int' name=ID ( ('[' size=INT ']') |  init=Init? ); 	
Objdecl 		: 	type=Objtyp name=ID   ( ('[' size=INT ']') | '('attrasslist=Attrasslist? ')' ); 	

Init 			: 	'=' Expr;

Objtyp			: 	'rectangle' | 'triangle' | 'circle';

Attribute		:  'height' | 'width' | 'speed' | 'x' | 'y' | 'size';

Attrasslist 	:	 Attrass (',' attrasslist+=Attrasslist)*;
Attrass 		:	 var = ( 'height' | 'width' | 'speed' | 'x' | 'y' | 'size') '=' expr=Expr;

Block 			:	 Animblock | Eventblock;
Animblock 		:	'animation' name=ID '(' type=Objtyp var_decl=ID ')' stmtblock=Stmtblock;
Eventblock 		:	'on' Keystroke Stmtblock;
Keystroke 		:	'space' | 'leftarrow' | 'rightarrow' | 'uparrow' | 'downarrow';

Stmtblock 		:	 '{' (stmt+=Stmt)* '}';

Stmt 			:	 Ifstmt | Forstmt | Assstmt ';';

Ifstmt 			:	'if' '(' expr=Expr ')' stmtblockif=Stmtblock ( 'else' stmtblockelse=Stmtblock )?;

Forstmt 		:	'for' '(' assstmt1=Assstmt ';' expr=Expr ';' assstmt2=Assstmt ')' stmtblock=Stmtblock;

Assstmt 		:	Var '=' expr=Expr;

Array			: '[' expr=Expr ']';
//Error
Var				:	 var=[Objdecl] array = Array? '.' attribute = Attribute
					| var=[Vardecl] array = Array? ;
					

Expr returns Expression: 		KonjunktionExpr ({Or.left=current} '||' right=KonjunktionExpr)*;

KonjunktionExpr returns Expression:	RelationExpr ({Konjunktion.left=current} '&&' right=RelationExpr)*;

RelationExpr returns Expression:	AddExpr ({Relation.left=current} type=('==' | '<=' | '<') right=AddExpr)*;

AddExpr returns Expression:		MultExpr ({PlusMinus.left=current} type=('+' | '-') right=MultExpr)*;

MultExpr returns Expression:		UnExpr ({PointArr.left=current} type=('*' | '/') right=UnExpr)*;

UnExpr returns Expression:		type=('!' | '-')? atom=Atom;

Atom returns Expression:
	{NumberLiteral} value=INT |
	{TouchedVar} var=Var ('touches' touched=Var)? |
	{ExprVar} '(' expr=Expr ')';


[Updated on: Fri, 22 February 2013 14:18]

Report message to a moderator

Re: Left recursion [message #1012971 is a reply to message #1012953] Fri, 22 February 2013 14:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6385
Registered: July 2009
Senior Member
Hi,

Var				:	 var=[XDecl] array = Array? ('.' attribute = Attribute)?
				;
					
XDecl:
	Objdecl | Vardecl
;	


Will help
Re: Left recursion [message #1013000 is a reply to message #1012971] Fri, 22 February 2013 15:38 Go to previous message
Rafael Bielen is currently offline Rafael Bielen
Messages: 5
Registered: February 2013
Junior Member
Wow finally, thank your!!This really save my day and weekend Smile
Previous Topic:accessing user defined templates at runtime
Next Topic:name feature not assigned with string.
Goto Forum:
  


Current Time: Thu Oct 23 17:30:19 GMT 2014

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

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