Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Xtext - Recursive grammar
Xtext - Recursive grammar [message #1066988] Fri, 05 July 2013 07:00 Go to next message
Eclipse UserFriend
How can I use a recursive grammar?

I have the following code:
model:
defs += CFG_objects
;

CFG_objects:
statement=ID name=ID '{'
(par+=CFG_pars)*
'}' (':' comment=STRING)? ';'
;

CFG_pars:
condition=ID val=ID
('{' (pars+=CFG_pars) '}')?
(':' comment=STRING)? ';'
;


It throws me an error with this:

switch answer
{
case good : do_nothing ;
}: store answer;


What is wrong with my grammar? How can I implement a recursive grammar rule? I was expecting the above example to work.

[Updated on: Fri, 05 July 2013 07:01] by Moderator

Re: Xtext - Recursive grammar [message #1066991 is a reply to message #1066988] Fri, 05 July 2013 07:13 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

you define your comments as String, but in your sample they are not. STRING requires single or double quotes.

Also, you are probably missing "+" or "*" in your list definitions (model feature defs, CFG_pars feature pars).

Alex
Re: Xtext - Recursive grammar [message #1067246 is a reply to message #1066991] Sun, 07 July 2013 23:39 Go to previous messageGo to next message
Eclipse UserFriend
Hello,

I have changed the code and my configuration. But I am still getting an error.

model:
defs += CFG_objects
;

CFG_objects:
statement=ID name=ID '{'
(pars+=CFG_pars)*
'}' (':' comment=STRING1)? ';'
;

CFG_pars:
condition=ID val=ID
('{' (pars+=CFG_pars)* '}')?
(':' comment=STRING1)? ';'
;
terminal STRING1: '"' -> '"';




switch answer
{
case good : "do nothing" ;
TYPE bad;
}: "store answer";



The error I am getting is:
ERROR:mismatched input 'TYPE' expecting '}'
ERROR:extraneous input ';' expecting RULE_ID

[Updated on: Sun, 07 July 2013 23:40] by Moderator

Re: Xtext - Recursive grammar [message #1067254 is a reply to message #1067246] Mon, 08 July 2013 02:31 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

this is not the complete grammar. It does not rise the problem you claim.

Alex
Re: Xtext - Recursive grammar [message #1067327 is a reply to message #1066988] Mon, 08 July 2013 06:15 Go to previous messageGo to next message
Eclipse UserFriend
I have modified the grammar a bit.

Model:
defs += CFG_objects*
;

CFG_objects:
	statement=ID name=ID '{'
	(pars+=CFG_pars)*
	'}'  comment=CFG_comment ';'
;

CFG_pars:
	name=ID value=SPECIAL
	('{' (pars+=CFG_pars)* '}')?
	comment=CFG_comment ';'
;

CFG_comment: {CFG_comment}
	(':' comment=STRING1)?
;

SPECIAL hidden():
	STRING1 | ID | INT| HEX | DOUBLE
;

terminal STRING1: '"' -> '"';
terminal DOUBLE:  INT '.' INT;
terminal HEX: '0' ('x' | 'X')(('0'..'9') | ('a'..'f') | ('A'..'F'))+;


I noticed that the error only occur in some scenarios.

switch answer
{
case good : "do nothing" ;
TYPE BAD; <--Problem line-->
PRIO HIGH;
}: "store answer";


If I change the problem line to "type", parsing still will not work. But other keywords works fine (e.g.STATUS, Size, size).



Re: Xtext - Recursive grammar [message #1067330 is a reply to message #1067327] Mon, 08 July 2013 06:24 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

this is still not the complete grammar, the example is processed without errors. My guess is that you use "TYPE" as a keyword somewhere in your real grammar. That way TYPE will not be read as ID (as your rule reqires) but as keyword token.

Alex
Re: Xtext - Recursive grammar [message #1067481 is a reply to message #1067330] Mon, 08 July 2013 21:48 Go to previous messageGo to next message
Eclipse UserFriend
OH! You are right! [Banging head on the monitor screen]
Been staring at this for a week now.

Is there a way to fix this? I do not want to specify each possible keyword. But other rules do require for the "TYPE" keyword to be there.

[Updated on: Mon, 08 July 2013 21:57] by Moderator

Re: Xtext - Recursive grammar [message #1067499 is a reply to message #1067481] Tue, 09 July 2013 02:25 Go to previous message
Eclipse UserFriend
See e.g. here. I'd recommend simple escaping if possible.

Alex
Previous Topic:XText rejects user defined hidden terminal rules
Next Topic:Factoring out rules with no wrapper return types
Goto Forum:
  


Current Time: Tue Jul 01 16:37:30 EDT 2025

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

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

Back to the top