[antlr-interest] Token stream in semantic predicates

Vaclav Barta vbar at comp.cz
Sat Aug 18 23:13:19 PDT 2007


Hi,

I'm trying to recognize special-case strings via semantic predicates:

command_name
	: { "java".equals(input.LT(1).getText()) }? => string -> ^( JAVA )
	| n = string -> ^( COMMAND[$n.value] )
	;

("java" isn't a real token because it's special only as a command; in other 
parts of the grammar, it's a string literal like any other.) But, the 
generated parser won't compile:

    [javac] /home/vb/antlr/antmaker/src/LoaderParser.java:2031: cannot find 
symbol
    [javac] symbol  : method LT(int)
    [javac] location: interface org.antlr.runtime.IntStream
    [javac]                         if (
( "java".equals(input.LT(1).getText()) ) ) {s = 13;}

(Repeated in couple of places.) What is the correct syntax for getting at the 
text of the current token?

Funny thing is, I can write

@parser::members {
	boolean is_at(String special) {
		return special.equals(input.LT(1).getText());
	}
}

command_name
	: { is_at("java") }? => string -> ^( JAVA )
	| n = string -> ^( COMMAND[$n.value] )
	;

which it compiles OK and works as expected (as far as I can tell) - but why do 
I have to define the extra method?

	Bye
		Vasek


More information about the antlr-interest mailing list