[antlr-interest] Bug in exception initialization code?

Michael Bedward michael.bedward at gmail.com
Mon Mar 28 06:07:40 PDT 2011


Hi Søren,

I see that error every so often and, so far al least, it has always
been due to an error in my tree walker grammar - specifically an
alternative that arises in the AST but that I forgot to consider in
the grammar.

Michael

On 28 March 2011 23:29, Søren Boisen <sboisen at mail.dk> wrote:
> 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
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list