C++ NotesThe C++ runtime and generated grammars look very much the same as the java ones. There are some subtle differences though, but more on this later. Building the runtimeThe runtime files are located in the lib/cpp subdirectory of the ANTLR distribution. This release is the first to include preliminary automake/autoconf support. Building it is in general done by doing the following: ./configure --prefix=/usr/local make Installing the runtime is done by typing make installThis installs the runtime library libantlr.a in /usr/local/lib and the header files in /usr/local/include/antlr. Using the runtimeGenerally you will compile the ANTLR generated files with something similar to:c++ -c MyParser.cpp -I/usr/local/includeLinking is done with something similar to: c++ -o MyExec <your .o files> -L/usr/local/lib -lantlr Getting ANTLR to generate C++To get ANTLR to generate C++ code you have to add language="Cpp";to the global options section. After that things are pretty much the same as in java mode except that a all token and AST classes are wrapped by a reference counting class (this to make live easier). The reference counting class uses operator->to reference the object it is wrapping. As a result of this you use -> in C++ mode in stead of the '.' of java. See the examples in examples/c++ for some illustrations. Using Custom AST typesIn C++ mode it is also possible to override the AST type used by the code generated by ANTLR. To do this you have to do the following:
Using Heterogeneous AST typesThis is largely untested. Small examples seem to work. Functionality from duptree and the likes will not work, this may be fixed in the next release, in general inspection of the trees will work, transformations 90% sure not.. Basically follow the java instructions and look at the generated code. If someone would be willing to share some experiences? A template grammar file for C++header "pre_include_hpp" { // gets inserted before antlr generated includes in the header file } header "post_include_hpp" { // gets inserted after antlr generated includes in the header file // outside any generated namespace specifications } header "pre_include_cpp" { // gets inserted after the antlr generated includes in the cpp file } header "post_include_cpp" { // gets inserted after the antlr generated includes in the cpp file } header { // gets inserted after generated namespace specifications in the header // file. But outside the generated class. } options { language="Cpp"; namespace="something"; // encapsulate code in this namespace // namespaceStd="std"; // cosmetic option to get rid of long defines // in generated code // namespaceAntlr="antlr"; // cosmetic option to get rid of long defines // in generated code genHashLines = true; // generated #line's or turn it off. } { // global stuff in the cpp file ... } class MyParser extends Parser; options { exportVocab=My; } { // additional methods and members ... } ... rules ... { // global stuff in the cpp file ... } class MyLexer extends Lexer; options { exportVocab=My; } { // additional methods and members ... } ... rules ... { // global stuff in the cpp file ... } class MyTreeParser extends TreeParser; options { exportVocab=My; } { // additional methods and members ... } ... rules ... Version: $Id: //depot/code/org.antlr/release/antlr-2.7.1/doc/cpp-runtime.html#3 $ |