ANTLR C++ Target

This target is incorporated into the 3.5 ANTLR release.

Here are two examples using the C++ target (option language=Cpp).

The C++ Target was mainly written in order to incorporate the C++ way of doing things into ANTLR. The C Target instructions work as it is with the C++ Target, with some additional stuffs. 

a) @namespace is a section which can be defined to provide the namespace, in which the new parser/lexer should get generated.The code below, if put into lexer and parser will create them in this namespace.

eg.

@namespace { User }

b) The C++ Target is a header-only library and so installation is not required. Just add it to the include directories.

Cpp.tar.gz

c) It works on both Windows and Linux and tested on Visual Studio 2010, Visual Studio 2008 and g++ 4.4.6. It should work with other compilers without much issue.

d) Almost everything in the C++ Library can be customized to the user's requirements. They should be provided in the form of Traits named after the lexer and parser to be generated. The attachment above also provides two examples explaining the same.

e) In order to build the C++ Target, copy the Cpp.stg and CppTarget.java files into the appropriate directories( {ANTLR_INSTALL_DIR}\tool\src\main\resources\org\antlr\codegen\templates\CPP and {ANTLR_INSTALL_DIR}\tool\src\main\java\org\antlr\codegen) and build antlr using Maven. It was built for antlr-3.4 and an Uber Jar can be found below

antlr-3.4-with-cpp.jar

f) Because of the meta-programming involved, the C++ Target parser requires the C++ Target Lexer to be included and both the lexer and parser requires the Traits to be included, which can be seen from the examples in the attachment.

g) New Changes to the members of the lexer and parser can be also done using the traits by overriding the types BaseLexerType and BaseParserType. It can be also done using the @members section in the parser.

h) There is an another new section called @traits. This is for users who want to do custom implementation of inner ANTLR classes. For TLexer and TParser, the no-override traits section will look like this

@lexer::traits {

     class TLexer; class TParser;

     typedef Traits< TLexer, TParser > TLexerTraits;

     typedef TLexerTraits TParserTraits;

}

i) There is a TokenStream implementation in the C++ Target, which deletes the tokens after the tokens are condensed into a rule( except the start and stop tokens). This might be useful for those who parse a large texts. It is explained in one of the example. If the above grammar wants to use, the traits definition should be changed to

@lexer::traits {

class TLexer;  class TParser; 

template<class ImplTraits>

class UserTraits : public antlr3::CustomTraitsBase<ImplTraits> {

public:
static const bool TOKENS_ACCESSED_FROM_OWNING_RULE = true;
};

typedef antlr3::Traits< TLexer, TParser, UserTraits > TLexerTraits;

typedef TLexerTraits TParserTraits;

}

j) There is a small difference between Cpp target and Java Target. Since C++ Target has to work with a header file and a source file, @members section in Java is split into @context (for inserting into header file) and @members (for inserting into cpp file). This is consistent with C-Target behavior

Any suggestions on the API are welcome.