|PCCTS, multiple ANTLR calls email@example.com (1993-08-11)|
|Re: PCCTS, multiple ANTLR calls firstname.lastname@example.org (Terence Parr) (1993-08-15)|
|From:||"Terence Parr" <email@example.com>|
|Date:||Sun, 15 Aug 1993 14:12:52 GMT|
> I'm trying to use PCCTS as a parser within a larger program from
> which I need to call the ANTLR macro several times.
> Is there a way call ANTLR multiple times within an executable?
Yep. Depends on what you want to do. There are two cases:
 You want to rerun the same grammar or pieces of it on the same
input stream w/o rewinding it. Solution: just call the appropriate
function associated with that grammar rule (don't forget to pass
any arguments you have have defined for the rule):
/* recognize 4 ints using a grammar which recognizes only 2 */
#header <<#include "charbuf.h">>
ANTLR(a(), stdin); /* set up input stream, bufs, etc... */
a(); /* keep parsing, dude! */
#token "[\ \t\n]+" <<zzskip();>>
a : "[0-9]+" "[0-9]+"
Note that no tokens are lost between the end of the first
invocation of 'a' (via ANTLR(a(),stdin)) and the second
invocation (a()). Had you used another ANTLR call, the
lexical buffers would have been reset thereby losing the
token (3rd int in this case) already hanging out in the
 You want to run the same grammar on a different input stream.
Solution: use ANTLR(start_symbol(), stream) each time. Using
the above example, change main() to:
f = fopen("file1","r");
g = fopen("file2","r");
where file1 contains "1 2" and file2 contains "3 4", for
example. After having read the 2nd int during the first
invocation, the parser will get the EOF symbol for file1.
This is ignored (the current token LA(1) is reset) when the
second ANTLR invocation is given. This is why, when you invoke
another grammar using the ANTLR macro, it loses a token.
Note that the starting rule does not have to be the same each
Question: are you trying to switch input streams and return to the
same input stream after having processed the other? If so, you have
to save the state of ANTLR and DLG using some cool functions we have
in the new ANTLR release 1.10.
> Another feature I need is the ability to load
> multiple, independently generated parsers. Most parser, other than Bison,
> do not allow this.
The soon-to-be-released version of ANTLR/PCCTS, 1.10, allows parsers
to be "named." Specifically, multiple parsers can now be linked
together if the
directive is used--it prefixes all globally visible symbols with
"myname_". Future versions will take this a step further to take
advantage of C++'s information hiding.
ANTLR 1.10 will be out by the end of August if it kills me (hopefully
in time for those of you who teach courses with ANTLR). For more
information on ANTLR/PCCTS, please send email to our mail server,
with a blank "Subject:" line, at:
which will register you for info broadcasts etc...
If you wish to converse with a human, please contact me at:
AHPCRC, U of MN
People's Socialist Arctic Republic of Minnesota
Return to the
Search the comp.compilers archives again.