Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Xtext grammar translation bug?(A nesting rules problem)
Xtext grammar translation bug? [message #1231028] Mon, 13 January 2014 11:54 Go to next message
Eclipse UserFriend
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 12:01] by Moderator

Re: Xtext grammar translation bug? [message #1231037 is a reply to message #1231028] Mon, 13 January 2014 12:12 Go to previous messageGo to next message
Eclipse UserFriend
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
;
Re: Xtext grammar translation bug? [message #1231265 is a reply to message #1231037] Tue, 14 January 2014 03:02 Go to previous messageGo to next message
Eclipse UserFriend
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 03:22 Go to previous message
Eclipse UserFriend
If you come up with a reproducable example i can help finding a workaround.
Previous Topic:Variable Reference or New Variable Declaration
Next Topic:Determine differences between two representations of an EObject
Goto Forum:
  


Current Time: Wed Jul 23 21:23:33 EDT 2025

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

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

Back to the top