Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Keyword as ID (context sensitive)
Keyword as ID (context sensitive) [message #954726] Tue, 23 October 2012 07:44 Go to next message
Bryce Brinkley is currently offline Bryce BrinkleyFriend
Messages: 8
Registered: October 2011
Junior Member
Hello!

I am trying to implement a rather lengthy parameter file syntax in Xtext. I have defined a terminal for arbitrary IDs which looks like this:
terminal ID returns ecore::EString:
	('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '0'..'9' | '_' | '*' | '-' | ':' | '.')*;

This means an ID has to start with a letter or "_" and can then contain any number of letters, numbers, "_", "*" or "-".

Now I run into problems as soon as there comes an ID which is already defined as a keyword. The grammar contains around 270 keywords: For example words like "n", "Min", "Angle" or "Energy".
At some places I even _want_ an ID to be one of these keywords.

Is there an easy way to tell Xtext to ignore keywords at certain places and treat them as IDs? (enable context sensitive parsing) As far as I see it should be clear from the context at which places that is.

Thanks in advance!

B.
Re: Keyword as ID (context sensitive) [message #954759 is a reply to message #954726] Tue, 23 October 2012 08:15 Go to previous messageGo to next message
Vlad Dumitrescu is currently offline Vlad DumitrescuFriend
Messages: 431
Registered: July 2009
Location: Gothenburg
Senior Member
Hi!

You can use

KW: 'n' | 'Min' | 'Angle' | ...
NAME: ID | KW;

and instead of ID, use NAME.

best regards,
Vlad

[Updated on: Tue, 23 October 2012 08:15]

Report message to a moderator

Re: Keyword as ID (context sensitive) [message #954960 is a reply to message #954759] Tue, 23 October 2012 11:47 Go to previous messageGo to next message
Bryce Brinkley is currently offline Bryce BrinkleyFriend
Messages: 8
Registered: October 2011
Junior Member
Vlad: Thank you for the quick response.
It's not the nicest solution, but it works like that.
Many thanks!

B.

[Updated on: Tue, 23 October 2012 11:47]

Report message to a moderator

Re: Keyword as ID (context sensitive) [message #955965 is a reply to message #954960] Wed, 24 October 2012 06:11 Go to previous messageGo to next message
Markus Wolf is currently offline Markus WolfFriend
Messages: 153
Registered: July 2009
Senior Member
Hi, I have a similar problem.
The rules KW and NAME you state here, are they dataTypeRules or terminal rules?
Re: Keyword as ID (context sensitive) [message #955974 is a reply to message #955965] Wed, 24 October 2012 06:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
Data type rules

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Keyword as ID (context sensitive) [message #955975 is a reply to message #955974] Wed, 24 October 2012 06:23 Go to previous messageGo to next message
Markus Wolf is currently offline Markus WolfFriend
Messages: 153
Registered: July 2009
Senior Member
Thanks, it works.
Is there a cleaner solution? I mean I could stick with that, but I'm really new to Xtext and would like to learn bit more in detail.
Re: Keyword as ID (context sensitive) [message #955984 is a reply to message #955975] Wed, 24 October 2012 06:27 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14669
Registered: July 2009
Senior Member
Hi a bit more concise would be: myid: "mykw" | ID;
Never the less it is THE solution

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Keyword as ID (context sensitive) [message #957449 is a reply to message #955984] Thu, 25 October 2012 08:21 Go to previous message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2012-24-10 8:27, Christian Dietrich wrote:
> Hi a bit more concise would be: myid: "mykw" | ID;
> Never the less it is THE solution
>
Actually, it is THE solution under normal circumstances.

The alternative is to treat the problem as a lexical issue and implement
an external lexer. This way they grammar does not see keyword tokens
where it is not supposed to see them.

The lexer however has no notion of the grammar, so it has to be driven
by lexical content alone.

- henrik
Previous Topic:Xtext as Plug-In Editor
Next Topic:How to provide a different error message for 'Couldn't resolve reference to..' error
Goto Forum:
  


Current Time: Fri Apr 26 22:06:06 GMT 2024

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

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

Back to the top