[antlr-interest] Very high runtime and memory usage caused by trivial grammar => Is it my mistake?

Cremerius Ralf (DGS-EC/ECC3) Ralf.Cremerius at de.bosch.com
Thu Oct 21 00:13:00 PDT 2010


Hello,

I used ANTLR v3.2 and ANTLRWorks v1.4 to build multiple small tools. Recently I encountered the problem, that a rather trivial grammar causes IMHO inappropriately high runtime and memory usage.

Looking for my mistake, I stripped the grammar down to following one:

----------------------------------------------------------------------------------------------------------------------------------------

grammar problem;

main_rule
        :       SOMETOKEN;

SOMETOKEN
        :       ('a')*;

----------------------------------------------------------------------------------------------------------------------------------------

>From this source file I compiled a grammar using ANTLRWorks v1.4 and surrounded it with the standard Java code from the TDAR (so nothing special here).

If I feed the parser some input, that is not valid with respect to the above grammar (it can be anything, take e.g. the following string of only one character: "<"), it will take a lot of processor time and consume very high amounts of memory. E.g. I configured a limit of 1 GB of RAM in Eclipse (using "-Xmx1024m") and it's taking this amount.

To be complete, I also list the code calling the parser here:

----------------------------------------------------------------------------------------------------------------------------------------

public static void main(String[] args) throws Exception
{
        ANTLRStringStream       input   = new ANTLRStringStream("<");
        problemLexer            lexer   = new problemLexer(input);
        CommonTokenStream       tokens  = new CommonTokenStream(lexer);
        problemParser           parser  = new problemParser(tokens);
        parser.main_rule();
}

----------------------------------------------------------------------------------------------------------------------------------------

=> Did I make some beginner's error and I'm just not able to see it?

Thanks in advance and best regards,
   Ralf Cremerius



More information about the antlr-interest mailing list