Home > Xtext > Xtext: Referencing Elements of one DSL from another DSL

Xtext: Referencing Elements of one DSL from another DSL

This is a blog post on Inter-Language-Cross-References in Xtext. Let us asume we have a DSL that contains Definitions. Now we want to create another DSL that references (uses) the Definitions defined in the the first DSL.

So let us start with the first DSL: We create a new Xtext Project

And here is the (for demonstration purpose oversimplyfied) Grammar

grammar org.xtext.example.definitions.Definitions with org.eclipse.xtext.common.Terminals

generate definitions "http://www.xtext.org/example/definitions/Definitions"


	'define' name=ID;

We run GenerateDefinitions.mwe2 to generate the language.
This is all for the first DSL.

Now let us create a project for the second DSL.

To be able to reference the first DSL from the second we add a bundle dependency to the Manifest of the second

Here the resulting manifest

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: org.xtext.example.usages
Bundle-Vendor: My Company
Bundle-Version: 1.0.0.qualifier
Bundle-SymbolicName: org.xtext.example.usages; singleton:=true
Bundle-ActivationPolicy: lazy
Require-Bundle: org.eclipse.xtext;visibility:=reexport,
Import-Package: org.apache.log4j
Bundle-RequiredExecutionEnvironment: J2SE-1.5

Now we can create the Grammar from the Usage DSL

grammar org.xtext.example.usages.Usages with org.eclipse.xtext.common.Terminals

generate usages "http://www.xtext.org/example/usages/Usages"

import "http://www.xtext.org/example/definitions/Definitions" as def

	'use' definition=[def::Definition];

With "http://www.xtext.org/example/definitions/Definitions" as def we import the metamodel of our Define DSL to our Usage DSL.
Thus the Type Definition is available and can be used to define the cross reference definition=[def::Definition]

To get the thing running we have to do some adjustments to the workflow of the Usages language.

module org.xtext.example.usages.GenerateUsages

import org.eclipse.emf.mwe.utils.*
import org.eclipse.xtext.generator.*
import org.eclipse.xtext.ui.generator.*

var grammarURI = "classpath:/org/xtext/example/usages/Usages.xtext"
var file.extensions = "use"
var projectName = "org.xtext.example.usages"
var runtimeProject = "../${projectName}"

Workflow {
    bean = StandaloneSetup {
        scanClassPath = true
        platformUri = "${runtimeProject}/.."
        registerGeneratedEPackage = "org.xtext.example.definitions.definitions.DefinitionsPackage"
        registerGenModelFile = "platform:/resource/org.xtext.example.definitions/src-gen/org/xtext/example/definitions/Definitions.genmodel"

we generate our Usage DSL (GenerateUsages.mwe2)

finally we start a new runtime eclipse and give it a try

About these ads
  1. Duncan Krebs
    August 9, 2012 at 02:44

    Your example was very useful. My next question is in my use case I would have both test.define and test.use as serialized emf xml strings. To deserialize the test.use I imagine I would have to first deserialize test.define and add that as an object in the same resource set?

    In runtime, the test.define would be a set of trading signals and the test.use would be a set of strategies that reference the signals from test.define which from your example I can now do in my IDE. The next trick is figuring out how to successfully load the test.use model object in runtime so I can execute the trading strategy. Any links or tips would be helpful! – Duncan

    • August 9, 2012 at 06:16


      Am not quite sure what your problem is: simply make sure that both files are loaded into the same resource set (rs.get resource(URI,true)) and that you then call ecoreutil.resolve for the resouceset.

      • February 21, 2013 at 10:23

        Thanks for this tip! Could not find this hint anywhere else. Do you have a hint for documentation on this issue?

  2. Duncan Krebs
    August 9, 2012 at 22:02

    I got stuck at the part where adding the registerGenModelFile as that does not seem to be a valid element in the StandaloneSetup do you think its because I’m not on the most recent version? Also, if you have your dls files in different project folders do you have to do an explicit import for example “import /definitions/def1.def”? Thanks again for helping move along. – Duncan

  3. Duncan Krebs
    August 9, 2012 at 22:14

    Also noticed then when I try to run the mwe2 with only the registerGeneratedPackage it complains that there is not a registeredGenModelFile, in my mw2 file the error I get when adding a registerGenModel is “Could not resolve reference to jvm feature registerGenModel”

    • August 10, 2012 at 05:29

      Hi, the Example is targeting Xtext 2.3.0 and might be runnable with Xtext 2.2.x (maybe you have to change the namespace import with an platform:/resource/…./..ecore one.

      Is there a reason why you still use Xtext 1.0?

      You won’t need explicit imports unless you change the config regarding global scoping in the workflow

  4. Süleyman Issiz
    September 4, 2012 at 13:51

    I am trying to reference elements of one DSL from another using Xtext Version 1.0.1. I have problem when I try to register genmodel file. I have tried to register the genmodel file in the workflow file of the DSL that I want to reference the other DSL but it is possible to define “RegisterGenModelFile” in the workflow file. Do you have any experience about it? How would I solve the same problem using XText 1.0.1?


    • September 4, 2012 at 14:27

      Hi you should be able to do the register thing a few lines below in the ecoregeneratorfragment

      • Süleyman Issiz
        September 4, 2012 at 15:41

        Hi Christian,

        it works now!


  5. Süleyman Issiz
    September 7, 2012 at 11:10

    Hi Christian,
    I have managed to reference to a top level element of another DSL. But I couldn’t manage to reference to the elements which contain other elements also. In your example what would you do when you have a DSL like below and you want to access to the extension from another DSL?

    ‘define’ name=ID
    ‘extensions’ ‘{‘ (extensions+=Extension)+ ‘}';

    extensionName = ExtensionName ‘->’ extension_Definition = ExtensionDefinition';';

    ExtensionName :

    • September 7, 2012 at 11:36

      Xtext uses by default Full QualifiedNames. thus check which qualifiedname your stuff gets
      and how you reference them in the grammar (ref=[Thing] is short for ref=[Thing|ID] )
      which means that an ID will be parsed by you may need ref=[Thing|FQN] with FQN: ID (“.” ID)*;

  6. Michael Colburn
    October 26, 2012 at 14:24

    Christian, this was exactly what I needed! Thank you! I do have a question that is indirectly relevant. I have a customer who would like a definitions file such that there are unquoted strings. He hates having to put the quotes around strings. I am wondering is it possible for a definition dsl grammar to have something like this:

    Definition: name=ID ‘=’ value=unquotedSTRING

    So that in using the DSL editor on a .define file, we can have the following:

    Text1 = I am a text without quotes
    Text 2 = I also am a text without quotes

    Thank you in advance!

    • October 26, 2012 at 15:09

      Hi this should be possible by changing terminal rules e.g. for whitespace and the hidden() definition. Never the less this might be a kind of struggle for corner cases like the last line. You will find in the Xtext forum some topics on this.

    • Paul
      November 24, 2013 at 19:36

      Hi Michael,

      I am also interested about your question
      have you found a solution for that so far?


  7. Joern
    November 7, 2012 at 15:59

    Thanks – this has been helpful.
    Unfortunately, the generated Usages.ecore looks a little ugly: the referenced type is addressed using local paths (e.g., eType=”ecore:EClass ../../../../../../org.xtext.example.definitions/src-gen/org/xtext/example/definitions/Definitions.ecore#//Definition”/>).

    Is there a way to avoid this and to generate something like “http://www.xtext.org/example/definitions/Definitions” instead of the local path?

    Thank you in advance!

  8. October 20, 2013 at 07:18

    Dear All,

    I need your favor or suggestion regarding migrate the xtext version from 1.0.1 to 2.3.0 in SME application.

    I followed all following process for migrate the xtext version.
    1. Delete the old plug-ins and update the latest plug-ins in target platform.
    2. Update the Plug-in Dependencies and Import Statements.
    3. Introduction of the Qualified Name.
    4. Changes in the index and in find references.
    5. Rewritten Node Model.
    6. AutoEditStrategy.
    7. Other Noteworthy API Changes
    To consider the above steps, I started the work with Eclipse-4.2, Which has a xtext-2.3.0 dependency. Successfully I completed the all above steps and removed the compilation error.

    Problem: After that I start the testing and getting below error Messages:
    [XtextLinkingDiagnostic: null:6 Couldn't resolve reference to Material 'MPS_RECUR'.,
    XtextLinkingDiagnostic: null:9 Couldn't resolve reference to Cstic 'NUM_OF_ALLOC'.,
    XtextLinkingDiagnostic: null:15 Couldn't resolve reference to Cstic 'INSTANCE_NUM'.,
    XtextLinkingDiagnostic: null:14 Couldn't resolve reference to Class 'ALLOC'.]

    • October 20, 2013 at 13:04

      Please have a look at the docs (contains a mig guide) or ask (specific) questions on the Xtext forum

    • manoj
      November 19, 2013 at 11:01

      hi christian,
      can we use user created java objects or java classes in xtext grammar

      • November 24, 2013 at 19:40


        can you elaborate a bit? what do you mean by use?

  9. shahan
    January 5, 2014 at 15:22

    Hi christian,

    I had followed your article and managed to link two DSLs. However, i have faced problems when referencing some features. Grammar for my original two DSLs are huge, therefore i have recreated the problem using only one language.

    //Grammar example

    package = PackageDec?

    ‘package’ name=QualifiedName ;

    ‘great’ name=ID (‘with’ super = [Greeting| QualifiedName])? ‘{‘ ‘}’ ;

    ID (‘.’ ID)*;

    package p1.p2
    great G1 {}

    I need to generate a java class such as follows:
    package p1.p2;
    class G1 {}

    Therefore, i have sub-classed ‘DefaultDeclarativeQualifiedNameProvider’ with this:

    protected QualifiedName qualifiedName(Greeting ele) {
    Model m = (Model)ele.eContainer() ;
    if (m.getPackage() != null ){
    return QualifiedName.create(m.getPackage().getName(), ele.getName());
    else {
    return QualifiedName.create(ele.getName());

    All are fine and only now my problems come.

    Suppose i wrote a script like this:

    package p3.p4
    great G2 with p1.p2.G1 {}

    Unfortunately, this gives a referencing error “Couldn’t resolve reference to Greeting ‘p1.p2.G1′.” , though quick fix suggest changing to ‘p1.p2.G1′ which is exactly same as what i have written.

    Having said that, lets say i have changed package declaration to p1 instead of p1.p2 and rewrite the script as “great G2 with p1.G1 {} “, Surprisingly, that doesn’t throw an error. It seems that it fails when package declaration has multiple ‘.’ separated segments.

    Could you please tell me why is it and how to make it right ?

    Thanks in advance.

    • January 6, 2014 at 16:20

      Hi the problem is how you create the Qualified Name

      public class MyDslNameProvider extends DefaultDeclarativeQualifiedNameProvider {

      protected QualifiedName qualifiedName(Greeting ele) {
      Model m = (Model) ele.eContainer();
      if (m.getPackage() != null) {
      return getConverter().toQualifiedName(m.getPackage().getName()).append(ele.getName());
      } else {
      return QualifiedName.create(ele.getName());


      • shahan
        January 7, 2014 at 12:00

        Thanks a lot. It works now.

        On a separate note, I believe i have to re-impelment above function (qualified name for type ‘Greet’ ) in the seconds language again in case i need to get fully qualified name of a reference ?

      • January 8, 2014 at 15:32


        i did not understand this question, please elaborate …

  10. shahan
    January 8, 2014 at 20:14

    Lets create a second grammar referenced to grammer i have written in my original question.

    ‘use’ greeting=[X::Greeting | QualifiedName];

    Suppose i have written following examples:

    //DSL 1
    package p1.p2.p3
    greet A {}
    greet B {}

    //DSL 2
    use p1.p2.p3.A
    use p1.p2.p3.B

    With your suggestion for the ‘qualifidName(Greeting ele)’ of the first DSL now references are resolved correctly. However, lets say i need to generate a text file for the second DSL with output as below (just writing full qualified name of the given ‘Greeting’):


    What i tried was retrieving full qualified name from the name provider (IQualifiedNameProvider) of second language. Not surprisingly, it returns only simple name but not the full qualified name as i haven’t customized qualifiedName for the ‘Greet’ type in the second language. So, my question is it possible to access qualified name provider (IQualifiedNameProvider) of first language from second ? Otherwise do i have to re-implement customization for ‘Greeting” type in the second language’s name provider also.

    What is the best approach to archive my goal ?

    Thanking you in advance

    • January 9, 2014 at 18:43

      hi you can use iglobalserviceprovider to obtain language specific things

      • shahan
        January 10, 2014 at 03:07

        Thank you very much Christian. I have got it working by using IGlobalServiceProvider.

  11. Stefan
    May 6, 2014 at 16:54

    Hi Christian.
    I am currently working with Eclipse Kepler and Xtext 2.5.0.

    The problem is: The ecore and genmodel files I want to reference to from the grammar, even though I did all the things you described here, are not visible :(

    I added dependency in manifest, I added project holding ecore and genmodel files to the Java Build Path, I added registerEcoreFile and registerGenModelFile declarations to the mwe file, but no success.

    Running the mwe file works fine with no errors, but as I said I cannot reference the metamodel..

    Thanks in advance!

    • May 7, 2014 at 21:15

      Hi, can you share a sample project?
      what is the exact error you get and when do you get it? (in the editor, running the workflow)

  12. Stefan
    May 8, 2014 at 09:44

    Hi. Here I created an example project with ecore and genmodel files, and Xtext projects to which I configured references to the example project with those two files, as described before.

    Still I get no reference to the metamodel from Xtext grammar, after running the MWE file.. :(

    Thanks for helping, I hope I learn this time what’s the issue :)

    Link is here: https://www.dropbox.com/s/gg0ky1z8x05ag8d/projects.zip

    Kind regards,

    • May 8, 2014 at 19:04

      - org.example.metamodel has no xtext nature
      – your grammar makes no sense to me
      what do you actually want todo

    • May 8, 2014 at 19:08

      P.S: use registerGeneratedEPackage instead registerecorefile

      and what is the actual error/problem you have=

  13. Stefan
    May 8, 2014 at 21:55

    Hmm.. I need to edit the project containing the metamodel? I am not sure I will be able to do that, since I am just suppose to reference it (is it really necessary?).. The grammar does nothing, its the example grammar generated by Xtext framework..

    The actual problem is, you cannot import the metamodel into the grammar with the import declaration, its not visible from the Xtext grammar..

    Thanks for the suggestions, I will try them!

    Best regards,

  14. Stefan
    May 9, 2014 at 09:38

    It works, thanks!

  15. Stefan
    May 21, 2014 at 12:04

    I am having an agony about this issue :(
    My project that I want to refer to (with metamodel and gemodel) seems to have xtext nature added.. I added also all in MWE and manifest file.

    I add import statement with nsURI as specified in metamodel, and the running MWE fails with this stack trace: https://www.dropbox.com/s/0bg1xsnp17ikp6d/stack.txt

    I did all as you explained with a simple example i created from scratch and it works perfect, but for my real project I have this issue.. Can you help? (I spent weeks on this without luck :( )

  16. Stefan
    May 23, 2014 at 09:59

    Hi I have managed to solve this. I have created an Xtext project for my ecore metamodel that I want to refer to and just looked at how it hooks the MWE and xtext files to it.

    Then used this to create my own empty xtext project with correct hooks to the reference metamodel…

    Problem was I didn’t know how does correct MWE registerGenerateEPackage declarations should look like, and the fact that I have many packages in the referenced metamodel..

  17. Girish
    July 8, 2014 at 13:20

    Hi Christian,

    With the 2.6 release this steps does not work… and need help in achieving it.

    • July 9, 2014 at 05:43

      hi this still should work. can you share your non working code?

      • Girish Kumar
        July 9, 2014 at 10:13

        Hi I created a new set of plugins and it works..

        But by default everything is visible..

        I mean All Definitions are visible..

        I need only upon Import

        For Ex

        import “A.define”
        use A
        use B

        If i comment import then i should get errors..

        I have uncommented fragment = scoping.ImportURIScopingFragment auto-inject {}

      • July 9, 2014 at 11:55

        you have to disable importednamespaceaware… fragment and xbase fragment

  18. Girish
    July 14, 2014 at 09:36

    Hi Christian,

    There is still a problem with references. It works fine until the grammer is small. once the grammer gets bigger. it just simply generated ecore file in impl of src-gen have a look at it ..



  19. Aykut
    July 24, 2014 at 13:11

    Hello Christian,

    Thanks for the post.

    The article covers xtext resource to another xtext resource referencing. What about other options?
    ie xtext referencing instance of ecore resource or the vice versa?

    Think that I have 3 projects
    project A has a.xcore, where I generate .ecore and .genmodel files. Then I generate .edit and editor so I have a generic EMF editor
    project B is an xtext project. Its grammar has a cross reference to one of the classes in a.xcore.
    Project C has c.xcore which refers to b.ecore :) do the same generation as A.

    I have no compilation or etc. issues.

    I run the eclipse app and create an instance of A and fill the instance with values using generic EMF editor. then create a file for B. What should I do to get cross references to be able to refer to items in A? Similar for C.

    If you need a sample project, I can prepare one.



  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: