Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Allow negative unary expression in combination with subtraction
Allow negative unary expression in combination with subtraction [message #1782285] Wed, 21 February 2018 10:10 Go to next message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
Hi guys,

in my grammar I have the following expression rules:

Expression returns Expression :
	Assignment;

Assignment returns Expression :
	{Assignment} feature=[ecore::EObject|ID] OpSingleAssign value=Assignment |
	OrExpression;

OpSingleAssign:
	"="
;

OrExpression returns Expression:
	AndExpression (=>({BinaryOperation.left=current} op=OpOr) right=AndExpression)*;

OpOr:
	'||';

AndExpression returns Expression:
	EqualityExpression (=>({BinaryOperation.left=current} op=OpAnd) right=EqualityExpression)*;

OpAnd:
	'&&';

EqualityExpression returns Expression:
	RelationalExpression (=>({BinaryOperation.left=current} op=OpEquality)
	right=RelationalExpression)*
	;

OpEquality:
	'==' | '!=' ;

RelationalExpression returns Expression:
	AdditiveExpression (
		=>({InstanceOfExpression.left=current} 'instanceof' type=[ecore::EObject|ID]) |
		=>({BinaryOperation.left=current} op=OpCompare) right=AdditiveExpression
	)*
;

OpCompare:
	'>=' | '<' '=' | '>' | '<' ;

AdditiveExpression returns Expression:
	MultiplicativeExpression (=>({BinaryOperation.left=current} op=OpAdd) right=MultiplicativeExpression)*
;

OpAdd:
	'+' | '-'
;

MultiplicativeExpression returns Expression:
	ExponentExpression (=>({BinaryOperation.left=current} op=OpMul) right=ExponentExpression)*
;

OpMul:
	'*' | '/'
;

ExponentExpression returns Expression:
	UnaryOrPrimitivExpression
 	(=>({BinaryOperation.left=current} op=OpExponent) right=ExponentExpression)?;

OpExponent:
	'**';

UnaryOrPrimitivExpression returns Expression
	: PostfixOperation
	| UnaryOperation
; 
	
UnaryOperation returns Expression:
	({UnaryOperation} op=OpUnary operand=PrimitivExpression)
;

OpUnary:
	'!' | '-'
;

PostfixOperation returns Expression:
	PrimitivExpression =>({PostfixOperation.operand=current} op=OpPostfix)?
;

OpPostfix:
	'++' | '--'
;


PrimitivExpression returns Expression:
	LiteralExpression |
	ParenthesizedExpression 
;

...


With this grammar it's not possible to parse expressions like -6-7 and so far I have no idea how to solve this issue.

Has someone an idea to fix this?

I also had a look to the b3-project (https://github.com/eclipse/b3/blob/master/org.eclipse.b3.beelang/src/org/eclipse/b3/BeeLang.xtext), maybe I'm wrong, but I would say this grammar has the same problem or not?

Many thanks and have a nice day!

[Updated on: Wed, 21 February 2018 10:17]

Report message to a moderator

Re: Allow negative unary expression in combination with subtraction [message #1782287 is a reply to message #1782285] Wed, 21 February 2018 10:45 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6040
Registered: July 2009
Senior Member
Hi

The OCL grammar at GIT\org.eclipse.ocl\plugins\org.eclipse.ocl.xtext.essentialocl\src\org\eclipse\ocl\xtext\essentialocl\EssentialOCL.xtext solves the problem. The critical recursive rules are:

// An ExpCS permits a LetExpCS only in the final term to ensure
// that let is right associative, whereas infix operators are left associative.
// a = 64 / 16 / let b : Integer in 8 / let c : Integer in 4
// is
// a = (64 / 16) / (let b : Integer in 8 / (let c : Integer in 4 ))
/* An expression elaborates a prefixed expression with zero or more binary operator and expression suffixes.
* An optionally prefixed let expression is permitted except when suffixed with further expressions.*/
ExpCS returns ExpCS:
// ({InfixExpCS} ownedSource=PrefixedExpCS name=BinaryOperatorName ownedArgument=ExpCS)
//| PrefixedExpCS
// the above takes exponential or worse time for backtracking, below is fast
(PrefixedPrimaryExpCS ({InfixExpCS.ownedLeft=current} name=BinaryOperatorName ownedRight=ExpCS)?)
| PrefixedLetExpCS;

/* A prefixed let expression elaborates a let expression with zero or more unary prefix operators. */
PrefixedLetExpCS returns ExpCS:
({PrefixExpCS} name=UnaryOperatorName ownedRight=PrefixedLetExpCS)
| LetExpCS;

/* A prefixed primary expression elaborates a primary expression with zero or more unary prefix operators. */
PrefixedPrimaryExpCS returns ExpCS:
({PrefixExpCS} name=UnaryOperatorName ownedRight=PrefixedPrimaryExpCS)
| PrimaryExpCS;

Binary operator precedence is not performed in the grammar, rather by semantic resolution later.

You won't need the extra complexity that OCL's very badly behaved "let ... in ..." construct imposes.

Regards

Ed Willink

Re: Allow negative unary expression in combination with subtraction [message #1782288 is a reply to message #1782287] Wed, 21 February 2018 10:58 Go to previous messageGo to next message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
Hi Ed,

thanks! But there is no possibility to solve this issue with binary operator precedence in the grammar?
Re: Allow negative unary expression in combination with subtraction [message #1782291 is a reply to message #1782288] Wed, 21 February 2018 11:45 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6040
Registered: July 2009
Senior Member
Hi

It should be possible to use binary operator precedence.

I factored it out for OCL, because it changed between OCL 2.0/2.2 motivating flexibility as well as extensibility. Also because the 'let...in...' complexity required everything to be defined twice.

Oops. I forget. The Eclipse OCL grammar inverts the binary/unary precedence. The parse tree is therefore deterministically 'wrong'. I therefore have a suite of helper functions to access the logically left/right sibling/ancestor/child while converting the Xtext-friendly parsed Concrete Syntax to the OMG-defined Abstract Syntax.

Regards

Ed Willink
Re: Allow negative unary expression in combination with subtraction [message #1782293 is a reply to message #1782291] Wed, 21 February 2018 11:51 Go to previous messageGo to next message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
Hi Ed,

what did you mean with it inverts the binary and unary precedence?
So the precedence of sub/add, mul/div is higher than the precedence of the unary expression?

Would it be possible to adjust it?

[Updated on: Wed, 21 February 2018 11:55]

Report message to a moderator

Re: Allow negative unary expression in combination with subtraction [message #1782294 is a reply to message #1782293] Wed, 21 February 2018 12:13 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6040
Registered: July 2009
Senior Member
Hi

If you like using the Xtext grammar 'precedence' facilities it may be possible to do it 'right', but I have a strong dislike for such tweaks and backtracking, preferring an LALR sound solution.

IIRC the current solution arose after one of my users complained that parsing was really really slow for some autogenerated expressions that involved relatively simple but twenty term and expressions. It turned out that the backtracking that "get's it right" had an exponential cost.

Regards

Ed Willink
Re: Allow negative unary expression in combination with subtraction [message #1782298 is a reply to message #1782294] Wed, 21 February 2018 13:04 Go to previous messageGo to next message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
Hi Ed,

many thanks for these information's ;)
Re: Allow negative unary expression in combination with subtraction [message #1782343 is a reply to message #1782298] Thu, 22 February 2018 08:17 Go to previous message
Chris X is currently offline Chris XFriend
Messages: 60
Registered: November 2017
Member
I have to correct me! Now the grammar of my post works! I found the stupid mistake!
In my grammar a ID could start with a number and additionally in between it was allowed to use minus sign.
With the fix IDs have to start with letters it works!
Previous Topic:ImportURI is depricated?
Next Topic: implementing QualifiedNameProvider
Goto Forum:
  


Current Time: Sat Sep 22 06:56:03 GMT 2018

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

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

Back to the top