Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Unary Minus in Expressions(-1 - - -1)
Unary Minus in Expressions [message #759297] Mon, 28 November 2011 09:55 Go to next message
Moritz   is currently offline Moritz Friend
Messages: 22
Registered: July 2011
Junior Member
Hi,

My grammar shall parse expressions of the form "a * -b ---3", similar to arithmetic expressions in java.
The binary operators and their precedence work pretty good already, but I can't find a way to implement the unary minus properly. This minimal example is the closest I could come:

AdditiveExpression returns BExpression :
	UnaryOrPrimaryExpression
	({BBinaryOpExpression.leftExpr=current} functionName=("+" | "-") rightExpr=UnaryOrPrimaryExpression)*
	;

UnaryOrPrimaryExpression returns BExpression
	: PostopExpression 
	| UnaryExpression
	; 

UnaryExpression returns BExpression : {BUnaryOpExpression} 
	functionName="-" expr=PrimaryExpression
	;

PostopExpression returns BExpression :
	PrimaryExpression ({BUnaryPostOpExpression.expr=current} functionName = ("++"))?
	;
		
PrimaryExpression returns BExpression
	: ValueLiteral
	;

ValueLiteral :
	value = INT
	;


This already parses "-1--1" to "(-1)-(-1)", which is quite good, but not good enough:
The input can contain strings with multiple minuses as "-1-----1". This is possible because I have no Pre- or Postoperators like -- and ++.
The second problem with this grammar is, that I currently need to keep the PostopExpression to be able to parse the minus sign correctly at all.

The shown solution was proposed in older posts, but I just can't get them to run anymore (using Xtext 2.1)! I also strapped down the Expression part of the B3 Grammar (nice sample!), but also this grammar can't be build by a standard MWE2 file.

Do you have any hints on how to solve this? Let's say all I want is an expression with +, - and unary minus(es).

Thank you,
Moritz
Re: Unary Minus in Expressions [message #759331 is a reply to message #759297] Mon, 28 November 2011 12:09 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
Quote:
My grammar shall parse expressions...

That's the spirit! Smile

The essence of these older posts was to take care of the consequences of the differences between datatype and parser rules (which are called during parsing, after lexing, and therefore have some context) and terminals/lexing (which happens context-free, before the actual parsing, and just 'gobbles up' the input as greedily as it can). You left out the precise terminals, but I guess you re-use common.Terminals. In that case, "-1-----1" is lexed as: '-', INT(1), '-' (x5), INT(1).

Rewriting as follows should do the trick:
UnaryOrPrimaryExpression returns BExpression
	: PrimaryExpression // call next precedence level
	| UnaryExpression
	; 

UnaryExpression returns BExpression : {BUnaryOpExpression} 
	functionName="-" expr=UnaryExpression // call itself, making unitary minus left-associative (instead of non-associative)
	;

See http://dslmeinte.wordpress.com/2011/03/21/pre-and-postfix-operators-in-xtext/ for more explanation.


Re: Unary Minus in Expressions [message #759333 is a reply to message #759297] Mon, 28 November 2011 12:14 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Moritz,

this should do the trick:

UnaryExpression returns BExpression : {BUnaryOpExpression}
functionName="-" expr=UnaryOrPrimaryExpression // <- see here
;

Regards,
Sebastian

Am 28.11.11 10:55, schrieb Moritz:
> Hi,
>
> My grammar shall parse expressions of the form "a * -b ---3", similar to
> arithmetic expressions in java.
> The binary operators and their precedence work pretty good already, but
> I can't find a way to implement the unary minus properly. This minimal
> example is the closest I could come:
>
>
> AdditiveExpression returns BExpression :
> UnaryOrPrimaryExpression
> ({BBinaryOpExpression.leftExpr=current} functionName=("+" | "-")
> rightExpr=UnaryOrPrimaryExpression)*
> ;
>
> UnaryOrPrimaryExpression returns BExpression
> : PostopExpression | UnaryExpression
> ;
> UnaryExpression returns BExpression : {BUnaryOpExpression}
> functionName="-" expr=PrimaryExpression
> ;
>
> PostopExpression returns BExpression :
> PrimaryExpression ({BUnaryPostOpExpression.expr=current} functionName =
> ("++"))?
> ;
>
> PrimaryExpression returns BExpression
> : ValueLiteral
> ;
>
> ValueLiteral :
> value = INT
> ;
>
>
> This already parses "-1--1" to "(-1)-(-1)", which is quite good, but not
> good enough:
> The input can contain strings with multiple minuses as "-1-----1". This
> is possible because I have no Pre- or Postoperators like -- and ++.
> The second problem with this grammar is, that I currently need to keep
> the PostopExpression to be able to parse the minus sign correctly at all.
>
> The shown solution was proposed in
> http://www.eclipse.org/forums/index.php/mv/msg/202744/673403/#msg_673403
> http://www.eclipse.org/forums/index.php/mv/msg/202027/645570/#msg_645570, but
> I just can't get them to run anymore (using Xtext 2.1)! I also strapped
> down the Expression part of the
> http://git.eclipse.org/c/b3/b3.git/tree/org.eclipse.b3.beelang/src/org/eclipse/b3/BeeLang.xtext
> (nice sample!), but also this grammar can't be build by a standard MWE2
> file.
>
> Do you have any hints on how to solve this? Let's say all I want is an
> expression with +, - and unary minus(es).
>
> Thank you,
> Moritz


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Unary Minus in Expressions [message #759336 is a reply to message #759331] Mon, 28 November 2011 12:18 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Meinte,

this won't work since

UnaryExpression returns BExpression : {BUnaryOpExpression}
functionName="-" expr=UnaryExpression

will recurse endlessly and finally fail when all leading '-' were consumed.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


Am 28.11.11 13:09, schrieb Meinte Boersma:
> Quote:
>> My grammar shall parse expressions...
>
> That's the spirit! :)
>
> The essence of these older posts was to take care of the consequences of
> the differences between datatype and parser rules (which are called
> during parsing, after lexing, and therefore have some context) and
> terminals/lexing (which happens context-free, before the actual parsing,
> and just 'gobbles up' the input as greedily as it can). You left out the
> precise terminals, but I guess you re-use common.Terminals. In that
> case, "-1-----1" is lexed as: '-', INT(1), '-' (x5), INT(1).
>
> Rewriting as follows should do the trick:
>
> UnaryOrPrimaryExpression returns BExpression
> : PrimaryExpression // call next precedence level
> | UnaryExpression
> ;
> UnaryExpression returns BExpression : {BUnaryOpExpression}
> functionName="-" expr=UnaryExpression // call itself, making unitary
> minus left-associative (instead of non-associative)
> ;
>
> See
> http://dslmeinte.wordpress.com/2011/03/21/pre-and-postfix-operators-in-xtext/
> for more explanation.
Re: Unary Minus in Expressions [message #759339 is a reply to message #759297] Mon, 28 November 2011 12:20 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
What happens if you use:

UnaryOrPrimaryExpression returns BExpression
: PrimaryExpression | UnaryExpression
;
UnaryExpression returns BExpression : {BUnaryOpExpression}
functionName="-" expr=PrimaryExpression
;

PrimaryExpression returns BExpression
: ValueLiteral
;

Don't see why you would need the PostOp Rule.

The reason multipe '-' does not work is probably because the UnaryOp
only accepts a PrimaryExpression as the RHS. Did you try something like:

UnaryExpression returns BExpression : {BUnaryOpExpression}
functionName="-" expr=UnaryOrPrimaryExpression
;

Regards
- henrik
On 2011-28-11 10:55, Moritz wrote:
> Hi,
>
> My grammar shall parse expressions of the form "a * -b ---3", similar to
> arithmetic expressions in java.
> The binary operators and their precedence work pretty good already, but
> I can't find a way to implement the unary minus properly. This minimal
> example is the closest I could come:
>
>
> AdditiveExpression returns BExpression :
> UnaryOrPrimaryExpression
> ({BBinaryOpExpression.leftExpr=current} functionName=("+" | "-")
> rightExpr=UnaryOrPrimaryExpression)*
> ;
>
> UnaryOrPrimaryExpression returns BExpression
> : PostopExpression | UnaryExpression
> ;
> UnaryExpression returns BExpression : {BUnaryOpExpression}
> functionName="-" expr=PrimaryExpression
> ;
>
> PostopExpression returns BExpression :
> PrimaryExpression ({BUnaryPostOpExpression.expr=current} functionName =
> ("++"))?
> ;
>
> PrimaryExpression returns BExpression
> : ValueLiteral
> ;
>
> ValueLiteral :
> value = INT
> ;
>
>
> This already parses "-1--1" to "(-1)-(-1)", which is quite good, but not
> good enough:
> The input can contain strings with multiple minuses as "-1-----1". This
> is possible because I have no Pre- or Postoperators like -- and ++.
> The second problem with this grammar is, that I currently need to keep
> the PostopExpression to be able to parse the minus sign correctly at all.
>
> The shown solution was proposed in
> http://www.eclipse.org/forums/index.php/mv/msg/202744/673403/#msg_673403
> http://www.eclipse.org/forums/index.php/mv/msg/202027/645570/#msg_645570, but
> I just can't get them to run anymore (using Xtext 2.1)! I also strapped
> down the Expression part of the
> http://git.eclipse.org/c/b3/b3.git/tree/org.eclipse.b3.beelang/src/org/eclipse/b3/BeeLang.xtext
> (nice sample!), but also this grammar can't be build by a standard MWE2
> file.
>
> Do you have any hints on how to solve this? Let's say all I want is an
> expression with +, - and unary minus(es).
>
> Thank you,
> Moritz
Re: Unary Minus in Expressions [message #759340 is a reply to message #759336] Mon, 28 November 2011 12:27 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
Sebastian Zarnekow wrote on Mon, 28 November 2011 13:18

this won't work since

UnaryExpression returns BExpression : {BUnaryOpExpression}
functionName="-" expr=UnaryExpression

will recurse endlessly and finally fail when all leading '-' were consumed.

Hey, my coffee hadn't incubated yet, I was close enough and I wanted to take advantage of the apparent absence of Christian Dietrich since he's usually just 1 minute quicker than me Wink


Re: Unary Minus in Expressions [message #759513 is a reply to message #759297] Mon, 28 November 2011 21:04 Go to previous messageGo to next message
Moritz   is currently offline Moritz Friend
Messages: 22
Registered: July 2011
Junior Member
Meinte, Sebastian, Henrik, thank you very much!

You were right, I didn't figure out the correct rule call in the UnaryExpression.
UnaryOrPrimaryExpression returns BExpression
	: PrimaryExpression 
	| UnaryExpression
	; 

UnaryExpression returns BExpression : {BUnaryOpExpression} 
	functionName="-" expr=UnaryOrPrimaryExpression
	;


Best regards,
Moritz
Re: Unary Minus in Expressions [message #759514 is a reply to message #759331] Mon, 28 November 2011 21:10 Go to previous messageGo to next message
Moritz   is currently offline Moritz Friend
Messages: 22
Registered: July 2011
Junior Member
Meinte Boersma wrote on Mon, 28 November 2011 07:09

See http://dslmeinte.wordpress.com/2011/03/21/pre-and-postfix-operators-in-xtext/ for more explanation.


Good link - I couldn't find it while searching google for expression, xtext, unary operator, precedence. Maybe a new keyword would lead other people to your post..
Re: Unary Minus in Expressions [message #759518 is a reply to message #759514] Mon, 28 November 2011 21:49 Go to previous message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
I tagged the post with those keywords now. They should show up in a Google search as soon as the crawler has been over it. Thanks for the tip! Smile

Previous Topic:Problem in Scoping with getting left side of a XBinaryOperation
Next Topic:Autocompletion doesn't work in Xtend editor
Goto Forum:
  


Current Time: Thu Mar 28 14:40:03 GMT 2024

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

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

Back to the top