Ambiguous xtext grammar and referencing error
I have been trying out different versions and none of the work. However,
this they don't compile as well. These are my different alternatives:
1
Model:
params += Parameter*
greetings+=Greeting*
;
Greeting:
'OBJ' name = ID
(
list1 += (switchType1 )*
) ';'
;
switchType1:
'SWITCH' '(' ((expression = Formula ) ) ')' '{'
cases += CaseType1 *
'}'
;
CaseType1:
'CASE' val = INT ':' '{' objList += Greeting* '}'
;
Formula:
(param1 = [Parameter] ) | (objRef = [Greeting] )
;
Parameter:
'PARAM' name = ID ';'
;
2
Model:
params += Parameter*
greetings+=Greeting*
;
Greeting:
'OBJ' name = ID
(
list1 += (switchType1 )*
) ';'
;
switchType1:
'SWITCH' '(' ((expression = Formula ) | (objRef = [Greeting] ) ) ')' '{'
cases += CaseType1 *
'}'
;
CaseType1:
'CASE' val = INT ':' '{' objList += Greeting* '}'
;
Formula:
(param1 = [Parameter] ) ('+' val = INT)?
;
Parameter:
'PARAM' name = ID ';'
;
alternative 3:
Model:
params += Parameter*
greetings+=Greeting*
;
Greeting:
'OBJ' name = ID
(
(list1 += (switchType1 )*)
|
(list2 += (switchType2)*)
) ';'
;
switchType1:
'SWITCH' '(' ((expression = Formula ) ) ')' '{'
cases += CaseType1 *
'}'
;
CaseType1:
'CASE' val = INT ':' '{' objList += Greeting* '}'
;
Formula:
(param1 = [Parameter] ) ('+' val = INT)?
;
switchType2:
'SWITCH' '(' objRef = [Greeting] ')' '{'
cases += CaseType1*
'}'
;
Parameter:
'PARAM' name = ID ';'
;
Th ideal implementation for me is number 3 because my model will be
cleaner but I have tried the other two as well. All of these I get this
error:
error(211):
../org.xtext.example.mydsl.ui/src-gen/org/xtext/example/mydsl/ui/contentassist/antlr/internal/InternalMyDsl.g:260:1:
[fatal] rule rule__Greeting__Alternatives_2 has non-LL(*) decision due to
recursive rule invocations reachable from alts 1,2. Resolve by
left-factoring or using syntactic predicates or using backtrack=true
option.
warning(200):
../org.xtext.example.mydsl.ui/src-gen/org/xtext/example/mydsl/ui/contentassist/antlr/internal/InternalMyDsl.g:260:1:
Decision can match input such as "'SWITCH' '(' RULE_ID '+'" using multiple
alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
warning(200):
../org.xtext.example.mydsl.ui/src-gen/org/xtext/example/mydsl/ui/contentassist/antlr/internal/InternalMyDsl.g:260:1:
Decision can match input such as "'SWITCH' '(' RULE_ID ')' '{' '}'" using
multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
I have set backtrack true in MWE2, but still I get the same error.
Only when I remove the ? from Formula val definition, I get the grammar
built and running, but this is not possible for my real grammar.
Do you have any idea how I can fix it? How should I implement this grammar?
No comments:
Post a Comment