Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8489139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:44:37+00:00 2026-06-10T21:44:37+00:00

I’ve defined a simple Xtext grammar which looks like this (simplified): grammar org.xtext.example.mydsl.MyDsl with

  • 0

I’ve defined a simple Xtext grammar which looks like this (simplified):

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
import "http://www.eclipse.org/emf/2002/Ecore" as ecore

System:
   'Define System'
   (
     'Define Components' '{' components+=Component+ '}'
   )
   'End'
;

Component:
    'Component' name=ID 'Value' value=Double ';'
;

Double returns ecore::EDouble:
    '-'? INT? '.' INT
;

The problem I like to solve is – how can I convert a simple Java Object to a valid xtext file?

To simplify my problem, lets say we create a list of components in Java:

List<Component> components = new ArrayList<Component>();
components.add(new Component("FirstComponent", 1.0));
components.add(new Component("SecondComponent", 2.0));
components.add(new Component("ThirdComponent", 3.0));

The output-file I like to create should look like this:

Define System
   Define Components { 
       Component FirstComponent Value 1.0;
       Component SecondComponent Value 2.0;
       Component ThirdComponent Value 3.0;
   }
End

It is important that this file is checked by the xtext grammar, so that it’s valid.
I Hope you have any ideas for me. Here are some of mine, but so far I don’t know how to implement them:

Idea #1:
I know how to read and write a file. In my head one solution could look like this:
I have the list in my Java code, now I like to write a file which looks like the output-file above. Afterwards I like to read this file and check for errors by the grammar. How can I do this?

Idea #2:
If I imagine I would create a xml file out of Java code using JDOM, I wish I could do the same in xtext. Just define a parent “Define System” which ends with “End” (see my output-file) and then add a child “Define Components {” which ends with “}” and then add the children to this, e.g. “Component FirstComponent Value 1.0;”. I hope this isn’t confusing 🙂

Idea #3:
I could use a template like the following and add children between the braces “{” … “}”:

Define System
   Define Components { ... }
End

Btw: I already tried Linking Xtext with StringTemplate code generator, but it is kind of another problem. Hope you have any ideas.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:44:39+00:00Added an answer on June 10, 2026 at 9:44 pm

    You can use Xtext’s serialization for this. Unlike Java’s default Serialization API, Xtext’s implementation creates the DSL.

    The code would look like so:

    Injector injector = Guice.createInjector(new  my.dsl.MyDslRuntimeModule());  
    Serializer serializer = injector.getInstance(Serializer.class);  
    String s = serializer.serialize(eobj);  
    

    where eobj is an instance of System.

    If you have written a formatter for your DSL, the output will also look nice.

    Related blog post: Implement toString with Xtext’s Serializer

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.