Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Please help with simple grammar
icon5.gif  Please help with simple grammar [message #881089] Sun, 03 June 2012 21:51 Go to next message
Andrew A is currently offline Andrew AFriend
Messages: 9
Registered: June 2012
Junior Member
Hello all!

I have a grammar for a simple template language:

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

generate myDsl "xtext.org/example/mydsl/MyDsl"


Model:
	st+=Expr*;
	
Expr:
	Text | Var
;

Text: TXT;

Var: '${' IDENT '}'	
;

terminal
IDENT:
	('a'..'z')+
;

terminal
TXT:
	('a'..'z')*	
;


I expect that the following code should satisfy the grammar, but really it's not.
${abc}qwerty${test}


How to fix the grammar to satisfy the code?
Re: Please help with simple grammar [message #881098 is a reply to message #881089] Sun, 03 June 2012 22:31 Go to previous messageGo to next message
Oleg Bolshakov is currently offline Oleg BolshakovFriend
Messages: 36
Registered: August 2010
Member
Model:
	st+=Expr*;
	
Expr: Text | Var;

Text: r = rule2;

Var: r = rule1;

terminal rule1: '${' ('a'..'z')+ '}';

terminal rule2: ('a'..'z')*;
Your problem is that every sequence of letters os considered to be IDENT (because it's first), so nothing is for TXT terminal

[Updated on: Sun, 03 June 2012 22:32]

Report message to a moderator

Re: Please help with simple grammar [message #881726 is a reply to message #881098] Tue, 05 June 2012 06:58 Go to previous messageGo to next message
Andrew A is currently offline Andrew AFriend
Messages: 9
Registered: June 2012
Junior Member
Thanks, Oleg!

Your grammar works right, but how can I specify that the name of the variable is between '${'and '}' ?


something like
 
terminal rule1: '${' name=('a'..'z')+ '}';

but this syntax is invalid Sad

[Updated on: Tue, 05 June 2012 06:59]

Report message to a moderator

Re: Please help with simple grammar [message #881735 is a reply to message #881726] Tue, 05 June 2012 07:14 Go to previous messageGo to next message
Oleg Bolshakov is currently offline Oleg BolshakovFriend
Messages: 36
Registered: August 2010
Member
Do you need this grammar?
Model: st+=Expr*;
	
Expr: Text | Var;

Text: n = name;

Var: '${' n = name '}';

terminal name: ('a'..'z')+;
Re: Please help with simple grammar [message #881908 is a reply to message #881089] Tue, 05 June 2012 13:27 Go to previous message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
Since you are using the common terminals, you already parse identifiers
(the ID terminal rule). Either stop using the common terminals, or just
use the ID as is and add validation that ID is the more restrictive
a..z. (Pushing logic to validation is almost always the best choice as
it gives you an opportunity to provide better error messages, associate
quick fixes with found problems etc.)

This will work:

grammar org.xtext.example.mydsl.MyDsl with
org.eclipse.xtext.common.Terminals
generate myDsl "xtext.org/example/mydsl/MyDsl"

Model: st += Expr* ;
Expr: Text | Var ;
Text: val = ID;
Var: '${' varName = ID '}' ;

And you need to add validation for Text and Var to check that ID is a..z.

You could instead to as below and use a data type (MYNAME) with
a converter that restricts name to a..z.

Model: st += Expr* ;
Expr: Text | Var ;
Text: val = MYNAME;
Var: '${' varName = MYNAME '}' ;

// Has value converter that restricts name
// to a..z
MYNAME: ID ;

Regards
- henrik

On 2012-03-06 23:51, Missing name Mising name wrote:
> Hello all!
>
> I have a grammar for a simple template language:
>
>
> grammar org.xtext.example.mydsl.MyDsl with
> org.eclipse.xtext.common.Terminals
>
> generate myDsl "xtext.org/example/mydsl/MyDsl"
>
>
> Model:
> st+=Expr*;
>
> Expr:
> Text | Var
> ;
>
> Text: TXT;
>
> Var: '${' IDENT '}'
> ;
>
> terminal
> IDENT:
> ('a'..'z')+
> ;
>
> terminal
> TXT:
> ('a'..'z')*
> ;
>
>
> I expect that the following code should satisfy the grammar, but really
> it's not.
>
> ${abc}qwerty${test}
>
>
> How to fix the grammar to satisfy the code?
>
Previous Topic:Set focus after quickfix
Next Topic:Problem with quick fixes
Goto Forum:
  


Current Time: Thu Oct 29 03:30:13 GMT 2020

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

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

Back to the top