Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Problems with code generation
Problems with code generation [message #1749448] Tue, 06 December 2016 21:56 Go to next message
Luiz Paulo Franz is currently offline Luiz Paulo FranzFriend
Messages: 19
Registered: November 2016
Junior Member
Hello guys.

I'm having a problem, I'm not getting information from the grammar tree in code generation. The situation is a bit unusual because my grammar is different.

What I need is to pass a valid entry in the grammar, such as:

The fridge SHALL detect and communicate information with AS MANY food packages AS POSSIBLE.


Capital letters are the operators of my grammar.

The output of this using the code generator should be:
SHALL (AS MANY AS POSSIBLE p)
p: The fridge detects and communicates information with food packages


However I am not able to use Xtend to generate this result.

My grammar was defined as:
//list of all requirements
Requirements:
	requirements+=Requirement+;

//each requirement is finalized with ";"
Requirement:
	(root+=General)+ ';';
//undefined types
terminal BOOLEAN returns ecore::EBoolean:'true'|'false';

terminal FREESTRING:  (('\\')?('a'..'z'|'A'..'Z'|'_'|'0'..'9'|','|'.'))*  ;

//Natural Language
NL hidden(WS): FREESTRING ( FREESTRING)*;

General:
	rest = Rest =>({UntilOperator.left=current}('UNTIL'|'until') right=General)*;

//aqui se encontram as demais regras da gramatica
Rest:
	Primitive|ShallOperator|MayOperator|EventuallyOperator|BeforeOperator|AfterOperator|InOperator|AsCloseOperator|AsOperator
;

Primitive:
	BooleanValue | StringValue
;
//primitives
BooleanValue:value=BOOLEAN;
StringValue:value=NL;

ShallOperator:
	('SHALL'|'shall') elements=General;

MayOperator:
	("MAY"|'may') action1=General ('OR'|'or') action2=General;

EventuallyOperator:
	('EVENTUALLY'|'eventually') element=General;

BeforeOperator:
	('BEFORE'|'before') event=NL other=General;

AfterOperator:
	('AFTER'|'after') event=NL other=General;

InOperator:
	('IN'|'in') t=NL other=General;
	
AsCloseOperator:
	('AS' 'CLOSE' 'AS' 'POSSIBLE' 'TO'|'as' 'close' 'as' 'possible' 'to') ForQ=NL;

AsOperator:
	AsEarlyOperator|AsLateOperator|AsManyOperator|AsFewOperator;

AsEarlyOperator:
	('AS' 'EARLY' 'AS' 'POSSIBLE'|'as' 'early' 'as' 'possible') value=General;
AsLateOperator:
	('AS' 'LATE' 'AS' 'POSSIBLE'|'as' 'late' 'as' 'possible') value=General;
AsManyOperator:
	('AS' 'MANY'|'as' 'many') (element=NL)? ('AS' 'POSSIBLE'|'as' 'possible') value=General;
AsFewOperator:
	('AS' 'FEW'|'as' 'few') (element=NL)? ('AS' 'POSSIBLE'|'as' 'possible') value=General;


And on the generator, I was able to do the following:
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
		// criamos um arquivo em separado para cada requisito
		var counter = 1
		for (e : resource.allContents.toIterable.filter(Requirement)) {
			fsa.generateFile("relaxExpressions" + counter + ".exp", e.compile)
			counter++
		}
	}

	def compile(Requirement r) '''
		«FOR f : r.root»
			«f.compileGeneral»
		«ENDFOR»
	'''

	def compileGeneral(General g) '''
		«IF g.rest != null»
			«g.rest.compileRest »
		«ENDIF»
	'''
	
	def compileRest(Rest r) '''
		«IF r.eContainer.class.equals(Primitive) »
			aa
		«ENDIF»
	'''


My doubts in the compileRest method, how do I deal with a "Rest" rule that does not have any internal variables in grammar, only directs to other rules?

Also, how do I get information from the left recursion treated in the General rule in grammar?

And last one, am I doing it the right way?

