Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext grammar translation bug?(A nesting rules problem)
Xtext grammar translation bug? [message #1231028] Mon, 13 January 2014 16:54 Go to next message
Oleg Bolshakov is currently offline Oleg BolshakovFriend
Messages: 36
Registered: August 2010
Member
Hello. I can see some problem in Xtext antlr grammar generating mechanism.
Let me have this simple grammar:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

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

Model:	
	tu = translation_unit;

terminal fragment LETTER: '$' | 'A'..'Z' | 'a'..'z' | '_';
terminal ID: LETTER (LETTER|'0'..'9')*;

translation_unit: 
	{translation_unit} 
	(decls += declaration ';')*
;

declaration:
	'decl'	
	id_opt = ID? // (1) point of interest 
	id = ID
;


It works fine until I decide to nest the (1) part of declaration rule into a sub-rule like that:

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

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

Model:	
	tu = translation_unit;

terminal fragment LETTER: '$' | 'A'..'Z' | 'a'..'z' | '_';
terminal ID: LETTER (LETTER|'0'..'9')*;

translation_unit: 
	{translation_unit} 
	(decls += declaration ';')*
;


nest_rule: // (3) sub-rule
	{nest_rule} id1 = ID? // the same as (1), i.e. looking for an optional ID
;

declaration:
	'decl'	
	id_opt = nest_rule // (2) calling subrule
	id = ID
;

The second grammar crashes in antlr with the messages:
error(202): ../org.xtext.example.test1dsl/src-gen/org/xtext/example/mydsl/parser/antlr/internal/InternalMyDsl.g:174:2: the decision cannot distinguish between alternative(s) 1,2 for input such as "RULE_ID EOF EOF"
error(202): ../org.xtext.example.test1dsl.ui/src-gen/org/xtext/example/mydsl/ui/contentassist/antlr/internal/InternalMyDsl.g:349:1: the decision cannot distinguish between alternative(s) 1,2 for input such as "RULE_ID EOF EOF"

The second grammar DOES work in antlr when written manually. Obviously the problem is in antlr grammar generation by xtext. What's wrong? The grammar is LL(2) if I'm not mistaken.

I decided to look for EOF tokens in generated InternalMyDsl.g grammar. And I found out that EOF token is used in each and every entry rule (which is created for each rule in Xtext grammar):

entryRulenest_rule returns [EObject current=null]
:
{ newCompositeNode(
<...>
EOF // WHY? WHAT FOR?
;

Can somebody tell me what those entry rules mean and why EOF is used in all of them? Are there any info on how Xtext generates antlr grammar? Can anybody help me solve the problem of nesting optional rules ?

[Updated on: Mon, 13 January 2014 17:01]

Report message to a moderator

Re: Xtext grammar translation bug? [message #1231037 is a reply to message #1231028] Mon, 13 January 2014 17:12 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13665
Registered: July 2009
Senior Member
Looks like a bug to me.
as a workaround

nest_rule: // (3) sub-rule
	{nest_rule} id1 = ID // the same as (1), i.e. looking for an optional ID
;

declaration:
	'decl'	
	id_opt = nest_rule? // (2) calling subrule
	id = ID
;


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext grammar translation bug? [message #1231265 is a reply to message #1231037] Tue, 14 January 2014 08:02 Go to previous messageGo to next message
Oleg Bolshakov is currently offline Oleg BolshakovFriend
Messages: 36
Registered: August 2010
Member
The problem is that my grammar was simplified.
Actually nest_rule contains several more properties so that I can not use "?" before the whole rule name - just before the part of that.
Re: Xtext grammar translation bug? [message #1231270 is a reply to message #1231265] Tue, 14 January 2014 08:22 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13665
Registered: July 2009
Senior Member
If you come up with a reproducable example i can help finding a workaround.

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Variable Reference or New Variable Declaration
Next Topic:Determine differences between two representations of an EObject
Goto Forum:
  


Current Time: Mon Nov 30 18:10:28 GMT 2020

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

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

Back to the top