Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » grammar help please
grammar help please [message #491862] Fri, 16 October 2009 09:12 Go to next message
Matt Biggs is currently offline Matt Biggs
Messages: 68
Registered: July 2009
Member
Please could someone help me with a grammar issue? I am trying to write a DSL for an xml based file.

The xml tags/elements only allow specific values so i have written these into the dsl rather than accepting any tag/element.

The problem i'm having is that i cannot get it to accept any character for the data block between the xml tags.

eg:

<constant name="variable1">THIS IS THE DATA I CANT ASSIGN</constant>


So far my dsl consists of:

Constant:
	'<constant' (attributes+=ConstantAttribute)* ('>' data=ID '</constant>' | '/>');

ConstantAttribute:
	name=ConstantAttributeToken '=' value=STRING;

ConstantAttributeToken:
	'name' | 'description';


I have tried assigning data to ID terminal so far, but one strange thing is that is fails if i use any of the tokens specified in 'ConstantAttributeToken'.

Is there a better way for me to write the DSL or how can i simply get it to read any character for the data?

hope this all makes sense

thanks
Re: grammar help please [message #492204 is a reply to message #491862] Mon, 19 October 2009 13:05 Go to previous messageGo to next message
Matt Biggs is currently offline Matt Biggs
Messages: 68
Registered: July 2009
Member
I believe i have managed to solve the problem of it accepting any character, by changing the grammar to that below, however if i use any of the tokens defined in 'ConstantAttributeToken' it still doesn't accept them within the 'data' assignment and i can't understand why.

Is this a bug or is my grammar wrong still? I thought that my ConstantAttributeToken counted as a data type rule and was therefore only valid in certain contexts?


Constant:
	'<constant' (attributes+=ConstantAttribute)* ('>' data=ConstantData '</constant>' | '/>');

ConstantData:
	ANY_OTHER | ID (ANY_OTHER | ID)*;

ConstantAttribute:
	name=ConstantAttributeToken '=' value=STRING;

ConstantAttributeToken:
	'name' | 'description';



Valid Example:

<constant name="variable1">Hello world</constant>


Invalid Example:

<constant name="variable1">Hello description</constant>
Re: grammar help please [message #492213 is a reply to message #491862] Mon, 19 October 2009 13:45 Go to previous messageGo to next message
Tim  is currently offline Tim
Messages: 21
Registered: August 2009
Junior Member
Maybe not the most beautifully way to do this but it works:
Constant:
	('<constant' (attributes+=ConstantAttribute)* ('>' data=ConstantData '</constant>' | '/>'))+;

ConstantData:
	ANY_OTHER | ID (ANY_OTHER | ID)*;

ConstantAttribute:
	('name=' | 'description=') value=STRING;


Not your 'description' should always have a '=' at the end and cause of this the invalid example becomes valid.
Re: grammar help please [message #492259 is a reply to message #492204] Mon, 19 October 2009 15:52 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian Zarnekow
Messages: 2908
Registered: July 2009
Senior Member
Hi Matt,

you could try to use the following pattern:

ConstantData:
(ANY_OTHER | ID | ConstantAttributeToken)+;
// list basically any lexer rule and keyword that may occur as
ConstantData

Please note, that lexer rules and keywords are still global thus a
keyword will never be parsed as an ID.

Hope that helps,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Matt Biggs schrieb:
> I believe i have managed to solve the problem of it accepting any
> character, by changing the grammar to that below, however if i use any
> of the tokens defined in 'ConstantAttributeToken' it still doesn't
> accept them within the 'data' assignment and i can't understand why.
> Is this a bug or is my grammar wrong still? I thought that my
> ConstantAttributeToken counted as a data type rule and was therefore
> only valid in certain contexts?
>
>
>
> Constant:
> '<constant' (attributes+=ConstantAttribute)* ('>' data=ConstantData
> '</constant>' | '/>');
>
> ConstantData:
> ANY_OTHER | ID (ANY_OTHER | ID)*;
>
> ConstantAttribute:
> name=ConstantAttributeToken '=' value=STRING;
>
> ConstantAttributeToken:
> 'name' | 'description';
>
>
>
> Valid Example:
>
> <constant name="variable1">Hello world</constant>
>
> Invalid Example:
>
> <constant name="variable1">Hello description</constant>
Re: grammar help please [message #492345 is a reply to message #492213] Tue, 20 October 2009 06:21 Go to previous message
Alexander Nittka is currently offline Alexander Nittka
Messages: 1156
Registered: July 2009
Senior Member
> ConstantAttribute:
> ('name=' | 'description=') value=STRING;

Of course the drawback is that having a space before "=" in a tag
becomes invalid and again "name=" will not be accepted in the data part.

Another thing to note is that all whitespaces will be missing in the
feature data as they are hidden. So if they are needed in the model
still a little more work is necessary.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Previous Topic:Grammar for SQL
Next Topic:Integrated M2M (model to model) transformations
Goto Forum:
  


Current Time: Fri Oct 24 16:55:56 GMT 2014

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

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