Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Generate correct code in xtend
Generate correct code in xtend [message #1850314] Fri, 25 February 2022 02:07 Go to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
I have written a grammar for a quiz program and after declaring the question I have created a class to which I am passing the question as well as the answer. Now, in the xtend generator file I have a problem. I want to loop through the questions and answer and save them in an array on the same index with a comma. I have difficulty in doing so.

Grammar is written below: Plus in image 1 if you see line 52 I am filtering through the "AskQuestions" in which I have questions as well as answers and in image 2 if you see the main method in that there is a questions array and question 1(q1) is repeating with all three options.

Now, my question is how can I change the loop in the xtend generator file that values will add in the array when indexof of both the questions and answers is the same. I have to avoid repetition.

I want like this:
Questions [ ] questions [
new Question(q1,a),
new Question(q2,c),
new Question(q2,b),
]

grammar org.qanda.example.quesans.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.qanda.org/example/quesans/MyDsl"
Model:
parts+=ModelParts*;
ModelParts:
Questions|AskQuestions;
Questions:
"question{" ques+=Statements* "}";
Statements:
name=ID "=" value=STRING;
AskQuestions:
"answers[" ("question("show+=[Statements] "," ans+=Answer ")" (",")*)+ "]" ;
Answer:
value= STRING;


  • Attachment: 1.png
    (Size: 754.04KB, Downloaded 110 times)
  • Attachment: 2.png
    (Size: 680.73KB, Downloaded 97 times)


Salman
Re: Generate correct code in xtend [message #1850316 is a reply to message #1850314] Fri, 25 February 2022 04:29 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Use FOR ... SEPARATOR „,"
Re: Generate correct code in xtend [message #1850317 is a reply to message #1850316] Fri, 25 February 2022 04:35 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
Sorry, I didn't understand. Can you correct this piece of code.
«FOR ques:questions.show»
«FOR ans:questions.ans»
new Question(«ques.name»,"«ans.value»"),
«ENDFOR»
«ENDFOR»


Salman
Re: Generate correct code in xtend [message #1850318 is a reply to message #1850317] Fri, 25 February 2022 04:40 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

«FOR ques:questions.show»
«FOR ans:questions.ans SEPARATOR ","»
new Question(«ques.name»,"«ans.value»"),
«ENDFOR»
«ENDFOR»
Re: Generate correct code in xtend [message #1850319 is a reply to message #1850318] Fri, 25 February 2022 05:17 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
No, You understood it wrong.
Right now, if you see in image 2 q1 in the Question array is repeating 3 times because of the loop. I want that q1,q2,q3 to only occur once as I have defined in the grammar checking file (Which is attached below)
answers[
question(q1,"a"),
question(q2,"b"),
question(q3,"c")
]


Salman
Re: Generate correct code in xtend [message #1850320 is a reply to message #1850318] Fri, 25 February 2022 05:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
is there a reson to do such a bad grammar design?
if yes you can try the indexed method:

«FOR ques:questions.show.indexed»
new Question(«ques.value.name»,"«questions.ans.get(ques.key).value»"),
«ENDFOR»


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generate correct code in xtend [message #1850321 is a reply to message #1850320] Fri, 25 February 2022 05:23 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
No, not any special reason.
I am a beginner. You have understood the scenario. Can you rewrite the grammar in a good way. Thank you in advance.


Salman
Re: Generate correct code in xtend [message #1850322 is a reply to message #1850321] Fri, 25 February 2022 05:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
something like (pseudo grammar)

AskQuestions:
"answers[" (andas+=QuestionWithAnwer)+ "]" ;

QuestionWithAnwer:
"question("show+=[Statements] "," ans+=Answer ")" (",")*;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Generate correct code in xtend [message #1850381 is a reply to message #1850322] Mon, 28 February 2022 14:23 Go to previous message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
Thanks a lot. This grammar is really a professional one.


Salman
Previous Topic:Text changes not reflected on the AST
Next Topic:Installation of older SDK Versions broken
Goto Forum:
  


Current Time: Thu Apr 25 13:29:05 GMT 2024

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

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

Back to the top