Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » two grammar rules should be independent(but actually they aren't)
two grammar rules should be independent [message #872519] Wed, 16 May 2012 09:06 Go to next message
Tim Student is currently offline Tim StudentFriend
Messages: 12
Registered: May 2012
Junior Member
Hi Team,

this is a snip out of my code, that makes some kind of trouble.

action:
	typ=Event	|
	typ=Ex	
	;

Event hidden(UTS_COMMENT):
	type='event' WS ('-d'|'-e') WS ID+ WS*
	;

Ex hidden(UTS_COMMENT):
	type='exit' (WS ('okay' | 'OKAY' | 'ERROR' | 'CRITERR' | 'TIMEOUT' | 'ABORT' | 'TRANS') WS* (value+=NEWmore_strings)?)?
	;


This part should recognize the following as an example:
event -d TESTEVENT
exit	OKAY
exit 	ERROR

This works so far. But I get into trouble, if the ID in the "Event" rule contains one of the values from the multiple choice String out of the "Ex" Rule.
From my Point of view these two rules are totaly independent, but they aren't anyway.

The following example should also be 'good', but it isn't recognized as good:
event -d OKAY
exit	OKAY
event -d ERROR
exit ERROR

Eclipse tells me:
required (...)+ loop did not match anything at input 'OKAY'
So in my opinion the 'OKAY' is not recognized as an ID, but as a string from the Ex rule, but why.

Thanks in advance.
Re: two grammar rules should be independent [message #872528 is a reply to message #872519] Wed, 16 May 2012 09:20 Go to previous messageGo to next message
Mikael Karpberg is currently offline Mikael KarpbergFriend
Messages: 8
Registered: April 2012
Location: Sweden
Junior Member
Hello, Tim!

What's happening is that all explicit strings you put into your grammar turn into keywords (including the 'e', if you implement doubles with scientific notation the way it's recommended, i.e. via datatype rules, so I made mine a terminal anyway).

I have the same problem with some of my grammar... I want to have a an identifier at a place in the grammar and then continue parsing down one path only if that identifier is a certain string, without it turning into a keyword, since the keyword can then not be used as an identifier in other places in the grammar. The reason in my cases is that I'm implementing an already designed DSL that's parsed in a more flexible framework elsewhere.

Anyway, I hope that at least helps you figure out what's going wrong. I'm afraid I don't have a solution for you, except this:

action:
	typ=Event	|
	typ=Ex	
	;

ExKeyword:
        key=('okay' | 'OKAY' | 'ERROR' | 'CRITERR' | 'TIMEOUT' | 'ABORT' | 'TRANS');

Event hidden(UTS_COMMENT):
	type='event' WS ('-d'|'-e') WS (ID | ExKeyword)+ WS*
	;

Ex hidden(UTS_COMMENT):
	type='exit' (WS ExKeyword WS* (value+=NEWmore_strings)?)?
	;


(untested)
Re: two grammar rules should be independent [message #872533 is a reply to message #872528] Wed, 16 May 2012 09:31 Go to previous messageGo to next message
Mikael Karpberg is currently offline Mikael KarpbergFriend
Messages: 8
Registered: April 2012
Location: Sweden
Junior Member
A question to the less newbie xtext hackers here:

Is there a way to solve this? I'm thinking something like....

FieldSection:
    section=ID '{'   ({ current.section=='special' =>} fields+=SpecialFields*) 
                   | ({ current.section=='odd' =>} fields+=OddFields*) 
                   | fields+=NormalFields*
    '}';
Re: two grammar rules should be independent [message #872691 is a reply to message #872528] Wed, 16 May 2012 15:51 Go to previous message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2012-16-05 11:20, Mikael Karpberg wrote:
> Hello, Tim!
>
> What's happening is that all explicit strings you put into your grammar
> turn into keywords (including the 'e', if you implement doubles with
> scientific notation the way it's recommended, i.e. via datatype rules,
> so I made mine a terminal anyway).
>
> I have the same problem with some of my grammar... I want to have a an
> identifier at a place in the grammar and then continue parsing down one
> path only if that identifier is a certain string, without it turning
> into a keyword, since the keyword can then not be used as an identifier
> in other places in the grammar. The reason in my cases is that I'm
> implementing an already designed DSL that's parsed in a more flexible
> framework elsewhere.
>
The standard solution would be to have a MyID that is ID | your keywords
and use that instead of ID.

MyID : ID | ExKeyword ;

You then need to take care of coloring of the ExKeywords when they are
used as identifiers.

> Anyway, I hope that at least helps you figure out what's going wrong.
> I'm afraid I don't have a solution for you, except this:
>
>
> action:
> typ=Event |
> typ=Ex
> ;
>
> ExKeyword:
> key=('okay' | 'OKAY' | 'ERROR' | 'CRITERR' | 'TIMEOUT' | 'ABORT' |
> 'TRANS');
>
> Event hidden(UTS_COMMENT):
> type='event' WS ('-d'|'-e') WS (ID | ExKeyword)+ WS*
> ;
>
Depending on the rest of the grammar the '-d' and '-e' may give you
problems. You could instead do '-' ID and validate that the ID is a 'd'
or an 'e'.

Regards
- henrik
Previous Topic:generation of an Xtext grammar
Next Topic:Validating unused declarations
Goto Forum:
  


Current Time: Tue Jul 07 17:14:25 GMT 2020

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

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

Back to the top