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

Terence Parr parrt at cs.usfca.edu
Sun Apr 29 15:24:16 PDT 2007


On Apr 23, 2007, at 9:24 AM, Johannes Luber wrote:

> 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) {}

Yes, the book talks about this problem for syntactic predicates, but  
apparently I didn't for semantic predicates.  Strange.  I thought I  
had described this issue.

> So here input isn't a TokenStream but only an IntStream. How can I

  in the lexer the lookahead is only a character. unless you go match  
an identifier, you will not have that text.  There is no such thing  
as the lookahead text in a lexer. I suspect that you need to have  
your predicates in the parser not the lexer.

Ter

> 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