Parsers generated for Python require the same version of the runtime as the ANTLR tool used. Right now the Python target is only functional up to v3.1.3, so you'll have to use that version of ANTLR and the runtime. The |
The runtime package for Python is called antlr3
. The package is pure Python and does not have any dependencies to non-standard modules.
wget http://www.antlr.org/download/antlr-3.1.3.tar.gz tar xzf antlr-3.1.3.tar.gz cd antlr-3.1.3/runtime/Python sudo python setup.py install |
$ python Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import antlr3 >>> |
The package structure tries to mimic the Java runtime as close as possible. Translating import statements from Java to Python should be straight forward:
Java |
Python |
||
---|---|---|---|
|
|
The only difference is that a number of static names, which are members of various Java classes, are defined in the module namespace:
Java |
Python |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The API should be mostly identical to Java, with a few exceptions:
CommonToken(type=None, channel=DEFAULT_CHANNEL, text=None, input=None, start=None, stop=None, oldToken=None)
TreeAdaptor.createWithPayload(token)
TreeAdaptor.createFromToken(typeType, fromToken, text=None)
TreeAdaptor.createFromType(typeType, text)
There is also full API documentation available for the Python runtime.