Thank you for your attention.
Re: Problems with code generation [message #1749469 is a reply to message #1749448] Wed, 07 December 2016 06:23 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i recommend you two things.

first have a look at the inferred metamodel classes and their type hierarchy
there you will see something like

Rest
  - AsOperator
  - ...
  - Primitive
    - StringValue
    - BooleanValue


and

General
 - UntilOperator


so with some instanceof-ing you would be able to the distinction

or have a look at the feature of Xtend that is called dispatch methods, that allows you to write something like


	def dipatch compileGeneral(General g) '''
		//TODO
	'''
	def dipatch compileGeneral(UntilOperator g) '''
		//TODO
	'''
	
	def dipatch compileRest(Rest r) '''
		// TODO
	'''
	def dipatch compileRest(BooleanValue r) '''
		// TODO
	'''
	def dipatch xompileRest(StringValue r) '''
		// TODO
	'''
	def dipatch compileRest(AsOperator r) '''
		// TODO
	'''


update: added missing dipatch keywords


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Wed, 07 December 2016 13:25]

Report message to a moderator

Re: Problems with code generation [message #1749503 is a reply to message #1749469] Wed, 07 December 2016 13:22 Go to previous messageGo to next message
Christian Schneider is currently offline Christian SchneiderFriend
Messages: 10
Registered: June 2011
Junior Member
Hi Luiz,

I also recommend the dispatch methods for your case.
To add to Christian's reply, they than have to look like

	def dispatch compileRest(Rest r) '''
		// TODO
	'''
	def dispatch compileRest(BooleanValue r) '''
		// TODO
	'''
	def dispatch compileRest(StringValue r) '''
		// TODO
	'''
	def dispatch compileRest(AsOperator r) '''
		// TODO
	'''

regards,
Christian


Christian Dietrich wrote on Wed, 07 December 2016 07:23
Hi,

i recommend you two things.

first have a look at the inferred metamodel classes and their type hierarchy
there you will see something like

Rest
  - AsOperator
  - ...
  - Primitive
    - StringValue
    - BooleanValue


and

General
 - UntilOperator


so with some instanceof-ing you would be able to the distinction

or have a look at the feature of Xtend that is called dispatch methods, that allows you to write something like


	def compileGeneral(General g) '''
		//TODO
	'''
	def compileGeneral(UntilOperator g) '''
		//TODO
	'''
	
	def compileRest(Rest r) '''
		// TODO
	'''
	def compileRest(BooleanValue r) '''
		// TODO
	'''
	def compileRest(StringValue r) '''
		// TODO
	'''
	def compileRest(AsOperator r) '''
		// TODO
	'''
Re: Problems with code generation [message #1751364 is a reply to message #1749503] Mon, 09 January 2017 12:00 Go to previous messageGo to next message
Luiz Paulo Franz is currently offline Luiz Paulo FranzFriend
Messages: 19
Registered: November 2016
Junior Member
Hi guys, thanks for the tips.

I did as you've said, to using the dispatcher, it looked like this:

def compileGeneral(General g) '''
		«IF g.rest != null»
			«g.rest.compileRestRule »
		«ENDIF»
	'''
	
	def compileRestRule(Rest r) '''
		«r.rule.compileRest»
	'''
	//sobreposicao das regras contidas em Rest
	def dispatch compileRest(Primitive p)'''
		«p.compilePrimitive»
	'''
	
	def dispatch compileRest(ShallOperator s)'''
		SHALL
		«s.elements.compileGeneral»
	'''
	
	//compilar primitivos
	def dispatch compilePrimitive(BooleanValue b)'''
		«b.value»
	'''
	
	def dispatch compilePrimitive(StringValue s)'''
		«s.value.trim»
	'''


But now in the compileRest (Primitive p) method, the following error is displayed: Unhandled parameter types: [null]

Apparently the parameter is coming with null value, any idea why this happens?

Thank you.
Re: Problems with code generation [message #1751365 is a reply to message #1751364] Mon, 09 January 2017 12:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
simply debug it? then you should see where it comes from

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Problems with code generation [message #1752291 is a reply to message #1751365] Fri, 20 January 2017 16:30 Go to previous message
Luiz Paulo Franz is currently offline Luiz Paulo FranzFriend
Messages: 19
Registered: November 2016
Junior Member
I was able to solve the problems, thank you for the tips.
Previous Topic:Problem with Cross-Referencing and ProxyURIs
Next Topic:[XTEXT] How to display specific error for special characters?
Goto Forum:
  


Current Time: Fri Apr 19 08:16:21 GMT 2024

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

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

Back to the top