Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » HOW DO YOU MODEL A DECIMAL? (I can't find a good way to construct a DECIMAL rule.)
HOW DO YOU MODEL A DECIMAL? [message #915322] Mon, 17 September 2012 03:48 Go to next message
JJ Mising name is currently offline JJ Mising name
Messages: 12
Registered: November 2010
Junior Member
I can't seem to find a way to model a decimal. These rules nearly work and I understand that there is an ambiguity with INT, but I am trying to create a decimal rule that would allow things like 0.1, 12, 23.234235 ...


terminal INT returns ecore::EInt:('0'..'9') | (('1'..'9') ('0'..'9')+ ) ;

DECIMAL returns ecore::EString : '-'? INT ('.' INT)? ;

POSINTNUMBER returns ecore::EString : INT ;

NEGINTNUMBER returns ecore::EString : '-' INT ;

INTNUMBER:
POSINTNUMBER | NEGINTNUMBER ;

Again I understand that my DECIMAL rule can evaluate to INT in the case of say '12', but it actually works. I do get the right value. The only time these rules don't work is when the value starts with '0' for instance 0.1 creates an error. I resulted in making sure INT would allow a single digit (see INT rule above), but that didn't change anything.

Could someone tell me what I am missing? Again, everything works perfectly except the case 0.XXX '12' and '23.234234' work just fine, negatives too, POSINT and NETINT work well.

thanks,

JJ-
Re: HOW DO YOU MODEL A DECIMAL? [message #915368 is a reply to message #915322] Mon, 17 September 2012 06:35 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4061
Registered: July 2009
Senior Member
Hi

If you're happy to model decimal as EString rather than EBigDecimal you
might just model aNumber and let the ValueConverters sort out the details.

Alternatively having INT return EString can give you more flexibility
over re-use again letting ValueConverters sort out the details.

[For OCL, where there are further ambiguities for e.g. "4..9" as a range
and "x.y" as a model navigation, the EssentialOCL grammar lies and
declares that a decimal is parsed as an INT, but actually a customized
TokenSource acts as a pre-lexer so that numeric '.' sequences appear to
Xtext as atomic tokens avoiding the need for clever disambiguation in
the subsequent rules.]

Regards

Ed Willink

On 17/09/2012 04:48, JJ Mising name wrote:
> I can't seem to find a way to model a decimal. These rules nearly work
> and I understand that there is an ambiguity with INT, but I am trying
> to create a decimal rule that would allow things like 0.1, 12,
> 23.234235 ...
>
>
> terminal INT returns ecore::EInt:('0'..'9') | (('1'..'9') ('0'..'9')+ ) ;
>
> DECIMAL returns ecore::EString : '-'? INT ('.' INT)? ;
>
> POSINTNUMBER returns ecore::EString : INT ;
>
> NEGINTNUMBER returns ecore::EString : '-' INT ;
>
> INTNUMBER:
> POSINTNUMBER | NEGINTNUMBER ;
>
> Again I understand that my DECIMAL rule can evaluate to INT in the
> case of say '12', but it actually works. I do get the right value. The
> only time these rules don't work is when the value starts with '0' for
> instance 0.1 creates an error. I resulted in making sure INT would
> allow a single digit (see INT rule above), but that didn't change
> anything.
> Could someone tell me what I am missing? Again, everything works
> perfectly except the case 0.XXX '12' and '23.234234' work just fine,
> negatives too, POSINT and NETINT work well.
> thanks,
>
> JJ-
Re: HOW DO YOU MODEL A DECIMAL? [message #915453 is a reply to message #915322] Mon, 17 September 2012 10:02 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik Lindberg
Messages: 2500
Registered: July 2009
Senior Member
I use:
terminal INT : ('0'..'9')+;
REAL hidden(): INT ? '.' (EXT_INT | INT);
terminal EXT_INT: INT ('e'|'E')('-'|'+') INT;

Note that REAL is not a terminal.

I also don't associate value converters with these as there are other
places where I need INT, REAL etc as strings. So I also have:

RealValue returns ecore::EDoubleObject: REAL ;

Hope that helps.

Regards
- henrik

On 2012-17-09 5:48, JJ Mising name wrote:
> I can't seem to find a way to model a decimal. These rules nearly work
> and I understand that there is an ambiguity with INT, but I am trying to
> create a decimal rule that would allow things like 0.1, 12, 23.234235 ...
>
>
> terminal INT returns ecore::EInt:('0'..'9') | (('1'..'9') ('0'..'9')+ ) ;
>
> DECIMAL returns ecore::EString : '-'? INT ('.' INT)? ;
>
> POSINTNUMBER returns ecore::EString : INT ;
>
> NEGINTNUMBER returns ecore::EString : '-' INT ;
>
> INTNUMBER:
> POSINTNUMBER | NEGINTNUMBER ;
>
> Again I understand that my DECIMAL rule can evaluate to INT in the case
> of say '12', but it actually works. I do get the right value. The only
> time these rules don't work is when the value starts with '0' for
> instance 0.1 creates an error. I resulted in making sure INT would allow
> a single digit (see INT rule above), but that didn't change anything.
> Could someone tell me what I am missing? Again, everything works
> perfectly except the case 0.XXX '12' and '23.234234' work just fine,
> negatives too, POSINT and NETINT work well.
> thanks,
>
> JJ-
Re: HOW DO YOU MODEL A DECIMAL? [message #915507 is a reply to message #915453] Mon, 17 September 2012 12:38 Go to previous message
JJ Mising name is currently offline JJ Mising name
Messages: 12
Registered: November 2010
Junior Member
Ed, Henrik:

thank you very much for your reply.

I guess my DECIMAL rule was interfering with an array declaration '0..'

I did get it to go one step further with Henrik's suggestion and support .2 for 0.2, but I preferred replacing the a

Yes, I like/can make everything a String, this has worked best for me.

thank you again,

JJ-
Previous Topic:xtext + tycho + xtext-utils unit testing fails
Next Topic:semantic quick fixes and commens
Goto Forum:
  


Current Time: Thu Oct 02 12:37:33 GMT 2014

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

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