Problems with code generation [message #1749448] |
Tue, 06 December 2016 16:56  |
Eclipse User |
|
|
|
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.
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.07493 seconds