Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Grammar basics(Need to be able to specify one or zero only)
Grammar basics [message #1698696] Wed, 17 June 2015 10:26 Go to next message
Roy Stoliday is currently offline Roy StolidayFriend
Messages: 6
Registered: December 2013
Junior Member
Hi,

I'm very new to XText and I'm trying a few examples of my own to get to grips with grammar basics. Can anyone help with a grammar question?

I want to be able to specify that a language element can appear once only or zero times.

Here's my grammar so far:

Model:
test+=Fruit*;

Fruit:
Apple | Banana | Grape
;

Apple:
'Apple' '(' 'type' '=' type = ID ')'';'
;

Banana:
'Banana' '('green='green' '=' ('true'|'false') ')'';'
;

Grape:
{Grape}'Grape' '(' options+=GrapeOptions* ')'';'
;

GrapeOptions:
options=GrapeColour | GrapeMouldiness
;

GrapeColour:
colour='colour' '='('black'|'red'|'white')
;

GrapeMouldiness:
mouldy='mouldiness' '=' ('none'|'saggy'|'extreme')
;


The trouble is this allows me to do the following:
Grape(colour=black mouldiness=extreme colour=red);
I can specify the colour more than once. As many times as I like in fact.

Is there a way to specify that I want the GrapeColour and GrapeMouldiness to both be optional, but at most appear only once? The above example should really show an error for both 'colour=' because it should be possible to only specify it once.

Sorry I realise this probable very basic.
Re: Grammar basics [message #1698698 is a reply to message #1698696] Wed, 17 June 2015 10:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi the feature you actually are looking for is called unordered group

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Grammar basics [message #1698701 is a reply to message #1698698] Wed, 17 June 2015 11:06 Go to previous message
Roy Stoliday is currently offline Roy StolidayFriend
Messages: 6
Registered: December 2013
Junior Member
Very many thanks Christian. That was the only pointer I needed.

This proved to work for me.

Grape:
{Grape}'Grape' '(' options=GrapeOptions2 ')'';'
;

GrapeOptions2:
{GrapeOptions2Action}
(
(gcolour?=GrapeColour)?
& (gmould?=GrapeMouldiness)?
)
;
Previous Topic:Grammar problems, "No viable alternative at input..."
Next Topic:custom declarative scopes stop to work after dsl extend xbase
Goto Forum:
  


Current Time: Tue Mar 19 07:05:59 GMT 2024

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

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

Back to the top