Python runtime
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 python-antlr
packages available with distributions like Ubuntu are not current. Install the appropriate runtime for your version of ANTLR using the instructions below.
Python runtime package
The runtime package for Python is called antlr3
. The package is pure Python and does not have any dependencies to non-standard modules.
Installation
- Download the "Complete ANTLR 3.x Java binaries jar" from the ANTLR download page, extract it, and run the Python installer.
- On a Linux shell:
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
- On a Linux shell:
- Check your installation by using the Python shell:
$ 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 >>>
Differences from Java runtime
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 |
---|---|
import org.antlr.runtime.*; import org.antlr.runtime.tree.CommonTree; |
from antlr3 import * from antlr3.tree import CommonTree |
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:
- The CommonToken constructor has a different signature than the (overloaded) Java version. It is advised to call it always with keyword arguments to avoid confusion:
CommonToken(type=None, channel=DEFAULT_CHANNEL, text=None, input=None, start=None, stop=None, oldToken=None)
- The overloaded TreeAdaptor.create() is available, but should not be used directly. Instead you should use
TreeAdaptor.createWithPayload(token)
TreeAdaptor.createFromToken(typeType, fromToken, text=None)
TreeAdaptor.createFromType(typeType, text)
There is also full API documentation available for the Python runtime.