Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext grammar to Java Code(I want to generate the Java Code from a grammar.)
Xtext grammar to Java Code [message #1823622] Mon, 30 March 2020 12:39 Go to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 15
Registered: March 2020
Location: Germany
Junior Member
I have a simple grammar which is listed below
Model:
(videos+=video)*;
video:
name=ID "=" STRING ";";


AND

I want to generate the java code for this.i have read the documentation but could not get it. Please guide me on how to do it.


Salman
Re: Xtext grammar to Java Code [message #1823623 is a reply to message #1823622] Mon, 30 March 2020 12:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
Hi,

can you give an indication how the generated code should look like?
did you do this tutorial:

https://www.eclipse.org/Xtext/documentation/103_domainmodelnextsteps.html


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar to Java Code [message #1823626 is a reply to message #1823623] Mon, 30 March 2020 13:08 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 15
Registered: March 2020
Location: Germany
Junior Member
Yes, I follow the same link but when I reached the 15 Minutes extended tutorial then things are hard for me to understand as well as compare.

Code should be look like this.
Public String="Any Path in here";

  • Attachment: Capture.PNG
    (Size: 6.97KB, Downloaded 17 times)


Salman
Re: Xtext grammar to Java Code [message #1823630 is a reply to message #1823626] Mon, 30 March 2020 14:05 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
this should be straight forward from the tutorial

i change the grammar to assign the string. am not sure if this is a C&P error

video:
name=ID "=" value=STRING


class MyDslGenerator extends AbstractGenerator {

	override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
		for (m : resource.allContents.toIterable.filter(Model)) {
			fsa.generateFile("sample/Demo.java", '''
				package sample;
				public class Demo {
					«FOR v : m.videos»
						public static final String «v.name» = "«v.value»";
					«ENDFOR»
				}
			''')
		}
	}
}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar to Java Code [message #1823647 is a reply to message #1823630] Mon, 30 March 2020 17:35 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 15
Registered: March 2020
Location: Germany
Junior Member
Why I am getting this error please see the image?
  • Attachment: Capture.PNG
    (Size: 23.77KB, Downloaded 14 times)


Salman
Re: Xtext grammar to Java Code [message #1823648 is a reply to message #1823647] Mon, 30 March 2020 17:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
did you change the grammar as proposed?

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar to Java Code [message #1823649 is a reply to message #1823648] Mon, 30 March 2020 17:55 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 15
Registered: March 2020
Location: Germany
Junior Member
yes, have a look.

grammar org.video.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.video.org/example/mydsl/MyDsl"

Model:
(videos+=video)*;
video:
name=ID "=" value=STRING;


Salman
Re: Xtext grammar to Java Code [message #1823652 is a reply to message #1823649] Mon, 30 March 2020 18:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
if you regeneate the language by running the workflow and or do a clean build on the project
then this should work. in doubt open and close the editor


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar to Java Code [message #1823653 is a reply to message #1823652] Mon, 30 March 2020 18:15 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 15
Registered: March 2020
Location: Germany
Junior Member
Thank you.
It's working now. Do you have any blog which I can follow?


Salman

[Updated on: Mon, 30 March 2020 18:22]

Report message to a moderator

Re: Xtext grammar to Java Code [message #1823654 is a reply to message #1823653] Mon, 30 March 2020 18:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
i am not regularly blogging. some of my posts are on https://www.dietrich-it.de and https://blogs.itemis.com/topic/xtext

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Mon, 30 March 2020 18:24]

Report message to a moderator

Re: Xtext grammar to Java Code [message #1827183 is a reply to message #1823654] Sat, 09 May 2020 16:01 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 15
Registered: March 2020
Location: Germany
Junior Member
I have the grammar and confusing in generating the java code. Can you please look on the grammar and help in generating code.

Model:
parts+=ModelPart*
;
ModelPart:
Video | Videouse
;

Video:
"videodeclaration" "{" videos+=Videodef* "}"
;
Videodef:
name=ID "=" value=STRING ";"
;
Videouse:
"actions" "{" viduse+=Videoshow* "}"
;
Videoshow:
"show(" vid=[Videodef] ");"
;


Salman
Re: Xtext grammar to Java Code [message #1827185 is a reply to message #1827183] Sat, 09 May 2020 20:10 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
And how should the java code,look like?

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar to Java Code [message #1827188 is a reply to message #1827185] Sat, 09 May 2020 21:08 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 15
Registered: March 2020
Location: Germany
Junior Member
The code should look like this.
  • Attachment: xtext.PNG
    (Size: 12.10KB, Downloaded 18 times)


Salman
Re: Xtext grammar to Java Code [message #1827194 is a reply to message #1827188] Sun, 10 May 2020 08:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
This is the model file and not the java file you want to generate out of it

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar to Java Code [message #1827206 is a reply to message #1827194] Sun, 10 May 2020 13:12 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 15
Registered: March 2020
Location: Germany
Junior Member
Actually, the idea is that videos can be defined and presented in a specific order.
The corresponding Java code should be generated in such a way that it can be executed.
Videos are defined separately and one can show them By referencing to the identifier.
Now you got the point?


Salman
Re: Xtext grammar to Java Code [message #1827214 is a reply to message #1827206] Sun, 10 May 2020 19:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
Sorry.

It is your talk to think on about the java code for that sample model should look like
Once you have that you can map that to the generator

Maybe you also use the wrong terms and I don't get your question and you are not asking about the generator at all (MyDslGenerator)



Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar to Java Code [message #1827217 is a reply to message #1827214] Sun, 10 May 2020 20:41 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 15
Registered: March 2020
Location: Germany
Junior Member
I am asking about the generator(MyDslGenerator). I want to generate the java code for the grammar which I posted above.

Salman
Re: Xtext grammar to Java Code [message #1827225 is a reply to message #1827217] Mon, 11 May 2020 05:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
Yes but you have to design the java code first.
And then you can adapt the generator to produce it.


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar to Java Code [message #1827238 is a reply to message #1827225] Mon, 11 May 2020 08:59 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 15
Registered: March 2020
Location: Germany
Junior Member
I am finding difficulty in generating the java code and for that, I ask for your help.

Salman
Re: Xtext grammar to Java Code [message #1827246 is a reply to message #1827238] Mon, 11 May 2020 12:04 Go to previous messageGo to next message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 114
Registered: February 2016
Senior Member
Frankly speaking we can help you how to generate Java code, but first you have to decide it yourself what java code to generate.

To come up with some ideas, maybe you can write some Java code by hand, belonging to different 'videos dsl' files, than you can generalize them into a template that you integrate into your MyDslGenerator.

Hope that helps,
Tamás
Re: Xtext grammar to Java Code [message #1827249 is a reply to message #1827246] Mon, 11 May 2020 13:05 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 15
Registered: March 2020
Location: Germany
Junior Member
Now let me explain in detail.
I have different variables and all are in video declaration(see the grammar and attached picture). Now I want to create a function that takes the variables as in the same order they are defined. I am finding difficulty in writing the code for the function. You can also see the grammar and modify it as per your need.
  • Attachment: xtext.PNG
    (Size: 12.10KB, Downloaded 9 times)


Salman

[Updated on: Mon, 11 May 2020 13:45]

Report message to a moderator

Re: Xtext grammar to Java Code [message #1827255 is a reply to message #1827249] Mon, 11 May 2020 14:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
i still dont understand your question:

both have a list

so you can ask a Video object for videos (getVideos())
and a Videouse for viduse (getViduse())

which are both ordered as declared.

starting from

Model:
parts+=ModelPart*

you can either filter the parts for Video or Videouse using model.getParts().filter(Video)
of you use instanceof


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar to Java Code [message #1827257 is a reply to message #1827255] Mon, 11 May 2020 15:18 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 15
Registered: March 2020
Location: Germany
Junior Member
Actually, I am new to xtext and not have much experience in generating the java code. Whatever you are understanding can you write code( xxtend ). I will then change it to my requirement. Also, see the attached picture maybe you can now understand a bit.
  • Attachment: xtext.PNG
    (Size: 6.50KB, Downloaded 8 times)

[Updated on: Mon, 11 May 2020 15:43]

Report message to a moderator

Re: Xtext grammar to Java Code [message #1827259 is a reply to message #1827257] Mon, 11 May 2020 15:43 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6874
Registered: July 2009
Senior Member
Hi

You completely misunderstand. Xtext generates editors and associated functionality fromthe user's program input in the form of models/grammars. Xtext does not use telepathy to generate by magic.

I've not used the generators, but I suspect that these too need programming.

What you are asking is similar to asking Oracle why the Java compiler doesn't generate code to calculate prime numbers for you. It needs you to write the program.

Regards

Ed Willink
Re: Xtext grammar to Java Code [message #1827260 is a reply to message #1827259] Mon, 11 May 2020 15:54 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13628
Registered: July 2009
Senior Member
(1) before you can generate code you need to figure out how the generated code should look like. this is a manual process. the process is the same if you would write the code by hand without generator
(2) you identify static and dynamic parts in that code
(3) you take that over to the generator. if you dont feel well you can also write it in java instead of xtend

have a look at the tutorial part on code generation


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:[Solved] Specify path of file to generate
Next Topic:Standalone error when validating
Goto Forum:
  


Current Time: Thu Oct 22 18:54:14 GMT 2020

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

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

Back to the top