Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Negative numbers in Xtext Arithmetic Sample
Negative numbers in Xtext Arithmetic Sample [message #647850] Mon, 10 January 2011 03:58 Go to next message
Aleks Mising name is currently offline Aleks Mising nameFriend
Messages: 8
Registered: November 2010
Junior Member
Hi guys,

I am working on a DSL which includes the whole Xtext arithmetic sample project.
What I wanted to change is to add negative numbers in the grammar so I basically changed the

terminal NUMBER returns ecore::EBigDecimal:
	('0'..'9')* ('.' ('0'..'9')+)?;


to

terminal NUMBER returns ecore::EBigDecimal:
	('-')?('0'..'9')* ('.' ('0'..'9')+)?;


This works fine, apart from a few specific situations:

Expressions such as

2 + 3
2+3
2*4
2 * 4
2 *4


seem to work and parse fine, regardless of the presence of spaces in the expression.

However, there's an obvious problem for subtraction. If I try something like

3 - 2


it's fine, but

3-2


breaks because it doesn't parse it as a subtraction, but I'm guessing rather gets "confused" and parses the expression "-2" as the negative number.


I hope the way I've described this is making sense. Any recommendations on how to work around this?

Thanks!
Re: Negative numbers in Xtext Arithmetic Sample [message #647922 is a reply to message #647850] Mon, 10 January 2011 13:23 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
You need to treat an unary minus as an expression instead of a terminal
negative value since its meaning changes depending on context.

- henrik

On 1/10/11 4:58 AM, Aleks wrote:
> Hi guys,
>
> I am working on a DSL which includes the whole Xtext arithmetic sample
> project.
> What I wanted to change is to add negative numbers in the grammar so I
> basically changed the
>
>
> terminal NUMBER returns ecore::EBigDecimal:
> ('0'..'9')* ('.' ('0'..'9')+)?;
>
>
> to
>
> terminal NUMBER returns ecore::EBigDecimal:
> ('-')?('0'..'9')* ('.' ('0'..'9')+)?;
>
>
> This works fine, apart from a few specific situations:
>
> Expressions such as
>
> 2 + 3
> 2+3
> 2*4
> 2 * 4
> 2 *4
>
>
> seem to work and parse fine, regardless of the presence of spaces in the
> expression.
>
> However, there's an obvious problem for subtraction. If I try something
> like
>
>
> 3 - 2
>
>
> it's fine, but
>
> 3-2
>
>
> breaks because it doesn't parse it as a subtraction, but I'm guessing
> rather gets "confused" and parses the expression "-2" as the negative
> number.
>
>
> I hope the way I've described this is making sense. Any recommendations
> on how to work around this?
>
> Thanks!
Re: Negative numbers in Xtext Arithmetic Sample [message #648689 is a reply to message #647850] Fri, 14 January 2011 03:16 Go to previous messageGo to next message
Aleks Mising name is currently offline Aleks Mising nameFriend
Messages: 8
Registered: November 2010
Junior Member
Thanks, I'll give it a try!
Re: Negative numbers in Xtext Arithmetic Sample [message #673380 is a reply to message #648689] Mon, 23 May 2011 13:21 Go to previous messageGo to next message
Aleks Mising name is currently offline Aleks Mising nameFriend
Messages: 8
Registered: November 2010
Junior Member
Guys, sorry to bump this thread back, this issue has come back again since I never actually managed to successfully implement it.
Does anybody have an example of the solution suggested by Henrik, based on the Xtext Arithmetic sample?


Or perhaps further hints to the final solution?

Thanks!
Re: Negative numbers in Xtext Arithmetic Sample [message #673382 is a reply to message #673380] Mon, 23 May 2011 13:30 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

use a datatype rule instead of a modified terminal rule.

PossiblySignedNumber returns ecore::EBigDecimal: '-'? NUMBER;

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Negative numbers in Xtext Arithmetic Sample [message #673399 is a reply to message #673380] Mon, 23 May 2011 13:33 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Something like:

LowerExpression returns pp::Expression:
UnaryOrHigherExpression ({LowerExpression.leftExpr=current}
opName = 'xxx'
rightExpr = UnaryOrHigherExpression
)*
;

UnaryOrHigherExpression returns Expression
: UnaryMinusExpression
| NotExpression
| HigherExpression
;

UnaryMinusExpression returns UnaryMinusExpression
: '-' expr = HigherExpression
;

NotExpression returns UnaryNotExpression
: '!' expr = HigherExpression
;

HigherExpression returns Expression
: ... function call, primary expression, etc..
;

- henrik

On 5/23/11 3:21 PM, Aleks wrote:
> Guys, sorry to bump this thread back, this issue has come back again
> since I never actually managed to successfully implement it. Does
> anybody have an example of the solution suggested by Henrik, based on
> the Xtext Arithmetic sample?
>
>
> Or perhaps further hints to the final solution?
>
> Thanks!
>
(no subject) [message #673402 is a reply to message #673380] Mon, 23 May 2011 13:30 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

use a datatype rule instead of a modified terminal rule.

PossiblySignedNumber returns ecore::EBigDecimal: '-'? NUMBER;

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Negative numbers in Xtext Arithmetic Sample [message #673403 is a reply to message #673380] Mon, 23 May 2011 13:33 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Something like:

LowerExpression returns pp::Expression:
UnaryOrHigherExpression ({LowerExpression.leftExpr=current}
opName = 'xxx'
rightExpr = UnaryOrHigherExpression
)*
;

UnaryOrHigherExpression returns Expression
: UnaryMinusExpression
| NotExpression
| HigherExpression
;

UnaryMinusExpression returns UnaryMinusExpression
: '-' expr = HigherExpression
;

NotExpression returns UnaryNotExpression
: '!' expr = HigherExpression
;

HigherExpression returns Expression
: ... function call, primary expression, etc..
;

- henrik

On 5/23/11 3:21 PM, Aleks wrote:
> Guys, sorry to bump this thread back, this issue has come back again
> since I never actually managed to successfully implement it. Does
> anybody have an example of the solution suggested by Henrik, based on
> the Xtext Arithmetic sample?
>
>
> Or perhaps further hints to the final solution?
>
> Thanks!
>
Re: Negative numbers in Xtext Arithmetic Sample [message #673932 is a reply to message #673382] Wed, 25 May 2011 08:10 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6405
Registered: July 2009
Senior Member
Hi,

When adding similar a terminal rule

terminal LONG returns ecore::ELong: '-'? ('0'..'9')+;

and run mwe2 I get an error message like this.

error(208):
.../org.ufacekit.qt.idl.dsl/src-gen/org/ufacekit/qt/idl/dsl/parser/antlr/internal/InternalQIdl.g:5971:1:
The following token definitions can never be matched because prior
tokens match the same input: RULE_INT

which makes sense because naturally this can also match the INT-Rule,
everything seems to work though in my editor.

As always my xtext-File is found in my git repo [1] if you need a bigger
context.

Tom

[1]https://github.com/tomsontom/model-qt/blob/master/modules/org.ufacekit.qt.idl.dsl/src/org/ufacekit/qt/idl/dsl/QIdl.xtext

Am 23.05.11 15:30, schrieb Alexander Nittka:
> Hi,
>
> use a datatype rule instead of a modified terminal rule.
>
> PossiblySignedNumber returns ecore::EBigDecimal: '-'? NUMBER;
>
> Alex
Re: Negative numbers in Xtext Arithmetic Sample [message #673941 is a reply to message #673932] Wed, 25 May 2011 08:41 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

please be aware of the essential difference between a terminal rule and a datatype rule.

terminal LONG returns ecore::ELong: '-'? ('0'..'9')+; //terminal rule
Long returns ecore::ELong: '-'? INT; //datatype rule

A datatype rule reuses existing terminal (and datatype rule) definitions. It can be used "context sensitive".
A terminal rule can conflict with other terminal rules and is used without context. The lexer simply chops the input file into tokens.

Thats why I said: use a datatype rule instead of a modified terminal rule.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: Negative numbers in Xtext Arithmetic Sample [message #674007 is a reply to message #673932] Wed, 25 May 2011 13:52 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Use only simple "core" terminals and then compose your other datatypes
using data rules with appropriate value conversions.

i.e.
Long : INT ;

- henrik

On 5/25/11 10:10 AM, Tom Schindl wrote:
> Hi,
>
> When adding similar a terminal rule
>
> terminal LONG returns ecore::ELong: '-'? ('0'..'9')+;
>
> and run mwe2 I get an error message like this.
>
> error(208):
> .../org.ufacekit.qt.idl.dsl/src-gen/org/ufacekit/qt/idl/dsl/parser/antlr/internal/InternalQIdl.g:5971:1:
> The following token definitions can never be matched because prior
> tokens match the same input: RULE_INT
>
> which makes sense because naturally this can also match the INT-Rule,
> everything seems to work though in my editor.
>
> As always my xtext-File is found in my git repo [1] if you need a bigger
> context.
>
> Tom
>
> [1]https://github.com/tomsontom/model-qt/blob/master/modules/org.ufacekit.qt.idl.dsl/src/org/ufacekit/qt/idl/dsl/QIdl.xtext
>
> Am 23.05.11 15:30, schrieb Alexander Nittka:
>> Hi,
>>
>> use a datatype rule instead of a modified terminal rule.
>>
>> PossiblySignedNumber returns ecore::EBigDecimal: '-'? NUMBER;
>>
>> Alex
>
Re: Negative numbers in Xtext Arithmetic Sample [message #674016 is a reply to message #674007] Wed, 25 May 2011 14:20 Go to previous messageGo to next message
Aleks Mising name is currently offline Aleks Mising nameFriend
Messages: 8
Registered: November 2010
Junior Member
Thanks guys,

worked like a charm! Smile


[Updated on: Wed, 25 May 2011 14:21]

Report message to a moderator

Re: Negative numbers in Xtext Arithmetic Sample [message #674127 is a reply to message #674007] Wed, 25 May 2011 21:30 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6405
Registered: July 2009
Senior Member
Well I'm going to need XBase stuff which itself uses
org.eclipse.xtext.common.Terminals so this does not work for me.

Tom

Am 25.05.11 15:52, schrieb Henrik Lindberg:
> Use only simple "core" terminals and then compose your other datatypes
> using data rules with appropriate value conversions.
>
> i.e.
> Long : INT ;
>
> - henrik
>
> On 5/25/11 10:10 AM, Tom Schindl wrote:
>> Hi,
>>
>> When adding similar a terminal rule
>>
>> terminal LONG returns ecore::ELong: '-'? ('0'..'9')+;
>>
>> and run mwe2 I get an error message like this.
>>
>> error(208):
>> .../org.ufacekit.qt.idl.dsl/src-gen/org/ufacekit/qt/idl/dsl/parser/antlr/internal/InternalQIdl.g:5971:1:
>>
>> The following token definitions can never be matched because prior
>> tokens match the same input: RULE_INT
>>
>> which makes sense because naturally this can also match the INT-Rule,
>> everything seems to work though in my editor.
>>
>> As always my xtext-File is found in my git repo [1] if you need a bigger
>> context.
>>
>> Tom
>>
>> [1]https://github.com/tomsontom/model-qt/blob/master/modules/org.ufacekit.qt.idl.dsl/src/org/ufacekit/qt/idl/dsl/QIdl.xtext
>>
>>
>> Am 23.05.11 15:30, schrieb Alexander Nittka:
>>> Hi,
>>>
>>> use a datatype rule instead of a modified terminal rule.
>>>
>>> PossiblySignedNumber returns ecore::EBigDecimal: '-'? NUMBER;
>>>
>>> Alex
>>
>
Re: Negative numbers in Xtext Arithmetic Sample [message #1781346 is a reply to message #674127] Tue, 06 February 2018 09:46 Go to previous message
Martin Trummer is currently offline Martin TrummerFriend
Messages: 17
Registered: December 2017
Junior Member
I am now using this:
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
SignedNumber returns ecore::EBigDecimal: ('+' | '-')? Number;

so I can use + and - as signum and EBigDecimal, so that I can use it for integral and decimal types
Number is defined in XBase
Number hidden():
	HEX | (INT | DECIMAL) ('.' (INT | DECIMAL))?;
terminal HEX:
	('0x'|'0X') ('0'..'9'|'a'..'f'|'A'..'F'|'_')+
	('#' (('b'|'B')('i'|'I') | ('l'|'L')))?;
terminal INT returns ecore::EInt:
	'0'..'9' ('0'..'9'|'_')*;
terminal DECIMAL:
	INT
	(('e'|'E') ('+'|'-')? INT)?
	(('b'|'B')('i'|'I'|'d'|'D') | ('l'|'L'|'d'|'D'|'f'|'F'))?;
Previous Topic:How to generate a factory?
Next Topic:Runtime modification of Scoping
Goto Forum:
  


Current Time: Thu Aug 16 02:32:29 GMT 2018

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

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

Back to the top