Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Difficulty with a grammar
Difficulty with a grammar [message #34045] Wed, 25 March 2009 22:40 Go to next message
Jonathan Whitall is currently offline Jonathan WhitallFriend
Messages: 18
Registered: July 2009
Junior Member
I'm running into a bit of trouble trying to create my DSL. In my case, I'm
trying to express than a Directions type can contain a list of
InstructionGroup types OR a list of Section types. I have tried to
represent it like this:

Directions :
'Directions' ':'?
(instructionGroups+=InstructionGroup)+ |
(instructionGroupContents+=InstructionGroupContents)+;

InstructionGroup :
'InstructionGroup' id=ID ':'?
(instructionGroupContents+=InstructionGroupContents)+;

InstructionGroupContents :
((sections+=Section)+);

Section :
'Section' ':'?;

but I get this error from Antlr when I go to generate:

warning(200):
../com.knitml.kel.grammar/src-gen/com/knitml/parser/antlr/in ternal/InternalKel.g:438:1:
Decision can match input such as "'Section'" using multiple alternatives:
1, 2
As a result, alternative(s) 2 were disabled for that input

Is there a better way to express this in the grammar so that I don't get
this warning?

I'm using XText with Eclipse 3.5M6 (the package downloaded from Itemis's
website yesterday evening.)
Re: Difficulty with a grammar [message #34091 is a reply to message #34045] Wed, 25 March 2009 23:26 Go to previous messageGo to next message
Heiko Behrens is currently offline Heiko BehrensFriend
Messages: 11
Registered: July 2009
Junior Member
Hi Jonathan,

great to see that Sven and Jan gained some attraction :)

Your Grammar

> InstructionGroup :
> 'InstructionGroup' id=ID ':'?
> (instructionGroupContents+=InstructionGroupContents)+;
>
> InstructionGroupContents :
> ((sections+=Section)+);
>
> Section :
> 'Section' ':'?;
>

Is ambiguous since the given input

InstructionGroup Foo :
Section S1
Section S2

Could either lead to

InstructionGroup (
InstructionContents (
Section
)
InstructionContents (
Section
)
)

or to

InstructionGroup (
InstructionContents (
Section
Section
)
)

It is best practice to start from a given sample and to derive the
needed grammar from this. If you could provide such a sample I would
be glad to assist you.

