[antlr-interest] How to use LT and LA in predicates?

Johannes Luber jaluber at gmx.de
Mon Apr 23 09:24:09 PDT 2007


Loring Craymer wrote:
> In the parser, input is a TokenStream.  In a Lexer, it
> is an IntStream and returns ints (characters).
> 
> --Loring

Is there an easier way to turn the ints into strings than using new
String(new char[]{(char) input.LT(1)})? And do I truly get the character
(couldn't test that yet)?

After further investigation of my last problem (pasted below for
reference) I saw that I overlooked that with {}?=> the predicate isn't
tested in the parser but in a nested DFA class, which has only as
function signature

public int specialStateTransition(int s, IntStream input) {}

So here input isn't a TokenStream but only an IntStream. How can I
change that? I don't want to implement another parser for the
recognition of the next token alone.

Best regards,
Johannes Luber

--------
The third problem I have is with these rules:

identifier
	:	{!isKeyword(input.LT(1).getText())}?=> AVAILABLE_IDENTIFIER
	|	'@' IDENTIFIER_OR_KEYWORD
	;
	
fragment AVAILABLE_IDENTIFIER
	:	IDENTIFIER_OR_KEYWORD /* An identifier_or_keyword that is not a
keyword */
	;

I have used {}?=> to enforce the use of the predicate, as otherwise the
presence or absence of an @ distinguishes between the cases. Now I
receive the error, that the class IntStream doesn't have the method
getText(). The following rule snippet is generated:

else if ( (LA58_0==AVAILABLE_IDENTIFIER) &&
(!isKeyword(input.LT(1).getText()))) {s = 3;}


More information about the antlr-interest mailing list