[antlr-interest] How does one refer to the char matching '.'?

Randall R Schulz rschulz at sonic.net
Sun Jul 15 10:07:36 PDT 2007


On Sunday 15 July 2007 09:59, Vaclav Barta wrote:
> Hi,
>
> I'd like to have a lexer rule for string characters:
>
> StringChar
> 	: .
> 	;

You might want to consider consolidating these characters, if that would 
work for your purposes:

StringChars
	: .+
	;


Otherwise you're likely to get a surfeit of one-character tokens.


> I presume that when defining it as the last rule, after quote tokens
> etc., only the "generic" characters will match, and it does seem to
> work, but how do I get at the matched character?
>
> Perhaps not very surprisingly,
>
> StringChar returns [ char value ]
> 	: . { $value = $.; }
> 	;
>
> doesn't work:

Call the method "getText()" in the lexical rule's action.

I'm not sure, but I don't think lexical rules can return anything other 
than Token (implementations).

You can, however, replace the token's text by calling setText().


Randall Schulz


More information about the antlr-interest mailing list