Parser ambiguity [message #1839316] |
Fri, 19 March 2021 08:42  |
Alexandra Tritean 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 #1839363 is a reply to message #1839345] |
Fri, 19 March 2021 14:13   |
Alexandra Tritean 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   |
|
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;
Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02413 seconds