Generate correct code in xtend [message #1850314] |
Fri, 25 February 2022 02:07 |
Muhammad Salman Tayyab 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 128 times) -
Attachment: 2.png
(Size: 680.73KB, Downloaded 114 times)
Salman
|
|
|
|
|
|
Re: Generate correct code in xtend [message #1850319 is a reply to message #1850318] |
Fri, 25 February 2022 05:17 |
Muhammad Salman Tayyab 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
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03986 seconds