Home » Modeling » TMF (Xtext) » Unary Minus in Expressions(-1 - - -1)
Unary Minus in Expressions [message #759297] |
Mon, 28 November 2011 09:55 |
Moritz 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 |
Meinte Boersma Messages: 434 Registered: July 2009 Location: Leiden, Netherlands |
Senior Member |
|
|
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.
Xtext blogs: executable models...again? | workshop material | custom scoping with Xtend
|
|
|
Re: Unary Minus in Expressions [message #759333 is a reply to message #759297] |
Mon, 28 November 2011 12:14 |
Sebastian Zarnekow 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 |
Sebastian Zarnekow 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 |
Henrik Lindberg 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
|
|
| | | | |
Goto Forum:
Current Time: Fri Sep 20 09:58:37 GMT 2024
Powered by FUDForum. Page generated in 0.03686 seconds
|