Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Left recursive grammar rule
Left recursive grammar rule [message #1810849] Tue, 20 August 2019 11:13 Go to next message
Eclipse UserFriend
Hello ,

So lately I've started learning to use Xtext in order to try to make models from a textual file but since the files I chose doesn't have a grammar but have a certain structure, I decided to have fun writing my own grammar for it and hopefully later i would pass them to QVTo for transformation.

But while writing the grammar I got stuck with left recursive grammar errors from these rules :


grammar org.xtext.example.codeDSL.Mycode with org.eclipse.xtext.common.Terminals

generate mycode "Link/Mycode"


CodeFile:
	(methods_decls+=Method_Decl)*;

Method_Decl:
	 method_name=ID? '()'	'('(methods_args+=Method_Arg)* ')'	compound_stmt=Compound_Stmt
;


Compound_Stmt: // a code block or a simple  list of statements
	 (stmts_list+=Statement)+
;

Statement:  
	Block_stmt | Simple_Stmt
;

Simple_Stmt:
	If_stmt | Switch_stmt | Goto_stmt | Return_stmt | Label_stmt | Call_stmt | Assign_stmt
;

Block_stmt: 
{Block} 'Code_Block' '<'    (vars_list+=Var_Decl)*    (embedded_block =Block_stmt)?        (block_body=Block_Body)?       '>' 
;

Var_Decl: 
	 var_type=TypeDef ("*")?	name=ID	("[" size=INT "]")?	';' 
;

Block_Body  returns Compound_Stmt: // List of simple statements that can't have another code block inside  
	(block_body+=Simple_Stmt)+
;

TypeDef:
	name=ID // Definition of variable types that are similare to Java types and structure declarations 	
;

Method_Arg:
	arg_list+=Var_Decl*
;



I know this looks messy but these is my first attempt as a student to use Xtext and to creating a grammar and I'm doing this while thinking that it should be valid to be used later with QVTo which I'm still discovering too.


So obviously this grammar is left recursive because of the Block_stmt rule but this is how my files are structured .
Every Code Block could have in it a list of variable declarations that's POSSIBLY followed by 1 other Code Block that's embedded in it and POSSIBLY followed by a list of simple statements (so no other code block inside) BUT the code block can't be empty it has to have either 1 other embedded code block or at least one simple statement.

To solve this i tried to follow few trics, that I've learned in class or found online, on how to eliminate left recursion but non of them seem to be similar to this rule i have.


So any help would be really appreciated.

Thank you.
Re: Left recursive grammar rule [message #1810967 is a reply to message #1810849] Thu, 22 August 2019 13:27 Go to previous message
Eclipse UserFriend
Hello again,

I already solved the recursion problem by restructuring the first part of my grammar and getting rid of the Compound_Stmt rule but i still don't know why is it the one causing the problem ?!!!

But any way i still have a lot of work to do and especially that I'm still not sure that this will give me a Meta-model with the correct structure that i need.
So any advice or remarks about this would be really welcomed.

Thank You.
Previous Topic:Is there a recommended way to turn the inferred Ecore model into imported one?
Next Topic:Text to EMF Model Generator
Goto Forum:
  


Current Time: Wed Mar 26 05:18:45 EDT 2025

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

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

Back to the top