[antlr-interest] reuse() methos in 3.4 C runtime

Jim Idle jimi at temporal-wave.com
Fri Jun 24 09:49:04 PDT 2011


Because the documentation is not yet up to date, here is an example of
reusing the allocated memory in input streams and token streams:



    for (i=0; i<iterations; i++) {



        // Run the parser.

        //

        psr->start(psr);



        // --------------------------------------

        // Now reset everything for the next run.

        // Order of calls is important.



        // Input stream can now be reused

        //

        input->reuse(input, sourceCode, sourceLen, sourceName);



        // Reset the common token stream so that it will reuse its resources

        //

        tstream->reset(tstream);



        // Reset the lexer (new function generated by antlr now)

        //

        lxr->reset(lxr);



        // Reset the parser (new function generated by antlr now)

        //

        psr->reset(psr);

    }



Note that tree parsers cannot reuse their allocations but this is rarely an
issue. The input->reuse() will reuse any memory it has allocated, but
requires that you handle the reading of the input files (or otherwise supply
a pointer to them). The input files are assumed to be encoded in the way
that the original input was created, for instance:



input       = antlr3FileStreamNew(fname, ANTLR3_ENC_8BIT);



Then all reused input must be 8 bit encoded.



Jim


More information about the antlr-interest mailing list