Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Problems with unary ++, --
Problems with unary ++, -- [message #645550] Fri, 17 December 2010 19:55 Go to next message
drew frantz is currently offline drew frantzFriend
Messages: 340
Registered: July 2009
Senior Member
I am trying to build some rules for the java/C++ like ++,-- operators. That
I based loosely on the java 1.6 anltl grammar.

This works
int x=2;
x=++x;
x=--x;
x=x++;
x=x--;

but these do not
int x=2;
++x;
--x;
x++;
x--;

Has anyone built these kind of rules in their DSl's?
UnaryExpression ({ExpressionTree.leftExpr=current} operator=PowerOperator
rightExpr=UnaryExpression)*;


UnaryExpression returns Expression

: operator=AddOperator rightExpr=UnaryExpression

| operator=IncDecOperator rightExpr=UnaryExpression

| UnaryExpressionNotPlusMinus;



UnaryExpressionNotPlusMinus returns Expression

: operator=NotOperator rightExpr=UnaryExpression

| SimpleExpression ({ExpressionTree.leftExpr=current}
operator=IncDecOperator)?

;
Re: Problems with unary ++, -- [message #645556 is a reply to message #645550] Fri, 17 December 2010 23:27 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 25
Registered: April 2010
Junior Member
in the First example you provide:
int x=2;
x=++x;
x=--x;
x=x++;
x=x--;


The unary operators are parsed as expressions.

In the second, the parser probably expects (depends on the rest of the grammar which you havent shown) a statement. Did you make the unuary expressions a statement as well?

e.g.

Statement:
.... //your statements here
| UnaryExpression SEMI;
Re: Problems with unary ++, -- [message #645570 is a reply to message #645550] Sat, 18 December 2010 01:57 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Hi, here is the relevant part from the b3 grammar. The InfixExpression
is the next expression up the precedence chain...

UnaryOrInfixExpression returns be::BExpression
: PostopExpression
| UnaryExpression
| PreopExpression
;

UnaryExpression returns be::BExpression : {be::BUnaryOpExpression}
functionName=("!" | "-") expr=InfixExpression
;

PreopExpression returns be::BExpression : {be::BUnaryPreOpExpression}
functionName=("++" | "--") expr=InfixExpression
;

PostopExpression returns be::BExpression :
InfixExpression ({be::BUnaryPostOpExpression.expr=current} functionName
= ("--" | "++"))?
;

Regards
- henrik

On 12/17/10 8:55 PM, Drew wrote:
> I am trying to build some rules for the java/C++ like ++,-- operators. That
> I based loosely on the java 1.6 anltl grammar.
>
> This works
> int x=2;
> x=++x;
> x=--x;
> x=x++;
> x=x--;
>
> but these do not
> int x=2;
> ++x;
> --x;
> x++;
> x--;
>
> Has anyone built these kind of rules in their DSl's?
> UnaryExpression ({ExpressionTree.leftExpr=current} operator=PowerOperator
> rightExpr=UnaryExpression)*;
>
>
> UnaryExpression returns Expression
>
> : operator=AddOperator rightExpr=UnaryExpression
>
> | operator=IncDecOperator rightExpr=UnaryExpression
>
> | UnaryExpressionNotPlusMinus;
>
>
>
> UnaryExpressionNotPlusMinus returns Expression
>
> : operator=NotOperator rightExpr=UnaryExpression
>
> | SimpleExpression ({ExpressionTree.leftExpr=current}
> operator=IncDecOperator)?
>
> ;
>
>
Re: Problems with unary ++, -- [message #645606 is a reply to message #645556] Sat, 18 December 2010 17:25 Go to previous message
drew frantz is currently offline drew frantzFriend
Messages: 340
Registered: July 2009
Senior Member
Yes that was the issue. Thanks for the help.

<Wattos@gmail.com> wrote in message news:iegrca$qe3$1@news.eclipse.org...
> in the First example you provide:
>
> int x=2;
> x=++x;
> x=--x;
> x=x++;
> x=x--;
>
>
> The unary operators are parsed as expressions.
> In the second, the parser probably expects (depends on the rest of the
> grammar which you havent shown) a statement. Did you make the unuary
> expressions a statement as well?
> e.g.
>
> Statement:
> ... //your statements here
> | UnaryExpression SEMI;
>
Previous Topic:Macro's
Next Topic:Hidden production rules?
Goto Forum:
  


Current Time: Thu Apr 25 06:34:17 GMT 2024

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

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

Back to the top