Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Recursive Grammar with EMF(Combining Model Code Generation and recursive Grammar)
Recursive Grammar with EMF [message #1842471] Mon, 21 June 2021 08:10 Go to next message
Frä Fröhlich is currently offline Frä FröhlichFriend
Messages: 22
Registered: June 2021
Junior Member
I am trying to add some simple recursion in my rules and I am using EMF for generating the code of my models.
I tried following the recursion guide in the docs, but get an error:
"Cannot add supertype 'JdlExpression' to sealed type 'JdlBracedExpression'."
I don't want Expression to be a Supertype of BracedExpression (since it might contain multiple expressions).
Here are my rules:
JdlExpressions returns JdlExpressions:
	{JdlExpressions}
	exp+=JdlExpression*
;

JdlExpression returns JdlExpression:
	{JdlExpression}
	(=> JdlBracedExpression | ID)
;

JdlBracedExpression returns JdlBracedExpression:
	"(" exp=JdlExpressions ")"
;


My Genmodel:
<genClasses ecoreClass="cbcmodel.ecore#//JdlExpressions">
       <genFeatures createChild="true" ecoreFeature="ecore:EReference cbcmodel.ecore#//JdlExpressions/exp"/>       
    </genClasses>
    <genClasses ecoreClass="cbcmodel.ecore#//JdlExpression">
    </genClasses>
    <genClasses ecoreClass="cbcmodel.ecore#//JdlBracedExpression">
       <genFeatures createChild="true" ecoreFeature="ecore:EReference cbcmodel.ecore#//JdlExpressions/exp"/>       
    </genClasses>

Ecore:
<eClassifiers xsi:type="ecore:EClass" name="JdlExpressions">
    <eStructuralFeatures xsi:type="ecore:EReference" name="exp" upperBound="-1" eType="#//JdlExpression"
        containment="true"/>
  </eClassifiers>
  <eClassifiers xsi:type="ecore:EClass" name="JdlExpression">
  </eClassifiers>
  <eClassifiers xsi:type="ecore:EClass" name="JdlBracedExpression">
    <eStructuralFeatures xsi:type="ecore:EReference" name="exp" upperBound="1" eType="#//JdlExpressions"
        containment="true"/>
  </eClassifiers>
Re: Recursive Grammar with EMF [message #1842472 is a reply to message #1842471] Mon, 21 June 2021 08:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
your ecore looks wired. i dont see a storage for the ID.

maybe something like this will work

JdlExpression:
{JdlExpression}
ID | JdlBracedExpression
;

JdlBracedExpression returns JdlExpression:
{JdlBracedExpression} "(" exp=JdlExpressions ")"
;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Recursive Grammar with EMF [message #1842473 is a reply to message #1842472] Mon, 21 June 2021 08:34 Go to previous messageGo to next message
Frä Fröhlich is currently offline Frä FröhlichFriend
Messages: 22
Registered: June 2021
Junior Member
This yields "Cannot find compatible feature exp in sealed EClass JdlExpression from imported package http://www.example.org/cbcmodel: The type 'JdlExpression' does not have a feature 'exp'."

What do you mean by strange ecore? Isn't Expression just a Unassigned Rule Call and thus has no attributes? How can I fix that?

[Updated on: Mon, 21 June 2021 08:35]

Report message to a moderator

Re: Recursive Grammar with EMF [message #1842475 is a reply to message #1842473] Mon, 21 June 2021 08:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i dont get your error.
your type hierarchy is terrible too
JdlBracedExpression does not inherit JdlExpression

how shall the hierarchy really look like?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Recursive Grammar with EMF [message #1842476 is a reply to message #1842475] Mon, 21 June 2021 09:04 Go to previous messageGo to next message
Frä Fröhlich is currently offline Frä FröhlichFriend
Messages: 22
Registered: June 2021
Junior Member
Ok maybe lets add another Rule and it might start making some sense.
Lets add an
JdlOperator:
"&" | "|" | "=>"

Now I want to be able to parse something like this:

(x & (x=>y) =>z)

Later I want to extend the Operations and add some rules like "foreach ... "
Re: Recursive Grammar with EMF [message #1842477 is a reply to message #1842476] Mon, 21 June 2021 09:07 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
maybe its better to start with a new grammar and a derived metamodel.
or you change the type hierarchy to reflect it

<eClassifiers xsi:type="ecore:EClass" name="JdlBracedExpression" eSuperTypes="#//JdlExpression">
<eStructuralFeatures xsi:type="ecore:EReference" name="exp" eType="#//JdlExpressions"
containment="true"/>
</eClassifiers>

https://www.eclipse.org/Xtext/documentation/307_special_languages.html#expressions


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:cannot build old xtend projects with gradle
Next Topic:Multiple xtext DSL projects in one workspace
Goto Forum:
  


Current Time: Fri Apr 26 16:26:19 GMT 2024

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

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

Back to the top