0
votes

If I have the following grammar:

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

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

WithX:
    A | B | C;

A:
    "a" x=INT y=INT;

B:
    "b" x=INT y=INT;

C:
    "c" x=INT;

Then Xtext will generate the following Ecore model with a nice super class to factorize x:

Ecore model generated from the first grammar

However, if I add a rule to the grammar to also factorize y:

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

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

WithX:
    A | B | C;

WithY:
    A | B ;

A:
    "a" x=INT y=INT;

B:
    "b" x=INT y=INT;

C:
    "c" x=INT;

Then the generated Ecore Model does not factorize any feature anymore:

Ecore model generated from the second grammar

Is there any wait to obtain x in WithX and y in WithY?

1

1 Answers

0
votes

I just answered my own question. I simply had to add a relationship between WithX and WithY:

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

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

WithX:
    WithY | C;

WithY:
    A | B ;

A:
    "a" x=INT y=INT;

B:
    "b" x=INT y=INT;

C:
    "c" x=INT;

And it now generates:

Ecore model generated from fixed grammar

Hurray :)