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

Update Xtext 2.9.2+:

Here is how the workflow looks like with the new Xtext 2.9.x generator

module org.xtext.example.usages.GenerateUsages

import org.eclipse.xtext.xtext.generator.*
import org.eclipse.xtext.xtext.generator.model.project.*

var rootPath = ".."

Workflow {
	component = XtextGenerator {
		configuration = {
			project = StandardProjectConfig {
				baseName = "org.xtext.example.usages"
				rootPath = rootPath
				runtimeTest = {
					enabled = true
				eclipsePlugin = {
					enabled = true
				eclipsePluginTest = {
					enabled = true
				createEclipseMetaData = true
			code = {
				encoding = "UTF-8"
				fileHeader = "/*\n * generated by Xtext \${version}\n */"
		language = StandardLanguage {
			name = "org.xtext.example.usages.Usages"
			referencedResource = "platform:/resource/org.xtext.example.definitions/model/generated/Definitions.genmodel"
			fileExtensions = "use"

			serializer = {
				generateStub = false
			validator = {
  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.



  20. tiberiu
    September 9, 2014 at 20:18

    I have tried your example. But in the test.use I receive an error : Could not resolve reference to Definition ‘A’. Am I missing something?

  21. Bianca
    September 18, 2014 at 15:13

    Hi Christian,

    Now that we have our two DSLs connected, working fine, I want to create a jar for Usage DSL, the second language.
    I created one but when I’m running it, I get in the command line the error with “Couldn’t resolve reference to…”
    Could you please tell me if there are any special settings/steps that I have to add/do in order to be able to create a working jar and solve the error mentioned above?

    Thanks a lot for your time,

    • September 18, 2014 at 15:21

      hi this should basically work. Can you give some hints what exactly you are doing with the jars

  22. Bianca
    September 18, 2014 at 15:30

    I want to be able to generate a file out of the second DSL, without depending on the Eclipse Environment. Through this jar.

  23. Bianca
    September 18, 2014 at 15:35

    Maybe I should mention that the file generation is working fine if I am using eclipse.

    • September 18, 2014 at 16:12

      how does your main look like? be aware of the following: in standalone mode you have to

      (1) call the standalonesetup of both dsls
      (2) add all relevant resources to the resourceset

  24. MartinP
    September 5, 2015 at 18:20


    I am trying to reference the data from of on DSL from another DSL model but I allways get the error “could not resolve reference”.
    I have two models Can2 and Sdata. And the Can2 model should use data defined in Sdata.

    I use Xtext SDK version 2.8.4

    I also had a look on Stefans solution what is uploaded on the dropbox but i could not understand the example.

    1)Below there is a part of my xtext file from CAN2. There i import the DSL Sdata and reference to sdata::Model.

    grammar org.xtext.Can2 with org.eclipse.xtext.common.Terminals

    generate can2 “http://www.xtext.org/Can2”
    import “http://www.xtext.org/Sdata” as sdata

    ‘test-import’ ‘{‘
    ‘Component’ name=ID ‘use’ definition=[sdata::Model]

    2)I also changed the workflow file and provided the path to the Sdata Package and Sdata.genmodel:
    registerGeneratedEPackage = “org.xtext.sdata.SdataPackage”
    3) In the manifest of Can2 i added org.xtext.sdata

    4) I run the Can2 model and when i try to reference the data from the Sdata model i get “could not resolve reference”.

    • September 5, 2015 at 19:24

      if you use the open model element dialog. how do the names of the “sdata::Model” elements look like

  25. MartinP
    September 6, 2015 at 07:29

    You mean with “open model element diaglog” the opening the ecore model?

    If i open the ecore model of sdata:
    -> ->data:sdata
    -> ->sdata:EString
    I already use references inside my Can2 model and they are working. It seems to be an issue across different files. My Can2-Model and the sdata-Model are stored in the same folder.

    Below the whole grammar definition of the Sdata and some line of the sdata-model:

    Thanks a lot for the quick response.

    grammar org.xtext.Sdata with org.eclipse.xtext.common.Terminals

    generate sdata “http://www.xtext.org/Sdata”


    ‘Data Description’ name=ID DataName=STRING ‘{‘
    (Comp +=Components)*

    Components: ‘Component’ name=ID CompName=STRING ‘{‘
    (Act +=Activities)*


    name=ID Act=STRING ‘,’
    Thats how my testdata.sdata model looks like:

    Data Description dataID “eStorage Data”{
    Component comp1 “STDC” {
    f1 “Set Voltage” ,
    f2 “Switch System On,Off,Standby”,

  26. September 6, 2015 at 09:15

    you reference a model. definition=[sdata::Model]. but the model has no name. so how do you expect the reference to work?!?

  27. MartinP
    September 6, 2015 at 15:16

    Yes you are right. I also tried around with [sdata::Component] and [sdata:Activity] which have names and it is still not working.

  28. MartinP
    September 6, 2015 at 15:21

    Here is the link to my last source im trying with

    • September 6, 2015 at 15:34

      Withoutlookingatyourcode: use ref=[Type|FQN] with FQN:ID(“.“ ID)*;

  29. MartinP
    September 8, 2015 at 16:26

    I removed the reference from Sdata to CAN model and now it is working in the manifest.
    Now i managed to see all the elements of Sdata from type “ID” and “STRING”.

    How is it possible to get also the data defined in type “INT”. In my case i want to use the adress definied in Sdata (exampe. 12) in the CAN-Model.

    name=ID ‘Type’ Type=STRING ‘Adress’ Adress=INT ‘Slot’ Slot=STRING

    • September 8, 2015 at 16:36

      Can you please ask this in the eclipse forum with all attached (sample grammar sample model etc

  30. October 22, 2015 at 09:39

    Hi Christian,
    We are faced with a problem during the development of the code generator for our custom grammars. We have Component file with its own grammar and Composition file with another grammar. Composition file has a link to Component file through the keyword ‘import’: import “MediaPlayer.ctrlcomponent”.
    So we don’t know how to configure interaction between these two grammars to create a general Composition-model, which includes Component model.

    CompositionModel :

    ‘import’ importURI=STRING;
    Composition :
    ‘controllerComposition’ name=ID
    ‘}’ ‘;’?;
    component=[Component] name=ID ‘;’?;


    ComponentModel :

    Import :
    ‘import’ importURI=STRING;

    Component :
    ‘controllerComponent’ name=ID
    (‘root ‘ ‘trace’ ‘scope’ ‘=’ traceScope+=ID (‘,’ traceScope+=ID)*)?
    (‘proxy’ ‘connection’ ‘reporting’ ‘=’ connectionReporting=EConnectionReporting)?
    ‘}’ ‘;’?
    enum EConnectionReporting:
    detailed | summarized;

    MediaPlayerService.composition file:

    import “MediaPlayer.component”
    controllerComposition MediaPlayerService {
    MediaPlayer MediaPlayer;

    Mediaplyer.component file:

    controllerComponent MediaPlayer {
    root trace scope = MediaPlayer
    proxy connection reporting = summarized

    Thanks in advance!

    • October 22, 2015 at 10:04

      what is the exact problem you have?

      • October 22, 2015 at 10:32

        We use type “Component” from Component-grammar inside of Composition-grammar, but type Component is defined in Component-grammar. And while we are doing code-generating, we run into following problem:
        We get instance of Component. It is not null, but it DOES NOT contain any data inside, even though we put the data of Component into file that imported with type Component.

      • October 22, 2015 at 10:41

        then you are doing something wrong. without having a complete reproducable example it is hard to tell what prevents the proxy from beeing resolved. so e.g. what do you mean by “when generating code”? does it work in the editor? can you follow the reference via f3?

  31. October 23, 2015 at 07:07

    hi, i still do not understand your problem. if you have a file a.adsl and a file b.bdsl and b has a reference to a then you can follow that reference during code generation without problems. if i do no have code i can generate or execute or detailed information what you are actually doing and what does go wrong i can only guess in the wild. on answer would e.g. be if you run this in the ide or standalone. cause if you run standalone you of course have to load all insolved files into the resourceset manually (but as said: this is a wild guess only due lack of information)

  32. Michel Caillat
    January 22, 2016 at 17:47

    Hi Christian,

    Very interested in the possibility to refer to elements of a DSL from another DSL, I developed a prototype which works nicely as long as I test it in Eclipse. But if I run the code generation by running the JVM from the bash command line, I end up with messages like “ERROR:Couldn’t resolve reference to … ”

    More precisely, I have two DSLs, say dsl and dsl1. For each of them I wrote the grammars (one grammar “import” the other one), the xtend generators, edited the mwe2 file of the “importing” language, added the required plugins to the MANIFEST and so on. So that I have a pair languages which I can edit/use with references to ‘dsl’ files from ‘dsl1’ files and trigger code generation with success (at the condition that *all* my source files –dsl and dsl1 — are in the same directory though).

    Happy of the first result, I started to move to an ant based approach. Created build.xml files, defining correctly the class paths , ran ant for both languages. Everything went fine until the time I run the Main java which executes the generator.

    volte:TestMyDsl michel$ java -classpath $MYDSL1_DIR/lib/*:$MYDSL1_DIR/ant/bin/:$MYDSL_DIR/ant/bin/ org.xtext.example.mydsl1.generator.Main ./src/import.mydsl1
    ERROR:Couldn’t resolve reference to Person ‘B’. (./src/import.mydsl1 line : 1 column : 4)
    ERROR:Couldn’t resolve reference to Person ‘A’. (./src/import.mydsl1 line : 3 column : 4)
    ERROR:Couldn’t resolve reference to Person ‘Charlie’. (./src/import.mydsl1 line : 5 column : 4)
    volte:TestMyDsl michel$

    ‘Charlie’, ‘A’ and ‘B’ are defined in dsl files, while they are refered to in a dsl1 file.
    Again it works in Eclipse but not with java launched from bash..

    Thank you in advance for any clue !

  33. Michel Caillat
    January 25, 2016 at 13:06

    Hi Christian, thank you for your attention to my question. Here is the Main for DSL1, which refers to DSL’s objects.

    package org.xtext.example.mydsl1.generator;
    import java.util.List;

    import org.eclipse.emf.common.util.URI;
    import org.eclipse.emf.ecore.resource.Resource;
    import org.eclipse.emf.ecore.resource.ResourceSet;
    import org.eclipse.xtext.generator.GeneratorContext;

    import org.eclipse.xtext.generator.JavaIoFileSystemAccess;
    import org.eclipse.xtext.util.CancelIndicator;
    import org.eclipse.xtext.validation.CheckMode;
    import org.eclipse.xtext.validation.IResourceValidator;
    import org.eclipse.xtext.validation.Issue;
    import org.xtext.example.mydsl1.MyDsl1StandaloneSetupGenerated;

    import com.google.inject.Inject;
    import com.google.inject.Injector;
    import com.google.inject.Provider;

    public class Main {

    public static void main(String[] args) {
    if (args.length==0) {
    System.err.println(“Aborting: no path to EMF resource provided!”);
    Injector injector = new MyDsl1StandaloneSetupGenerated().createInjectorAndDoEMFRegistration();
    Main main = injector.getInstance(Main.class);

    private Provider resourceSetProvider;

    private IResourceValidator validator;

    @Inject private GeneratorContext generatorContext;

    private JavaIoFileSystemAccess fileAccess;

    private MyDsl1Generator generator;

    protected void runGenerator(String string) {
    // load the resource
    ResourceSet set = resourceSetProvider.get();
    Resource resource = set.getResource(URI.createURI(string), true);

    // validate the resource
    List list = validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
    if (!list.isEmpty()) {
    for (Issue issue : list) {

    // configure and start the generator
    CancelIndicator ci = CancelIndicator.NullImpl;
    generator.doGenerate(resource, fileAccess, (GeneratorContext) ci);

    System.out.println(“Code generation finished.”);

    • January 25, 2016 at 13:15

      You call only one standalone setup and load only one resource? You have to load all resources

  34. Michel Caillat
    January 25, 2016 at 13:24

    I certainly did something very naive. You mean that a) I must call *all* the standalone setups attached to the languages which I plan to use, b) load *all* the resources (i.e. all the .dsl files) which I plan to use in my references ?

    • January 25, 2016 at 13:27

      You have to call all standalone setups for all languages you have to load all resources of the model to the resource set not only one

  35. Michel Caillat
    January 25, 2016 at 13:33

    Thank you Christian. This sounds more than logical. Let me fix my Main.

  36. Thomas
    March 25, 2016 at 19:26

    Hello Christian, did you by any chance try to replicate this example with Xtext 2.9? I can’t get it to work. Referencing entities from one DSL/model inside another DSL just won’t work.

    • March 26, 2016 at 08:54

      can you please ask this question which all you have on stackoverflow or the eclipse xtext forum. xtext 2.9.x has too many vectors build system, project layout, etc to give a general answer. the only common thing is to use a referencedResource = “…/abc.genmodel” inside the language section of the depending language

  37. Thomas
    March 25, 2016 at 20:32

    Could you perhaps show how to do the same thing, but instead of a DSL referencing elements from another DSL, have a DSL referencing elements from another Ecore model (that is, not a Xtext project)?

    It seems that the required steps are almost the same, but when starting the runtime eclipse environment the DSL editor can’t access the elements from the Ecore model. (whereas with two DSLs it magically works without one DSL “importing” the elements from the other DSL)

    • March 26, 2016 at 08:55

      for non xtext jects the key it to build up a resourceserviceprovider. this is what my blogpost on uml basically describes (the uml usecase is more compilated than needed for “normal” emf models)

  38. March 27, 2016 at 08:47

    i added a sample workflow see updated post

  39. Radhakrishna
    July 22, 2016 at 08:13

    Can I have multiple grammar files with same extension?

    • July 22, 2016 at 15:59

      yes and no. with some manual work you could bind your editors and the builder to content types but you would have to investigate that yourself. what is the reason behind this requirement?

  40. mranderson499
    August 14, 2016 at 17:21

    How can I refer from a supertype to imported language fragments?
    If I try the following, I get an error “Cannot add supertype ‘A’ to sealed type ‘B'”:
    {def::B} | {def::C};

    • August 14, 2016 at 17:23

      Please give more context

      • mranderson499
        August 14, 2016 at 17:50

        For example I define a grammar “Testtwo”. There I define the following:

        ‘Hello’ name=ID ‘!’;

        ‘Bye’ name= ID ‘!’

        I import that into my grammar “Testone” as “two”. There I want two an action that could be either Greeting or Byebye. So what I do is:

        {two::Greeting} | {two::Byebye}

        While if I had defined it all in one file I would just use the first in the same way and then

        Greeting | Byebye

  41. August 14, 2016 at 17:57

    this makes no sense. if you import you cannot call rules thus this makes no sense. you need actually to copy the rules. or not import but inherit

  42. June 2, 2017 at 19:22

    Hi Christian,
    I’ve referenced elements of one DSL from another DSL(second). Then, I have done a Text to Model Transformation using MTC-FLOW tool. However, when I try to navigate among the resulting model I can’t see children properties from the second DSL only see the parent properties. Thus, when I try to do an EGL transformation, I can’t print all information from the model. Can you help me?

    • June 3, 2017 at 06:34

      Hi how do you read the models for that transformation? standalone? if yes make sure you call both standalonesetups and load all relevant resources into the resourceset. in eclipse? make sure you use IResourceSetProvider to obtain the resourceset

      • July 17, 2017 at 20:24

        Thanks for your answer! However, I don’t understand how to load resources into the resourceset. Can you give me an example, please?

      • July 18, 2017 at 03:57

        URI otherUril = URI.create …..
        Resource otherResource = rs.getResource(uri, true)
        otherResource.load(null) ….

        would help if you would give some snippets that you are actually ding

      • natanjho
        July 18, 2017 at 22:30

        Ok, I’ll try to explain what I want to do!
        I only have one DSL with the following expressions:

        Module | Component

        Module: ‘Module’ name=Name LEFTCURLY
        ‘components’ COLON LEFTBRACKET (components+=[Component] COMA?)+ RIGHTBRACKET SEMICOLON

        ComponentStructurer | ComponentBasic;

        ‘ComponentStructurer’ name=Name LEFTCURLY
        ‘components’ COLON LEFTBRACKET (compoBasic+= [Component] COMA?)+ RIGHTBRACKET SEMICOLON

        —-end grammar —–

        Grammar supports start with module or component thus I want to have one file for module definition and other for components definition. I’ve read about scoping but I haven’t achieved to see all information about component references from module file (only get name property).

        first dls .mod
        Module Professor {
        components : [ProfessorInformation, ProductInformation];
        —second file

        ComponentStructurer ProductInformation {
        components : [FormProduct];

        ComponentBasic FormProfessor{
        DetailData: Professor;

        When I get xmi from dsl 1, I can’t able to see detailData either isOnlyRead properties. Thanks

      • July 19, 2017 at 05:15

        i assume this is a problem of qualified name.
        – have a look at the open model element dialog (crtl shift f3). which is the name the component has. likely it is ModuleName.ComponentName
        – either you need to change this
        – or you need to change the grammar ref=[SomeType|FQN] with FQN: ID (“.” ID)*;

        and it would be interesting how Name Looks like

      • natanjho
        July 19, 2017 at 21:33

        Hi Christian, I’ve changed the grammar with qualified name but I can’t navigate among component properties. When I ask for Module.components I only get a Sequence with eProxyURI (Not Object). Can I send you my grammar and model result screenshot? Thank

      • July 20, 2017 at 04:09

        Id prefer if you create a new minimal sample grammar, implement scoping etc and share the project via a git repo

      • November 16, 2017 at 21:33

        Hi Christian, I have published a repo in https://github.com/jhonatan89/gen-angular-grammar I am executing second eclipse instance and I have two files and one of them has a reference to another element inside another file. I can reference elements without problems. But when I make a Text to Model transformation(DSL-XMI) through of MTC-FLOW tool my result model doesn’t have children properties for all referenced elements. When I print element referenced I get
        “co.edu.Grammar.impl.ServiceImpl@7176b4 (eProxyURI: file:/C:/Users/…/Java%20and%20Models/runtime-EclipseXtext/ComponentGenerator/assets/grammar.angularApp#|8)” but I can’t get ‘Object’.

        I need to go through the whole model!!

        If I put all information in one file. It works! Any idea for this problem?

        Please, can you help me?

      • November 17, 2017 at 07:58

        make sure you load the complete model correctly when doing the transformation and to do the move to xmi resources in a correct way as well
        (move in sync). the next question would be why do you need xmi at all

  43. jatnho
    July 4, 2017 at 23:16

    Hi Christian,
    I added the dependency to the other grammar, but when I try to import it, Eclipse cant find it (the import statement has an error). The grammar I want to reuse, itself uses Xbase instead of Terminals. Is that maybe the reason behind it not finding it?

    • July 5, 2017 at 05:25

      Did you check if your elements are in the xtext index using the open model element dialog (Navigate Menu or Crtl/Cmd+Shift+F3)

      • jatnho
        July 5, 2017 at 21:07

        Yes, I can see them there. But still Eclipse cant find it, it should get suggested in autocompletion when you enter the URI string. The grammar is also imported in the workbench, so I´m not sure which step I left out.

      • July 6, 2017 at 03:42

        can you please elaborate what you are doing and what is not working? maybe the xtext forum is a better place to discuss

  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

%d bloggers like this: