Bug 377106 - [ALF] AST generated from an assignment seems incorrect
Summary: [ALF] AST generated from an assignment seems incorrect
Status: NEW
Alias: None
Product: Papyrus
Classification: Modeling
Component: Core (show other bugs)
Version: unspecified   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact: Arnaud Cuccuru CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-04-18 11:59 EDT by Erwan Bousse CLA
Modified: 2017-09-08 09:56 EDT (History)
1 user (show)

See Also:


Attachments
The expressions trace, in a separate and readable text file (1.60 KB, text/plain)
2012-04-18 12:02 EDT, Erwan Bousse CLA
no flags Details
Test program (3.28 KB, application/octet-stream)
2012-04-19 09:14 EDT, Erwan Bousse CLA
no flags Details
Output of Test.java (3.28 KB, application/octet-stream)
2012-04-19 09:17 EDT, Erwan Bousse CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Erwan Bousse CLA 2012-04-18 11:59:20 EDT
Build Identifier: Version: 3.7.2 Build id: M20120208-0800

I'm using the Alf parser of Papyrus as a standalone Java library, in order to obtain an AST from Alf code automatically, in order to implement a model-to-model transformation.

When I apply the Xtext grammatical rule "Block" to a very simple Alf block like this one :

{variable=5}

The AST I get seems correct... until I reach the "rightHandSize" property of the "AssignmentCompletion" element (which represents the "=5" part of the assignment).

At this point, the expression should be a "PrimaryExpression" with a property "prefix" containing an "INTEGER_LITERAL" element with the value 5.

But instead, I obtain a long series of "Expression" instances, each one containing an other one in its "exp" of "left" property... until it ultimately contains the "PrimaryExpression" introduced the paragraph before.

To illustrate, here is the "trace" of Expressions I obtain for this example, starting with the "AssignementCompletion" element (with, on the left, the name of the property the element is referenced by) :

--------
assignmentCompletion : org.eclipse.papyrus.alf.alf.impl.AssignmentCompletionImpl@7d2defdc (op: =)
rightHandSide : org.eclipse.papyrus.alf.alf.impl.ConditionalTestExpressionImpl@52de94b8
exp : org.eclipse.papyrus.alf.alf.impl.ConditionalOrExpressionImpl@12f5e689
exp : org.eclipse.papyrus.alf.alf.impl.ConditionalAndExpressionImpl@4df23618
exp : org.eclipse.papyrus.alf.alf.impl.InclusiveOrExpressionImpl@58bd89a2
exp : org.eclipse.papyrus.alf.alf.impl.ExclusiveOrExpressionImpl@3971832
exp : org.eclipse.papyrus.alf.alf.impl.AndExpressionImpl@34b57c75
exp : org.eclipse.papyrus.alf.alf.impl.EqualityExpressionImpl@76fe005b (op: null)
exp : org.eclipse.papyrus.alf.alf.impl.ClassificationExpressionImpl@1dc99365 (op: null)
exp : org.eclipse.papyrus.alf.alf.impl.RelationalExpressionImpl@1cebdb2a (op: null)
left : org.eclipse.papyrus.alf.alf.impl.ShiftExpressionImpl@4081b13b (op: null)
exp : org.eclipse.papyrus.alf.alf.impl.AdditiveExpressionImpl@29ab993 (op: null)
exp : org.eclipse.papyrus.alf.alf.impl.MultiplicativeExpressionImpl@7c0d633a (op: null)
exp : org.eclipse.papyrus.alf.alf.impl.UnaryExpressionImpl@52edb476 (op: null)
exp : org.eclipse.papyrus.alf.alf.impl.PrimaryExpressionImpl@73e2d982
prefix : org.eclipse.papyrus.alf.alf.impl.INTEGER_LITERALImpl@3236213e (value: 5)
--------

In my opinion, it should be simply :

