[antlr-interest] Bug in exception initialization code?

Søren Boisen sboisen at mail.dk
Mon Mar 28 05:29:05 PDT 2011


Greetings,
I have a encountered a problem while trying to write a tree parser for my
grammar. Sometimes when trying to walk the AST and the tree parser fails to
match some input, I get the following exception:

can't look backwards more than one token in this stream
    org.antlr.runtime.misc.LookaheadStream.LB(LookaheadStream.java:159)
    org.antlr.runtime.misc.LookaheadStream.LT(LookaheadStream.java:120)

org.antlr.runtime.RecognitionException.extractInformationFromTreeNodeStream(RecognitionException.java:144)
    org.antlr.runtime.RecognitionException.(RecognitionException.java:111)

org.antlr.runtime.MismatchedTreeNodeException.(MismatchedTreeNodeException.java:42)

org.antlr.runtime.tree.TreeParser.recoverFromMismatchedToken(TreeParser.java:135)
    org.antlr.runtime.BaseRecognizer.match(BaseRecognizer.java:115)

com.brunata.ws.rest.odata.query.NearestTelegramQueryWalker.queryOp(NearestTelegramQueryWalker.java:243)

com.brunata.ws.rest.odata.query.NearestTelegramQueryWalker.stmt(NearestTelegramQueryWalker.java:128)

I have looked through the FAQ, the issue tracker and Google, but I couldn't
find anything that looked related.

>From the stack trace it looks like an error in the
extractInformationFromTreeNodeStream, since I guess it shouldn't expect to
always be able to look back more than one token in a stream.
Is there a workaround for this? This is the code I use for creating parsers
and streams:

        String query = ...;  // this is a parameter to the method
        Map<String, String> propertyFilters = ...; // this is a field on the
class

        ANTLRStringStream input = new ANTLRStringStream(query);
        QueryParserLexer lexer = new QueryParserLexer(input);

        CommonTokenStream tokens = new CommonTokenStream(lexer);
        QueryParserParser parser = new QueryParserParser(tokens);

        QueryParserParser.stmt_return result = tryParse(parser, query);
        ast = (CommonTree) result.getTree();

        CommonTreeNodeStream nodes = new CommonTreeNodeStream(ast);
        nodes.setTokenStream(tokens);

        NearestTelegramQueryWalker walker = new
NearestTelegramQueryWalker(nodes);
        propertyFilters = tryWalk(walker, query);

Am I using the wrong kind of token/node streams or something?

Regards,
Søren Boisen


More information about the antlr-interest mailing list