Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Grammar help with white spaces
Grammar help with white spaces [message #553826] Thu, 19 August 2010 06:03 Go to next message
daniel gatis is currently offline daniel gatisFriend
Messages: 3
Registered: August 2010
Junior Member
hi folks,

I need help to write a grammar to parser a dsl like the following one:

As a Google User
I want to search Google
So that I can test it


Reserved keywords:
As a / I want to / So that

so my dsl have this template:

As a <actor>
I want to <what i want to do>
So that <what i want to be done>

my Xtext grammar is this:

grammar org.xtext.example.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/MyDsl"
Model:
story=Story;

Story:
"As a" actor=StringWithOutQuotes
"I want to" want=StringWithOutQuotes
"So that" that=StringWithOutQuotes

StringWithOutQuotes:
<i need something to put here>



thanks
Re: Grammar help with white spaces [message #553852 is a reply to message #553826] Thu, 19 August 2010 07:22 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

try the following

grammar org.xtext.example.mydsl.MyDsl with
org.eclipse.xtext.common.Terminals hidden(ML_COMMENT,SL_COMMENT,WS,NL)

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
story=Story;

Story:
"As" "a" actor=StringWithOutQuotes
"I" "want" "to" want=StringWithOutQuotes
"So" "that" that=StringWithOutQuotes;

StringWithOutQuotes hidden(): (WS|ID|INT|STRING|ANY_OTHER|"I")+;

terminal WS: (' '|'\t')+;
terminal NL: ('\r'|'\n')+;


The idea is to split new line and white space definition, so that they
can be distinguished. By default, comments new lines and white spaces
are hidden. The StringWithoutQuotes definition wants to see all tokens
(hence hidden() = none hidden). And it allows all terminal rules except
comments and new lines. The problem is that you also have to include all
keywords you want to allow in the list.

Note, keywords containing spaces cause problems. If you want better code
completion use a template with variables for the whole story.

(As a <variableOne>
I want <variableTwo>
So that <variableThree>)

You will have to adapt the highlighting, as keywords are keywords
wherever they appear, that is also within a StringWithoutQuotes.

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
Re: Grammar help with white spaces [message #554239 is a reply to message #553852] Fri, 20 August 2010 17:26 Go to previous messageGo to next message
daniel gatis is currently offline daniel gatisFriend
Messages: 3
Registered: August 2010
Junior Member
First tks for quickly reply alex.

I fix my grammar for this:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

import "http://www.eclipse.org/emf/2002/Ecore" as ecore
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Test:
	story=Story NL*;
	
Story:
	"As" "a" actor=StringWithOutQuotes NL
	"I" "want" "to" what=StringWithOutQuotes NL
	"So" "that" that=StringWithOutQuotes;
    
terminal WS:
	(' ' | '\t')+;

terminal NL:
	('\r' | '\n')+;

StringWithOutQuotes returns ecore::EString hidden():
	(WS | ID | INT | STRING | ANY_OTHER | "As" | "a" | "I" | "want" | "to" | "So" | "that")+; 


So i need to add more rules to my grammar for its parse Scenarios compcepts like this:

As a foo bar
I want to foo bar
So that for bar

Scenario X - foo bar
Given
    foo foo foo 
    bar bar bar
When
    foo foo foo
    bar bar bar
Then
    foo foo foo
    bar bar bar


can you show me what i need to do for my grammar parse it?!


tks
Re: Grammar help with white spaces [message #554248 is a reply to message #554239] Fri, 20 August 2010 18:10 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Not really. That is, of course I can give you a grammar (Model: stuff+=(<pipeSeparatedListOfAllTerminalRulesThereAre>)*;) that parses this input without syntax errors, but this is hardly what you want.
The point is that the grammar also defines the model that is instantiated from the text and without knowing what you want to achieve, what the expected syntax is, what you want it to mean, how you want to process the model afterwards, which relation the elements are in etc., it does not make sense to give suggestions.

The patterns that you seem to need are already covered in the grammar you have, can be found in the documentation or the examples shipped with Xtext.

Alex
Re: Grammar help with white spaces [message #554249 is a reply to message #554248] Fri, 20 August 2010 18:27 Go to previous message
daniel gatis is currently offline daniel gatisFriend
Messages: 3
Registered: August 2010
Junior Member
ok. i will read the docs and try do it.

tks again.
Previous Topic:input matching multiple rules
Next Topic:[MWE2] Error message for unresolved platform URIs
Goto Forum:
  


Current Time: Tue Sep 24 03:35:31 GMT 2024

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

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

Back to the top