74 lines
3.2 KiB
Markdown
74 lines
3.2 KiB
Markdown
|
# C++
|
||
|
|
||
|
The C++ target supports all platforms that can either run MS Visual Studio 2013 (or newer), XCode 7 (or newer) or CMake (C++11 required). All build tools can either create static or dynamic libraries, both as 64bit or 32bit arch. Additionally, XCode can create an iOS library.
|
||
|
|
||
|
## How to create a C++ lexer or parser?
|
||
|
This is pretty much the same as creating a Java lexer or parser, except you need to specify the language target, for example:
|
||
|
|
||
|
```
|
||
|
$ antlr4 -Dlanguage=Cpp MyGrammar.g4
|
||
|
```
|
||
|
|
||
|
You will see that there are a whole bunch of files generated by this call. If visitor or listener are not suppressed (which is the default) you'll get:
|
||
|
|
||
|
* MyGrammarLexer.h + MyGrammarLexer.cpp
|
||
|
* MyGrammarParser.h + MyGrammarParser.cpp
|
||
|
* MyGrammarVisitor.h + MyGrammarVisitor.cpp
|
||
|
* MyGrammarBaseVisitor.h + MyGrammarBaseVisitor.cpp
|
||
|
* MyGrammarListener.h + MyGrammarListener.cpp
|
||
|
* MyGrammarBaseListener.h + MyGrammarBaseListener.cpp
|
||
|
|
||
|
## Where can I get the runtime?
|
||
|
|
||
|
Once you've generated the lexer and/or parser code, you need to download or build the runtime. Prebuilt C++ runtime binaries for Windows (VS 2013 runtime), OSX and iOS are available on the ANTLR web site:
|
||
|
|
||
|
* http://www.antlr.org
|
||
|
|
||
|
Use CMake to build a Linux library (works also on OSX, if you don't have XCode, as we use pure C++ code). Building your own library on OSX or Windows is trivial, however. Just open the VS or XCode project, select target + arch and build it. Should work out of the box without any additional dependency.
|
||
|
|
||
|
|
||
|
## How do I run the generated lexer and/or parser?
|
||
|
|
||
|
Putting it all together to get a working parser is really easy. Look in the [runtime/Cpp/demo](../runtime/Cpp/demo) folder for a simple example. The [README](../runtime/Cpp/demo/README.md) there describes shortly how to build and run the demo on OSX, Windows or Linux.
|
||
|
|
||
|
## How do I create and run a custom listener?
|
||
|
|
||
|
The generation step above created a listener and base listener class for you. The listener class is an abstract interface, which declares enter and exit methods for each of your parser rules. The base listener is implements all those abstract methods with an empty body, so you don't have to do it yourself if you just want to implement a single function. Hence use this base listener as the base class for your custom listener:
|
||
|
|
||
|
```c++
|
||
|
#include <iostream>
|
||
|
|
||
|
#include "antlr4-runtime.h"
|
||
|
#include "MyGrammarLexer.h"
|
||
|
#include "MyGrammarParser.h"
|
||
|
#include "MyGrammarBaseListener.h"
|
||
|
|
||
|
using namespace org::antlr::v4::runtime;
|
||
|
|
||
|
class TreeShapeListener : public MyGrammarBaseListener {
|
||
|
public:
|
||
|
void enterKey(Ref<ParserRuleContext> ctx) {
|
||
|
// Do something when entering the key rule.
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
int main(int argc, const char* argv[]) {
|
||
|
std::wifstream stream;
|
||
|
stream.open(argv[1]);
|
||
|
ANTLRInputStream input("ae");
|
||
|
MyGrammarLexer lexer(&input);
|
||
|
CommonTokenStream tokens(&lexer);
|
||
|
MyGrammarParser parser(&tokens);
|
||
|
|
||
|
Ref<tree::ParseTree> tree = parser.key();
|
||
|
Ref<TreeShapeListener> listener(new TreeShapeListener());
|
||
|
tree::ParseTreeWalker::DEFAULT->walk(listener, tree);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
```
|
||
|
|
||
|
This example assumes your grammar contains a parser rule named `key` for which the enterKey function was generated. The `Ref<>` template is an alias for `std::shared_ptr<>` to simplify the runtime source code which often makes use of smart pointers.
|