Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Grammar generation bug when adding cross-reference
Grammar generation bug when adding cross-reference [message #1742797] Wed, 07 September 2016 15:57 Go to next message
SAULO ARAUJO is currently offline SAULO ARAUJOFriend
Messages: 3
Registered: September 2016
Junior Member
Hi,

I believe there is a bug in the antlr grammar generation code.

Below is my grammar (it is a subset of the Standard ML grammar):

grammar es.afluent.telluride.Ur hidden (WS)

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

generate ur "http ://www.afluent.es/telluride/Ur"

Prog
	:	(declarations += Dec ';'?)*
	;

Dec
	:	'type' (typeBindinds += TypBind) ('and' typeBindinds += TypBind)*		
	|	'datatype' dataTypeBindings += DatBind ('and' dataTypeBindings += DatBind)* 
		('withtype' (typeBindinds += TypBind) ('and' typeBindinds += TypBind)*)?
	;

TypBind
	:	(		variables += TypeVariableDeclaration
			|	'(' variables += TypeVariableDeclaration (',' variables += TypeVariableDeclaration)* ')'
		)? name = ID ('=' type = Typ)?
	;

TypeVariableDeclaration
	:	name = VAR
	;

Typ
	: 	FunctionType
	;

FunctionType returns Typ
	:	TupleType ('->' {FunctionType.domain = current} codomain = FunctionType)?	
	;

TupleType returns Typ
	:	TupleTypeElement ('*' {TupleType.elements += current} elements += TupleTypeElement
                         ('*'                                 elements += TupleTypeElement)*)?
	;

TupleTypeElement returns Typ
	:	RecordType
	|	TypeConstructorApplication
	;

RecordType
	:	'{' fields += Field (',' fields += Field)* '}'
	;

Field
	:	id = ID ':' type = Typ
	;

TypeConstructorApplication returns Typ
	:	(TypeReference | TypeVariableReference) ({TypeConstructorApplication.arguments += current} constructor = TypeReference)*
	|	'('	Typ (
				')' ({TypeConstructorApplication.arguments += current} constructor = TypeReference)*							
			|	',' {TypeConstructorApplication.arguments += current} arguments += Typ 
				(',' arguments += Typ)* ')' constructor = TypeReference 
				({TypeConstructorApplication.arguments += current} constructor = TypeReference)*
		)		
	;

TypeReference
	:	type = [TypBind]
	;
	
TypeVariableReference
	:	variableName = VAR
	;

DatBind returns TypBind
	:	(		variables += TypeVariableDeclaration
			| 	'(' variables += TypeVariableDeclaration (',' variables += TypeVariableDeclaration)* ')'		
		)? name = ID '=' constructors += ConBind ('|' constructors += ConBind)*
	;

ConBind
	:	name = ID ('of' type = Typ)?
	;
	
terminal VAR
	:	'\''? '\'' ID_FRAGMENT
	;

terminal ID
	:	ID_FRAGMENT
	;
	
terminal LONG_ID
	:	ID_FRAGMENT ('.' ID_FRAGMENT)*
	;	

terminal fragment ID_FRAGMENT
	:	('a' .. 'z' | 'A' .. 'Z') ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '\'' | '_')*
	;

terminal WS
	:	(' ' | '\t' | '\r' | '\n')+
	;


If I change the rule

TypeVariableReference
	:	variableName = VAR
	;


to

TypeVariableReference
	:	variable = [TypeVariableDeclaration]
	;


antlr complains saying

warning(200): ../es.afluent.telluride/src-gen/es/afluent/telluride/parser/antlr/internal/InternalUr.g:802:4: Decision can match input such as "RULE_ID" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
error(201): ../es.afluent.telluride/src-gen/es/afluent/telluride/parser/antlr/internal/InternalUr.g:802:4: The following alternatives can never be matched: 2


It seems that the problem is that when I change the rule, the associated rule in the generated antlr grammar changes from

// Rule TypeVariableReference
ruleTypeVariableReference returns [EObject current=null]
@init {
	enterRule();
}
@after {
	leaveRule();
}:
	(
		(
			lv_variableName_0_0=RULE_VAR
/* ... */


to

// Rule TypeVariableReference
ruleTypeVariableReference returns [EObject current=null]
@init {
	enterRule();
}
@after {
	leaveRule();
}:
	(
		(
			{
				if ($current==null) {
					$current = createModelElement(grammarAccess.getTypeVariableReferenceRule());
				}
			}
			otherlv_0=RULE_ID
/* ... */


i.e., rather than referencing RULE_VAR, it is referencing RULE_ID.

Is this really a bug? Is there a way to work around it?

PS: I am sending this message again because I could not find the first one in the forum. Sorry for duplicates!

Any help would be very appreciated,
Saulo
Re: Grammar generation bug when adding cross-reference [message #1742800 is a reply to message #1742797] Wed, 07 September 2016 16:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13883
Registered: July 2009
Senior Member
TypeVariableReference
	:	variable = [TypeVariableDeclaration]
	;


is short for

TypeVariableReference
	:	variable = [TypeVariableDeclaration|ID]
	;


which means it will be parsed as if there would be a

TypeVariableReference
	:	variable=ID
	;


thus it conflicts with

TypeReference
	:	type = [TypBind]
	;


but since TypeBind have a name that are IDs
and TypeVariableDeclaration have names that are vars

you can simply chnage the grammar to

TypeVariableReference
	:	variable = [TypeVariableDeclaration|VAR]
	;


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
Re: Grammar generation bug when adding cross-reference [message #1742819 is a reply to message #1742800] Wed, 07 September 2016 21:47 Go to previous message
SAULO ARAUJO is currently offline SAULO ARAUJOFriend
Messages: 3
Registered: September 2016
Junior Member
Thanks a lot Christian!
Previous Topic:Confusion about terminals
Next Topic:xtext get parser rule name based on context
Goto Forum:
  


Current Time: Fri Apr 16 11:45:28 GMT 2021

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

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

Back to the top