Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Single Character compete versus INT and ID
Single Character compete versus INT and ID [message #700135] Fri, 22 July 2011 21:54 Go to next message
Feriaman  is currently offline Feriaman Friend
Messages: 4
Registered: July 2011
Junior Member
Hello,

Make it short. I already got a grammar, that use those very common terminal rules for identifiers and integer :
terminal ID  : '^'?('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
terminal INT returns ecore::EInt: ('0'..'9')+;


Now i want to parse the following code that affect a value to some keyboard keys :
...
Keyboard :
A -> 1
b -> 4
5 -> 124
...


Unfortunately, if i declare that kind of rule :
terminal SINGLE_CHARACTER : ('a'..'z'|'A'..'Z'|'0'..'9');


Every single character word in my parsed text is recognized as a SINGLE_CHARACTER, so it doesn't match my grammar.
So something like :
int x;

or
toto = 4;

throw an error, because the "x" and the "4" are no more an ID (nor an INT) but SINGLE_CHARACTER

The only way i founded to avoid this problem is to change the language for :
Keyboard :
'A' -> 1
'b' -> 4
'5' -> 124

and so my rule becomes
terminal SINGLE_CHARACTER : '\''('a'..'z'|'A'..'Z'|'0'..'9')'\'';


Is there another way to do that without changing my the parsed language ?

Thanks for reading, thanks for the answers
Re: Single Character compete versus INT and ID [message #700146 is a reply to message #700135] Fri, 22 July 2011 22:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13887
Registered: July 2009
Senior Member
Hi,

using Xtext 2.0. what about terminal fragments

grammar org.xtext.example.mydsl1.MyDsl1 with org.eclipse.xtext.common.Terminals

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

generate myDsl1 "http://www.xtext.org/example/mydsl1/MyDsl1"

Model:
	greetings+=Greeting*;
	
Greeting:
	'Hello' name=ID value=INT x=X '!';
	

terminal fragment C	: ('a'..'z'|'A'..'Z');
terminal fragment D : ('0'..'9');
terminal X : (C | D);
terminal ID  		: '^'?(C|'_') (C|'_'|D)*;
terminal INT returns ecore::EInt: D+;


You may have to fix content assist

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Single Character compete versus INT and ID [message #700153 is a reply to message #700135] Fri, 22 July 2011 22:16 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
You can define a datatype that has a converter that only accepts a
single character and generates error if it gets something unacceptable.

Something like:

SingleCharacterString : INT | ID ;

And then implement:

@ValueConverter(rule = "SingleCharacterString")
public IValueConverter<String> MyApropriatelyNamedConverter() {
....
}

- henrik

On 7/22/11 11:54 PM, Feriaman wrote:
> Hello,
>
> Make it short. I already got a grammar, that use those very common
> terminal rules for identifiers and integer :
> terminal ID : '^'?('a'..'z'|'A'..'Z'|'_')
> ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
> terminal INT returns ecore::EInt: ('0'..'9')+;
>
> Now i want to parse the following code that affect a value to some
> keyboard keys :
> ...
> Keyboard :
> A -> 1
> b -> 4
> 5 -> 124
> ...
>
>
> Unfortunately, if i declare that kind of rule : terminal
> SINGLE_CHARACTER : ('a'..'z'|'A'..'Z'|'0'..'9');
>
> Every single character word in my parsed text is recognized as a
> SINGLE_CHARACTER, so it doesn't match my grammar.
> So something like : int x;
> or toto = 4;
> throw an error, because the "x" and the "4" are no more an ID (nor an
> INT) but SINGLE_CHARACTER
>
> The only way i founded to avoid this problem is to change the language
> for : Keyboard :
> 'A' -> 1
> 'b' -> 4
> '5' -> 124
>
> and so my rule becomes terminal SINGLE_CHARACTER :
> '\''('a'..'z'|'A'..'Z'|'0'..'9')'\'';
>
> Is there another way to do that without changing my the parsed language ?
>
> Thanks for reading, thanks for the answers
Re: Single Character compete versus INT and ID [message #700368 is a reply to message #700146] Sat, 23 July 2011 08:38 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Don't think that will work if you want an ID with a single character to
be recognized. A terminal fragment is just a short-hand notation afaik.

- henrik

On 7/23/11 12:22 AM, Christian Dietrich wrote:
> Hi,
>
> using Xtext 2.0. what about terminal fragments
>
>
> grammar org.xtext.example.mydsl1.MyDsl1 with
> org.eclipse.xtext.common.Terminals
>
> import "http://www.eclipse.org/emf/2002/Ecore" as ecore
>
> generate myDsl1 "http://www.xtext.org/example/mydsl1/MyDsl1"
>
> Model:
> greetings+=Greeting*;
>
> Greeting:
> 'Hello' name=ID value=INT x=X '!';
>
>
> terminal fragment C : ('a'..'z'|'A'..'Z');
> terminal fragment D : ('0'..'9');
> terminal X : (C | D);
> terminal ID : '^'?(C|'_') (C|'_'|D)*;
> terminal INT returns ecore::EInt: D+;
>
>
> You may have to fix content assist
>
> ~Christian
Re: Single Character compete versus INT and ID [message #700373 is a reply to message #700368] Sat, 23 July 2011 09:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13887
Registered: July 2009
Senior Member
@Hendrik, yes you are right, you'd still need a DataType rule as Replacement for the ID

XID : ID | X;


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Single Character compete versus INT and ID [message #700387 is a reply to message #700373] Sat, 23 July 2011 09:59 Go to previous message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 7/23/11 11:47 AM, Christian Dietrich wrote:
> @Hendrik, yes you are right, you'd still need a DataType rule as
> Replacement for the ID
>
> XID : ID | X;

You probably ment:
XID : ID | C ;

or a single digit would be an identifier ;)

- henrik
Previous Topic:[xtend] Generic return type for EOperation
Next Topic:Domain model: member access
Goto Forum:
  


Current Time: Wed Apr 21 06:07:10 GMT 2021

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

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

Back to the top