Regards,
Heiko
Re: Difficulty with a grammar [message #34125 is a reply to message #34091] Thu, 26 March 2009 03:57 Go to previous messageGo to next message
Jonathan Whitall is currently offline Jonathan WhitallFriend
Messages: 18
Registered: July 2009
Junior Member
Hi Heiko,

I'm trying (ultimately) to model this type of structure:

<Directions>
<InstructionGroup>
<Section/>
<Section/>
</InstructionGroup>
<InstructionGroup>
<Section/>
<Section/>
</InstructionGroup>
</Directions>

OR

<Directions>
<Section/>
<Section/>
<Section/>
<Section/>
</Directions>

That is to say, the higher-level InstructionGroup is completely optional,
depending on the specific pattern being created.
Re: Difficulty with a grammar [message #34159 is a reply to message #34045] Thu, 26 March 2009 06:57 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Jonathan,

seems to me, that the type InstructionGroupContents is superflous in
your grammar. The parser does not know, if the a section belongs to the
same InstructionGroupContents instance (is a child of), or if a new
InstructionGroupContent has to be instantiated for the section, that was
found.

Consider the following input:

Directions :
InstructionGroup :
Section : <- igc = new InstructionGroupContents() ..
Section : <- belongs this one to igc or igc2?

I would either introduce a keyword for InstrGroupContents or ommit it at
all (I'ld prefer the latter):

Directions:
'Directions' ':'? instructionGroups+=InstructionGroup+
| contents+=Section+;

InstructionGroup:
'InstructionGroup' id=ID ':'? contents+=Section+;

Section
'Section' ':'?;

Hope that helps,
Sebastian



Jonathan Whitall schrieb:
> I'm running into a bit of trouble trying to create my DSL. In my case,
> I'm trying to express than a Directions type can contain a list of
> InstructionGroup types OR a list of Section types. I have tried to
> represent it like this:
>
> Directions :
> 'Directions' ':'?
> (instructionGroups+=InstructionGroup)+ |
> (instructionGroupContents+=InstructionGroupContents)+;
>
> InstructionGroup :
> 'InstructionGroup' id=ID ':'?
> (instructionGroupContents+=InstructionGroupContents)+;
> InstructionGroupContents :
> ((sections+=Section)+);
> Section :
> 'Section' ':'?;
>
> but I get this error from Antlr when I go to generate:
>
> warning(200):
> ./com.knitml.kel.grammar/src-gen/com/knitml/parser/antlr/int ernal/InternalKel.g:438:1:
> Decision can match input such as "'Section'" using multiple
> alternatives: 1, 2
> As a result, alternative(s) 2 were disabled for that input
>
> Is there a better way to express this in the grammar so that I don't get
> this warning?
>
> I'm using XText with Eclipse 3.5M6 (the package downloaded from Itemis's
> website yesterday evening.)
>
Re: Difficulty with a grammar [message #34297 is a reply to message #34159] Thu, 26 March 2009 07:10 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Must have been to eager and missed Heikos post, who mentioned almost the
same ...

- Sebastian

Sebastian Zarnekow schrieb:
> Hi Jonathan,
>
> seems to me, that the type InstructionGroupContents is superflous in
> your grammar. The parser does not know, if the a section belongs to the
> same InstructionGroupContents instance (is a child of), or if a new
> InstructionGroupContent has to be instantiated for the section, that was
> found.
>
> Consider the following input:
>
> Directions :
> InstructionGroup :
> Section : <- igc = new InstructionGroupContents() ..
> Section : <- belongs this one to igc or igc2?
>
> I would either introduce a keyword for InstrGroupContents or ommit it at
> all (I'ld prefer the latter):
>
> Directions:
> 'Directions' ':'? instructionGroups+=InstructionGroup+
> | contents+=Section+;
>
> InstructionGroup:
> 'InstructionGroup' id=ID ':'? contents+=Section+;
>
> Section
> 'Section' ':'?;
>
> Hope that helps,
> Sebastian
>
>
>
> Jonathan Whitall schrieb:
>> I'm running into a bit of trouble trying to create my DSL. In my case,
>> I'm trying to express than a Directions type can contain a list of
>> InstructionGroup types OR a list of Section types. I have tried to
>> represent it like this:
>>
>> Directions :
>> 'Directions' ':'?
>> (instructionGroups+=InstructionGroup)+ |
>> (instructionGroupContents+=InstructionGroupContents)+;
>>
>> InstructionGroup :
>> 'InstructionGroup' id=ID ':'?
>> (instructionGroupContents+=InstructionGroupContents)+;
>> InstructionGroupContents :
>> ((sections+=Section)+);
>> Section :
>> 'Section' ':'?;
>>
>> but I get this error from Antlr when I go to generate:
>>
>> warning(200):
>> ./com.knitml.kel.grammar/src-gen/com/knitml/parser/antlr/int ernal/InternalKel.g:438:1:
>> Decision can match input such as "'Section'" using multiple
>> alternatives: 1, 2
>> As a result, alternative(s) 2 were disabled for that input
>>
>> Is there a better way to express this in the grammar so that I don't
>> get this warning?
>>
>> I'm using XText with Eclipse 3.5M6 (the package downloaded from
>> Itemis's website yesterday evening.)
>>
Re: Difficulty with a grammar [message #34365 is a reply to message #34125] Thu, 26 March 2009 09:08 Go to previous messageGo to next message
Heiko Behrens is currently offline Heiko BehrensFriend
Messages: 11
Registered: July 2009
Junior Member
Hey Jonathan,

this structure of the model can be built up with an Xtext parser that uses a
grammar similar to yours. Have a look at Sebastians suggestion. This should work
quite well in your case. In particular, take a closer look at his rule
InstructionGroup where he calls Section directly.

If you cannot do it without InstructionGroupContents since you want to compose
and reuse further features as your work continues, omit the +multiplicity when
calling the InstructionGroupContents:

Directions :
'Directions' ':'?
(instructionGroups+=InstructionGroup+ |
instructionGroupContents=InstructionGroupContents);

InstructionGroup :
'InstructionGroup' id=ID ':'?
instructionGroupContents+=InstructionGroupContents;

InstructionGroupContents :
sections+=Section+;

Section :
'Section' ':'?;


Does this solve your problem?

> Hi Heiko,
>
> I'm trying (ultimately) to model this type of structure:
>
> <Directions>
> <InstructionGroup>
> <Section/>
> <Section/>
> </InstructionGroup>
> <InstructionGroup>
> <Section/>
> <Section/>
> </InstructionGroup>
> </Directions>
>
> OR
>
> <Directions>
> <Section/>
> <Section/>
> <Section/>
> <Section/>
> </Directions>
>
> That is to say, the higher-level InstructionGroup is completely optional,
> depending on the specific pattern being created.
>
>
Re: Difficulty with a grammar [message #34467 is a reply to message #34159] Thu, 26 March 2009 15:51 Go to previous messageGo to next message
Jonathan Whitall is currently offline Jonathan WhitallFriend
Messages: 18
Registered: July 2009
Junior Member
Sebastian Zarnekow wrote:

> Directions:
> 'Directions' ':'? instructionGroups+=InstructionGroup+
> | contents+=Section+;

> InstructionGroup:
> 'InstructionGroup' id=ID ':'? contents+=Section+;

> Section
> 'Section' ':'?;

So I tried this format. The generator no longer complains, but when I go
to test the grammar, the validation doesn't recognize:

Directions:
Section:

as a valid grammar. It's still trying to insist that I have an
InstructionGroup first.
Re: Difficulty with a grammar [message #34501 is a reply to message #34467] Thu, 26 March 2009 16:40 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Jonathan,

the problem is, that Groups (lists of tokens, kind of invisible 'and'
operator) have a higher precedence than Alternatives (or operator).

You should use parenthesis around your alternative:


Directions:
'Directions' ':'? (
instructionGroups+=InstructionGroup+
| contents+=Section+
);

InstructionGroup:
'InstructionGroup' id=ID ':'? contents+=Section+;

Section
'Section' ':'?;

Regards,
Sebastian

Jonathan Whitall schrieb:
> Sebastian Zarnekow wrote:
>
>> Directions:
>> 'Directions' ':'? instructionGroups+=InstructionGroup+
>> | contents+=Section+;
>
>> InstructionGroup:
>> 'InstructionGroup' id=ID ':'? contents+=Section+;
>
>> Section
>> 'Section' ':'?;
>
> So I tried this format. The generator no longer complains, but when I go
> to test the grammar, the validation doesn't recognize:
>
> Directions:
> Section:
>
> as a valid grammar. It's still trying to insist that I have an
> InstructionGroup first.
>
Re: Difficulty with a grammar [message #34600 is a reply to message #34501] Fri, 27 March 2009 04:40 Go to previous message
Jonathan Whitall is currently offline Jonathan WhitallFriend
Messages: 18
Registered: July 2009
Junior Member
Ah, that works! Thanks!

I'm sure I'll have a lot more questions as I go through the rest of the
grammar.

Jonathan
Previous Topic:[TCS] source campatible with ATL 3.0
Next Topic:xtext interpreting examples
Goto Forum:
  


Current Time: Sat Jul 27 17:15:50 GMT 2024

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

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

Back to the top