[antlr-interest] Confused over how to use custom tokens and exceptions.

Tim Clark timgclark at gmail.com
Tue May 22 03:02:06 PDT 2007


Hi all

I'm a little confused over how to implement my own tokens and exceptions.

1. Tokens.
I want to add the current file name to the tokens, so I need to use a custom
token class (I'm doing file includes).
In the combined parser and lexer grammar, if I use options{
tokenLabelType=MyToken; } it appears to take effect in the parser, but not
the automatically generated) lexer. Specifically,  the lexer rule

INCLUDE  :    'include' (WS)? f=STRING

generates something like this:

public final void mINCLUDE() throws RecognitionException {
        try {
            int _type = INCLUDE;
            MyToken f=null;
......
            f = new CommonToken(.....);

which causes a java error (cannot convert CommonToken to MyToken).

Do I have to use separate parser and lexer, or what?

2. Exceptions
>From a tree parser derived from my parser I am calling methods in an
external object (to generate code). The problem is that I want to throw my
own exceptions from those methods, but the parser rule methods only throw
RecognitionException, so it's not allowed.

Is it possible to do what I want?

Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070522/79a61f76/attachment.html 


More information about the antlr-interest mailing list