Home » Modeling » Epsilon » Visualising an expression grammar
Visualising an expression grammar [message #1059916] 
Wed, 22 May 2013 07:41 
Claudio Heeg 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/parsingexpressionswithxtext.html
[Updated on: Wed, 22 May 2013 07:42] Report message to a moderator


 
Re: Visualising an expression grammar [message #1059945 is a reply to message #1059943] 
Wed, 22 May 2013 09:37 
Claudio Heeg 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 57 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 
Claudio Heeg 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:
to 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 
Claudio Heeg Messages: 75 Registered: April 2013 
Member 


Hello,
after a little work I managed to put up the important formulae in treeform.
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 outofheapspace 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, nonprimitive 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


   
Goto Forum:
Current Time: Sat Sep 20 11:59:42 GMT 2014
Powered by FUDForum. Page generated in 0.01966 seconds
