Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Epsilon » Visualising an expression grammar
Visualising an expression grammar [message #1059916] Wed, 22 May 2013 07:41 Go to next message
Claudio Heeg is currently offline Claudio HeegFriend
Messages: 75
Registered: April 2013
Member
Hello.

I have created an Xtext grammar working with arithmetic expressions (and functions and whatnot, but for the sake of this question we'll keep it easy) and now I am wondering whether I could visualise it with just Emfatic annotations and EuGENia.
I'm starting to doubt that, however, but I could be overlooking something terribly simplistic.

The grammar is as follows (as described in [1]):
grammar org.xtext.example.test.Test with org.eclipse.xtext.common.Terminals

import "http://www.eclipse.org/emf/2002/Ecore" as ecore
generate test "http://www.xtext.org/example/test/Test"

Addition returns Expression:
 Multiplication ({Addition.left=current} op='+' right=Multiplication)*;

Multiplication returns Expression:
 Primary ({Multiplication.left=current} op='*' right=Primary)*;

Primary returns Expression:
 NumberLiteral |
 '(' Addition ')';

NumberLiteral:
 value=INT;


The resulting Emfatic document:
@namespace(uri="http://www.xtext.org/example/test/Test", prefix="test")
package test;

class Expression {
}

class NumberLiteral extends Expression {
  attr int value;
}

class Addition extends Expression {
  val Expression left;
  attr String ~op;
  val Expression right;
}

class Multiplication extends Expression {
  val Expression left;
  attr String ~op;
  val Expression right;
}


The general goal is to create a (bidirectially editable) visualisation approximating the AST shown in [1], but any kind of hint could be helpful, especially since this is a rather 'open' question, for lack of a better word.

Thanks for your time.

--

[1] http://blog.efftinge.de/2010/08/parsing-expressions-with-xtext.html

[Updated on: Wed, 22 May 2013 07:42]

Report message to a moderator

Re: Visualising an expression grammar [message #1059943 is a reply to message #1059916] Wed, 22 May 2013 09:14 Go to previous messageGo to next message
Dimitris Kolovos is currently offline Dimitris KolovosFriend
Messages: 2162
Registered: July 2009
Location: York, UK
Senior Member

Hi Claudio,

Would this help?

http://www.eclipse.org/epsilon/doc/articles/eugenia-phantom-nodes/

Cheers,
Dimitris
Re: Visualising an expression grammar [message #1059945 is a reply to message #1059943] Wed, 22 May 2013 09:37 Go to previous messageGo to next message
Claudio Heeg is currently offline Claudio HeegFriend
Messages: 75
Registered: April 2013
Member
Hello Dimitris,

I actually looked at phantom nodes, i.e. that exact page, a little earlier.
I thought it would only be useful for later finetuning of the model, but it actually seems to have worked decently on first try.
However, the visualisation seems to be missing a level at the top as well as correct names for the operations (see attachment) - that's a mistake in annotations, though, I reckon.

In any case, thanks for your help.

//

After playing around for quite a while, I can say that I have no idea on how to make it work properly, sadly.
  • Attachment: sample.png
    (Size: 9.79KB, Downloaded 232 times)

[Updated on: Wed, 22 May 2013 10:46]

Report message to a moderator

Re: Visualising an expression grammar [message #1059971 is a reply to message #1059945] Wed, 22 May 2013 12:03 Go to previous messageGo to next message
Claudio Heeg is currently offline Claudio HeegFriend
Messages: 75
Registered: April 2013
Member
The following grammar/EMF worked for me. As good as possible.

grammar org.xtext.example.test.Test with org.eclipse.xtext.common.Terminals

generate test "http://www.xtext.org/example/test/Test"

Calcs: calc+=Calculation*;

Calculation:
	(literal=StringLiteral operation='=')? term=Addition;

Addition returns Expression:
	Multiplication ({Addition.left=current} operation='+' right=Multiplication)*;

Multiplication returns Expression:
	Primary ({Multiplication.left=current} operation='*' right=Primary)*;

Primary returns Expression:
	NumberLiteral |
	'(' Addition ')';

NumberLiteral:
	name=INT;

StringLiteral:
	name=STRING;


@namespace(uri="http://www.xtext.org/example/test/Test", prefix="test")
package test;

@gmf.diagram(foo="bar",diagram.extension="arith_diagram")
class Calcs {
  val Calculation[*] calc;
}

@gmf.node(label="operation")
class Calculation {
  @gmf.link()
  val StringLiteral literal;
  attr String operation;
  @gmf.link()
  val Expression term;
}

class Expression {
}

@gmf.node(label="name", phantom="true")
class NumberLiteral extends Expression {
  attr int name;
}

@gmf.node(label="name", phantom="true")
class StringLiteral {
  attr String name;
}

@gmf.node(label="operation", phantom="true")
class Addition extends Expression {
  @gmf.link()
  val Expression left;
  attr String operation;
  @gmf.link()
  val Expression right;
}

@gmf.node(label="operation", phantom="true")
class Multiplication extends Expression {
  @gmf.link()
  val Expression left;
  attr String operation;
  @gmf.link()
  val Expression right;
}


However, I run into errors, mostly NPE's, after rewriting formulae.
Example:
4*2+3*5
to
4*(2+3)*5
yields an erroneous tree.
After recreating the diagram, those are gone, so I thought I might want to note that in case that behaviour stems from a bug.

Also, on another note: I seem to be unable to reference attributes via the name "~op" (the assignment is "op" in the grammar), even though EMFatic seems to turn them into "~op".

[Updated on: Wed, 22 May 2013 12:28]

Report message to a moderator

Re: Visualising an expression grammar [message #1060709 is a reply to message #1059971] Tue, 28 May 2013 07:26 Go to previous messageGo to next message
Claudio Heeg is currently offline Claudio HeegFriend
Messages: 75
Registered: April 2013
Member
Hello,

after a little work I managed to put up the important formulae in tree-form.
I did this by heavily annotating every function, i.e. every parameter of every function got a gmf.link() to show in the tree.
And that, as you might imagine, caused me to run into out-of-heapspace errors and the like and made it impossible to generate diagram code.

As, first of all, the tree view doesn't look all too nice and secondly is costly, I wonder if I could instead put at lease the Parameters of a proper, non-primitive function (i.e. everything except +-*/) into that function's compartment (or as that function's property)?
I did try gmf.compartment(foo="bar") on the parameters, but that didn't seem to work ("Called feature domainMetaElement on undefined object").

A snippet of the grammar look as following:
Function_Simple:
	Abs | Cos;

Abs:
	name='Abs''('add=Addition')';

Cos:
	name='Cos''('add=Addition')';

As you can see those functions can include additions by themselves, so the corresponding Emfatic file snippet looks as follows:

class Expression {
}

@gmf.node(label="name", phantom="true")
class Function_Simple extends Expression {
  attr String name;
  @gmf.compartment(foo="bar")		// that doesn't work, @gmf.link() does
  val Expression add;
}

class Abs extends Function_Simple {
}

class Cos extends Function_Simple {
}



Any help would, as always, be greatly appreciated.

// Edit:
Nevermind, I now transformed that graphic into a pure compartment model. The possibility of an outright mix seems unlikely.
However, putting function parameters into the model as property values still seems interesting.

And, another related question that arose: Instead of modeling the elements as "Abs, Cos, ...", can I just annotate them somehow to only make them "Function_Simple", and, again, have the function's name only as a property value?
It is the same with primitive functions - they'll show up as Addition, Multiplication, ... instead of as an Expression, which makes for poor ability to bidirectionally edit the diagram.

[Updated on: Tue, 28 May 2013 08:19]

Report message to a moderator

Re: Visualising an expression grammar [message #1060891 is a reply to message #1060709] Tue, 28 May 2013 22:49 Go to previous messageGo to next message
Dimitris Kolovos is currently offline Dimitris KolovosFriend
Messages: 2162
Registered: July 2009
Location: York, UK
Senior Member

Hi Claudio,

It may just be me being too tired after a long day, but I'm afraid I've lost the plot here. It'd really help if you could provide a minimal metamodel and perhaps a sketch of the desired visualisation so that I can investigate.

Cheers,
Dimitris
Re: Visualising an expression grammar [message #1061584 is a reply to message #1060891] Mon, 03 June 2013 07:19 Go to previous messageGo to next message
Claudio Heeg is currently offline Claudio HeegFriend
Messages: 75
Registered: April 2013
Member
Hello Dimitris,

thank you for at least trying to understand my problem!

As requested, here's a small sample grammar and a very simple sketch of the desired outcome.
I'm just wondering whether this representation is possible simply using Eugenia annotations (or at least getting close to it) or whether I'd have to dig into those diagram files themselves.

Sample grammar:
grammar org.xtext.example.test.Test with org.eclipse.xtext.common.Terminals

generate test "http://www.xtext.org/example/test/Test"

Calcs: calc+=Calculation*;

Calculation:
	(literal=StringLiteral operation='=')? term=Addition;

Addition returns Expression:
	Multiplication (({Addition.left=current} operation='+' | {Substraction.left=current} operation='-') right=Multiplication)*;

Multiplication returns Expression:
	Primary (({Multiplication.left=current} operation='*' | {Division.left=current} operation='/') right=Primary)*;

Primary returns Expression:
	NumberLiteral | StringLiteral | Function_Simple |
	'(' Addition ')';
	
Function_Simple:
	Abs | ACos
;
Abs:
	name='Abs''('val=NumberLiteral')';

ACos:
	name='ACos''('add=Addition')';

NumberLiteral:
	name=INT;

StringLiteral:
	name=STRING;


Thank you for your help and time.
  • Attachment: example.png
    (Size: 1.99KB, Downloaded 220 times)
Re: Visualising an expression grammar [message #1062274 is a reply to message #1061584] Thu, 06 June 2013 23:01 Go to previous message
Dimitris Kolovos is currently offline Dimitris KolovosFriend
Messages: 2162
Registered: July 2009
Location: York, UK
Senior Member

Hi Claudio,

Phantom nodes and expression-backed labels [1] should hopefully do the trick here.

Cheers,
Dimitris

[1] http://wiki.eclipse.org/Graphical_Modeling_Framework/Versions/GMF_2.3
Previous Topic:ETL Transformation wipes out target model
Next Topic:Diagram Partitioning
Goto Forum:
  


Current Time: Tue Apr 16 20:28:00 GMT 2024

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

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

Back to the top