[antlr-interest] Lexer context problem

Keith Whittingham kwhittingham at gmail.com
Sat Nov 24 00:56:44 PST 2007


Could someone explain what I'm doing wrong here

Giving the input "Abc[1..2]" to the following
-----
grammar Test;

@lexer::members {
	boolean brackets;
}

tuple	:	NAME OSB INT DOTDOT INT CSB;

NAME	
	:	{!brackets}?  
('A'..'Z'|'a'..'z'|'.'|'_')('0'..'9'|'A'..'Z'|'a'..'z'|'.'|'_')+
	;

OSB
	:	'[' {brackets=true;}
	;

DOTDOT
	:	{brackets}? '..'
	;

CSB
	:	']' {brackets=false;}
	;

INT
	:	'0' | ('1'..'9')('0'..'9')*
	;
-----
I always get an error "ine 1:6 rule NAME failed predicate: {!brackets}?"

NAME seems to be jumping in when (as far as my understanding goes (which  
isn't very far) it shouldn't. The Open Square Brackets (OSB) sets  
'bracket' true so NAME should not be recognised. Strangely if I reduce  
'tuple' to:

	tuple	:	NAME OSB INT DOTDOT CSB ; / with the input "Abc[1..]" works

but..

	tuple	:	NAME OSB DOTDOT INT CSB ; // with the input "Abc[..1]" fails??

I've just bought the book and scanned that for an answer but can't find  
anything.

Thanks in advance
Keith



More information about the antlr-interest mailing list