Xtext grammar translation bug? [message #1231028] |
Mon, 13 January 2014 16:54 |
Oleg Bolshakov 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
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05014 seconds