--------
assignmentCompletion : org.eclipse.papyrus.alf.alf.impl.AssignmentCompletionImpl@7d2defdc (op: =)
rightHandSide : org.eclipse.papyrus.alf.alf.impl.PrimaryExpressionImpl@73e2d982
prefix : org.eclipse.papyrus.alf.alf.impl.INTEGER_LITERALImpl@3236213e (value: 5)
--------

I haven't looked into the Xtext definition of the langage ; I guess there must be a mistake somewhere in it. If required, I can provide the Java code I use to test the parser directly.

Many thanks in advance !

Reproducible: Always
Comment 1 Erwan Bousse CLA 2012-04-18 12:02:53 EDT
Created attachment 214193 [details]
The expressions trace, in a separate and readable text file

I'm adding the Expressions "trace" in a separate text file, which is much more readable than my original post.
Comment 2 Erwan Bousse CLA 2012-04-19 09:14:35 EDT
Created attachment 214237 [details]
Test program

Adding a testing program in Java, to explain what I'm concretely trying to do.
Comment 3 Erwan Bousse CLA 2012-04-19 09:17:00 EDT
Created attachment 214238 [details]
Output of Test.java

Adding the output of the test program previously attached.

You can find in this log the "trace" I show in my first post, but also the XMI output of the obtained Alf Block instance (which clearly shows that something is wrong).
Comment 4 Erwan Bousse CLA 2012-04-19 09:32:00 EDT
I finally looked into Alf.xtext, and now I undertand what happens. Here is what we an find in it at some point :

---------------------------

/**************
 * Expressions
 **************/

Expression :
	ConditionalTestExpression
;

ConditionalTestExpression :
	exp=ConditionalOrExpression ('?' whenTrue=ConditionalTestExpression ':' whenFalse=ConditionalTestExpression)?
;

ConditionalOrExpression :
	exp+=ConditionalAndExpression ('||' exp+=ConditionalAndExpression)*
;

ConditionalAndExpression :
	exp+=InclusiveOrExpression ('&&' exp+=InclusiveOrExpression)*
;

InclusiveOrExpression :
	exp+=ExclusiveOrExpression ('|' exp+=ExclusiveOrExpression)*
;

ExclusiveOrExpression :
	exp+=AndExpression ('^' exp+=AndExpression)*
;

// Skipping a lot of lines
// [...]
// Until PrimaryExpression

PrimaryExpression :
	prefix = ValueSpecification 
;
---------------------------


So this is made on purpose : "Expression" rules are expanded one after another. I suppose this is linked to operators priorities, somehow. However, wouldn't it be possible to to something like this instead :


---------------------------
Expression :
	ConditionalTestExpression |
	ConditionalOrExpression |
	ConditionalAndExpression |
	InclusiveOrExpression |
	ExclusiveOrExpression |
	// skipping most Expression rules
	PrimaryExpression 
;
---------------------------


The problem is that the AST we obtain for now is really big... for almost nothing. This will get worse with even bigger Alf programs.



Thanks in advance, and thanks for your work on Papyrus !
Comment 5 Arnaud Cuccuru CLA 2012-04-23 12:22:45 EDT
Hi Erwan,

Indeed, Expression rules in the XText grammar are designed to capture priorities of operators. And as you noticed, this makes ASTs being quite big (for a simple literal, you get an instance of each class in the "hierarchy" of expression rules).

For the moment, there is no plan to make the XText grammar evolve, since any change in the grammar may require significant changes in the validation part of the editor. 

Note however that we are currently working on a transformation from the AST produced by XText to the AST described in the Alf specification (which includes simplifications such as the one you described above with Expressions, but also interesting things such as the construction of the data dependence graph). I will let you know when this is available (hopefully, in the 3rd quarter of 2012).
Comment 6 Erwan Bousse CLA 2012-04-26 03:59:04 EDT
Thank you for your answer !

So there will be two metamodels for Alf : one very close to the concrete syntax, and one based on the spec abstract syntax.

I'll be waiting for this tranformation then :)