Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Parser ambiguity
Parser ambiguity [message #1839316] Fri, 19 March 2021 08:42 Go to next message
Alexandra Tritean is currently offline Alexandra TriteanFriend
Messages: 37
Registered: March 2020
Member
Hello,

I'm facing an issue with my grammar which I believe is an ambiguity that I've introduced, but I'm not sure which would be the best approach to solve it.

A very simplified version of the grammar:
Context:
	'context' name=CTXT_FQN ('extends' parentContext=[Context|CTXT_FQN] )? '{' 
	('entity-catalog' '{'
	entities+=Entity*
	'}')?
	'}';
	
Entity:
	name=ID  ('extends' (parentEntity=[Entity|GENERAL_FQN]))? '{'
	features+=(Feature)*
	'}';

Feature:
	name=ID
        ';';

CTXT_FQN hidden():
    ID '-' VERSION ;
VERSION hidden():
    INT '.' INT '.' INT ('.' INT)?;
GENERAL_FQN hidden():
	CTXT_FQN'.'ID;


The change that I've recently made was to change the definition of the parentEntity to
parentEntity=[Entity|ID]|parentEntity=[Entity|GENERAL_FQN]

in order to avoid using the qualified name when the entity that represents the parent entity is within the same context.

And the adapted scope provider:
override getScope(EObject context, EReference reference){
		if (reference == ePackage.entity_ParentEntity && context instanceof Entity){
			val entity=context as Entity;
			return scopeForParentEntity(entity,context,reference)
		}
		return super.getScope(context,reference)
	}

	def protected IScope scopeForParentEntity(Entity currentEntity, EObject context, EReference reference){
		val List<Entity> entities = newArrayList;
		var descriptions = new ArrayList<IEObjectDescription>
		val currentContext = currentEntity.eContainer as Context;
		collectEntities(currentContext, entities);
		entities.remove(currentEntity);
		for(Entity entity: entities){
			var contextEntity = entity.eContainer as Context
			if(contextEntity.name.equals(currentContext.name)){
				descriptions.add(EObjectDescription.create(QualifiedName.create(entity.name), entity))
			} else {
				descriptions.add(EObjectDescription.create(QualifiedName.create((entity.eContainer as Context).name.split("\\.")).append(entity.name), entity))
			}
		}
		return new SimpleScope(IScope.NULLSCOPE, descriptions)
	}
	
	def protected void collectEntities(Context context, List<Entity> entities){
		entities.addAll(context.entities);
		if(context.parentContext !== null && !context.getName.equals(context.parentContext.getName)){
			collectEntities(context.parentContext,entities);
		}
	}


Everything works correctly from the scope provider point of view, the problem is that when using the content assist for the parent entity the list of proposals shows the features of the 'current' entity instead of showing the possible entities.
I've tried to override the ProposalProvider, but this didn't helped cause at that point I've discovered that the features of the entity are indeed 'interpreted' as entities. This is the reason for which I believe that the parser/lexer is facing an ambiguity.

Which would be the best approach to handle this?

Kind Regards,

Alexandra


Re: Parser ambiguity [message #1839324 is a reply to message #1839316] Fri, 19 March 2021 10:26 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
can you try

parentEntity=[Entity|IDorGENERAL_FQN]


IDorGENERAL_FQN: ID | GENERAL_FQN;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parser ambiguity [message #1839328 is a reply to message #1839324] Fri, 19 March 2021 10:51 Go to previous messageGo to next message
Alexandra Tritean is currently offline Alexandra TriteanFriend
Messages: 37
Registered: March 2020
Member
That works from the content assist/proposals point of view and when using entities from the parentContext, but when using entities within the same context it fails with the error 'No viable alternative at input 'X''.
Re: Parser ambiguity [message #1839329 is a reply to message #1839328] Fri, 19 March 2021 10:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
this is dont understand. is X and ID ? is it an GENERAL_FQN?
what is it

can you provide a complete minimal reproducible grammar and unit test?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Fri, 19 March 2021 11:06]

Report message to a moderator

Re: Parser ambiguity [message #1839331 is a reply to message #1839329] Fri, 19 March 2021 11:11 Go to previous messageGo to next message
Alexandra Tritean is currently offline Alexandra TriteanFriend
Messages: 37
Registered: March 2020
Member
I'm sorry for the confusion. I was trying to generalize the error message.

I made the change that you suggested and for the following example :
context BaseContext-1.0.0 extends ParentContext-1.0.0 {
	 entity-catalog  {
	 	Instrument extends InterestInstrument {
	 		feature1;
	 		feature2;
	 	}
	 	
	 	InterestInstrument extends ParentContext-1.0.0.Party {
	 		feature3;
	 		feature4;
	 	}
	 }
}

context ParentContext-1.0.0 {
	entity-catalog {
		Party { 
			feature5;
		}
	}	 
}


For extends InterestInstrument I'm getting the error 'No viable alternative at input InterestInstrument'.

For extends ParentContext-1.0.0.Party it compiles without any error.
Re: Parser ambiguity [message #1839333 is a reply to message #1839331] Fri, 19 March 2021 11:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
can you please provide the grammar you use right now

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parser ambiguity [message #1839334 is a reply to message #1839333] Fri, 19 March 2021 11:41 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
also: are you sure you mean GENERAL_FQN and not CTXT_FQN?
there is no .ID in ParentContext-1.0.0

so ParentContext-1.0.0.ID parses fine


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Fri, 19 March 2021 11:42]

Report message to a moderator

Re: Parser ambiguity [message #1839335 is a reply to message #1839334] Fri, 19 March 2021 11:51 Go to previous messageGo to next message
Alexandra Tritean is currently offline Alexandra TriteanFriend
Messages: 37
Registered: March 2020
Member
Grammar attached.
Re: Parser ambiguity [message #1839338 is a reply to message #1839335] Fri, 19 March 2021 12:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
indeed

GENERAL_FQN hidden():
CTXT_FQN'.'ID;

there must be an ID and you dont have one in your example model


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parser ambiguity [message #1839339 is a reply to message #1839338] Fri, 19 March 2021 12:10 Go to previous messageGo to next message
Alexandra Tritean is currently offline Alexandra TriteanFriend
Messages: 37
Registered: March 2020
Member
IDorGENERAL_FQN hidden():
ID | GENERAL_FQN;

My expectation was that in that case will go into ID alternative.
Re: Parser ambiguity [message #1839345 is a reply to message #1839339] Fri, 19 March 2021 12:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
but ParentContext-1.0.0 is not an ID

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parser ambiguity [message #1839363 is a reply to message #1839345] Fri, 19 March 2021 14:13 Go to previous messageGo to next message
Alexandra Tritean is currently offline Alexandra TriteanFriend
Messages: 37
Registered: March 2020
Member
Ok, but I'm referring to the case that throws the error.
context BaseContext-1.0.0 {
	 entity-catalog  {
	 	Instrument extends InterestInstrument {
	 		feature1;
	 		feature2;
	 	}
	 	
	 	InterestInstrument {
	 		feature3;
	 		feature4;
	 	}
	 }
}

In this case InterestInstrument isn't ID ? Cause this is the case for which I'm getting 'No viable alternative at input 'InterestInstrument''.

[Updated on: Fri, 19 March 2021 14:15]

Report message to a moderator

Re: Parser ambiguity [message #1839365 is a reply to message #1839363] Fri, 19 March 2021 14:16 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i dont get it. this grammar works fine for me:

Model:
	(c+=Context | e+=Entity)*;
	
Context:
	'context' name=CTXT_FQN ('extends' parentContext=IDorGENERAL_FQN )? '{' 
	('entity-catalog' '{'
	entities+=Entity*
	'}')?
	'}';
	
Entity:
	name=ID  ('extends' (parentEntity=IDorGENERAL_FQN))? '{'
	features+=(Feature)*
	'}';

Feature:
	name=ID
        ';';
        
IDorGENERAL_FQN: ID | GENERAL_FQN;        

CTXT_FQN hidden():
    ID '-' VERSION ;
VERSION hidden():
    INT '.' INT '.' INT ('.' INT)?;
GENERAL_FQN hidden():
	CTXT_FQN'.'ID;



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parser ambiguity [message #1839367 is a reply to message #1839365] Fri, 19 March 2021 14:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
the hidden thing may be buggy. please remove it from GENERAL_FQN

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Parser ambiguity [message #1839370 is a reply to message #1839367] Fri, 19 March 2021 14:43 Go to previous message
Alexandra Tritean is currently offline Alexandra TriteanFriend
Messages: 37
Registered: March 2020
Member
You're right. This is what made the difference. :)

Thank you for your help!

Have a nice weekend,

Alexandra
Previous Topic:Read ._trace files with java
Next Topic:Xtext 2.14 - programmatic EMF model manipulation with minimal re-indexing
Goto Forum:
  


Current Time: Thu Apr 18 22:04:49 GMT 2024

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

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

Back to the top