[antlr-interest] context information through rule parameters

Gerard van de Glind g.vandeglind at beinformed.nl
Thu Jul 3 01:04:09 PDT 2008


Hi Terence and all,

Coming back to my problem of passing down context information through
rule parameters. I think I have pinpointed the problem.
First let me provide some context about the grammar I am stuck with. The
language the grammar describes is ambiguous. It has been designed this
way in the past and can't be changed anymore. :-( Because of this, my
grammar contains two rules that use backtracking. This also implies that
it is impossible to replace these by syntactic predicates.

Here I present a simplified grammar that contains one of the
backtracking rules (relationalExpression). In this form ANTLR accepts
it. But if I change rule relationalExpression slightly I get ANTLR
errors. What I discovered is that when I use backtracking (which I need)
it is only allowed to pass parameters in the last branch of a rule, so I
can only pass the checkAmbiguity parameter along with booleanAtom. If I
pass it along with formula or dateAtom in the preceding branches then I
get the following error: attribute is not a token, parameter, or return
value: checkAmbiguity. I think this might have got something to do with
ANTLR (Java) code generation. So backtracking in combination with
passing along parameters (by reference) in branches that are not the
last, causes problems.

I hope this provides you with enough information to check if it is
indeed a problem with ANTLR.

Thanks a lot!

Cheers, Gerard


=====>
	grammar MyGrammar;

	options {
		output=AST;
		ASTLabelType=CommonTree;
	}

	expression : relationalExpression[true];

	relationalExpression[boolean checkAmbiguity]
	  options {backtrack=true;}
    		:	formula[true]	(LET^ | GET^ | LT^ | GT^)
formula[true]
	   	|	dateAtom[true]	(LET^ | GET^ | LT^ | GT^)
dateAtom[true]
	    	|	booleanAtom[$checkAmbiguity]
		;
	
	formula[boolean bool]
		: IDENTIFIER;

	dateAtom[boolean bool]
		: IDENTIFIER;
	
	booleanAtom[boolean bool]
		: IDENTIFIER;
	
	GT      :	'>'	;
	LT      :	'<'	;
	GET     :	'>='	;
	LET     :	'<='	;

	IDENTIFIER 	:	('a'..'z'|'A'..'Z')
('0'..'9'|'a'..'z'|'A'..'Z'|'_'|'.')*;
<=====


cool. PleaseNarrow it down to the smallest possible grammar that  
fails; I will be able to fix it quickly :)
Ter
On Jul 1, 2008, at 11:25 PM, Gerard van de Glind wrote:

>> Dear all,
>>
>> I want to pass context information along with the rules that are
>> parsed.
>>
>> Consider the following grammar:
>>
>> expression : rule1[false] | rule2[true];
>>
>> rule1[boolean bool]: rule3[true] | rule4[$bool];
>>
>> rule2[boolean bool]: rule3[false] | rule4[$bool];
>>
>> ...
>>
>> However, rule4[$bool] is not accepted. Is this an invalid construct?
>> If yes, is there a work-around?
>
> what is the error?  note rule4 is not defined in example.
> Ter
>
>
> Hi Terence and all
>
> I am afraid I over simplified my grammar to present my problem.
>
> However, I get the following message in my console: attribute is not a
> token, parameter, or return value: bool.
> And I thought it had something to do with passing the boolean  
> parameter
> along.
> I have to dig in it deeper to pinpoint my problem.
>
> Thanks!
>
> Gerard
>



More information about the antlr-interest mailing list