[antlr-interest] Creating a main() for C++ code, passing params on command line

Sam Barnett-Cormack sdb at geekworld.co.uk
Sat Apr 15 15:31:12 PDT 2006


Paul D Watson wrote:
> 
> It is clearly evident that I am new to antlr.  I have searched quite a
> bit through the ANTLR Reference Manual and with Google to find out about
> how a main() is created for antlr generated programs.  There are
> main.cpp and Main*.cpp files around in the examples directory.  But I am
> hoping to find some reference documentation that will tell what must, or
> must not, go into a main() for antlr generated programs.

ANTLR doesn't generate main() methods. This is because it doesn't
generate programs, it generates code in your chosen language to do
parsing, that is then compiled to object code (or class file or
whatever, as appropriate) to be used by *your* program. That is, you
have to wrap the lexer/parser in your own code to make use of it. After
all, there's only so much the parser can do in terms of semantics, your
code would do more of that.

> Ultimately, I would like to pass parameters on the command line for the
> parser and lexer to use.  Things such as debug_level to indicate the
> amount of debug output desired or language_dialect which would allow the
> same processor to handle multiple dialects of a language.  The only way
> I see to do this now is to embed them into the grammar .g file.

Well, if you want the lexer/parser to use command line params, you would
have to embed that into a .g file. However, all the rest of that would
be in your other code.

That aside, you could have antlr .g files creating a runnable program by
putting an embedded main() method into one of them...

Sam


More information about the antlr-interest mailing list