Skip to main content



      Home
Home » Modeling » TMF (Xtext) » How to solve this ambiguity problem
How to solve this ambiguity problem [message #1844775] Fri, 24 September 2021 12:28 Go to next message
Eclipse UserFriend
Hi,

I don't understand why i get the following ambiguity error for the minimal grammer posted below:
error(202): ../org.xtext.example.mydsl/src-gen/org/xtext/example/mydsl/parser/antlr/internal/InternalMyDsl.g:286:3: the decision cannot distinguish between alternative(s) 1,2 for input such as "RULE_ID EOF EOF"


Minimal Grammar for my problem:

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

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

Model:
	(elements+=Enumeration)*
;
	
Enumeration:
	{Enumeration}
	'Enumeration' name=ID '{' 
		(encoding=Encoding)
	'}'
;

Encoding:
	{Encoding}
	Annotations
	name=ID 
;

fragment Annotations *:
	(annotations+=Annotation)*
;
Annotation:
	{Annotation}
	'@'(name=ID)?
;


The problem is that the name of the Annotation-rule is optional (after the @-character). I assume the parser has a problem when trying to parse something like:
Enumeration ABC {
    @
    MyEncoding
}


In this example, the ID "MyEncoding" could either be the name of the annotation or the name of the encoding. Is there any way to tell the parser that is is supposed to be the name of the encoding? I tried Syntactic predicates but without success. I also tried to forbid any Hidden Terminal Symbols between the add character and the name, also without success.

Any ideas?


Re: How to solve this ambiguity problem [message #1844776 is a reply to message #1844775] Fri, 24 September 2021 12:39 Go to previous message
Eclipse UserFriend
Well, after writing all this up I found a solution that works for me. Here is it for reference:

Annotation hidden():
	{Annotation}
	'@'(name=ID | WS)
;


Basically I forbid all whitespaces in the annotation rule unless when the name is omitted. The parser then understands that the annotation is complete if there is a whitespace after the @.
Previous Topic:How to stop the validation programmatically for dsl project
Next Topic:Best Practise for autocompletion during syntax errors
Goto Forum:
  


Current Time: Tue May 13 07:59:04 EDT 2025

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

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

Back to the top