Skip to content

Instantly share code, notes, and snippets.

@jeroendeswaef
Last active August 29, 2015 14:05
Show Gist options
  • Save jeroendeswaef/563cd2ab68ab895aedff to your computer and use it in GitHub Desktop.
Save jeroendeswaef/563cd2ab68ab895aedff to your computer and use it in GitHub Desktop.
Antlr 4 + Python setup on ubuntu
#!/bin/sh
java -cp "/opt/antlr/antlr4/dist/antlr-4.4-complete.jar:$CLASSPATH" org.antlr.v4.Tool $*
from antlr4 import *
import sys
from HelloLexer import HelloLexer
from HelloParser import HelloParser
def main(argv):
input = FileStream(argv[1])
lexer = HelloLexer(input)
stream = CommonTokenStream(lexer)
parser = HelloParser(stream)
tree = parser.r()
if __name__ == '__main__':
main(sys.argv)
Python target support for ANTLR landed in version 4.4.
In this short writeup, I would like to document the installation process.
git clone git@github.com:parrt/antlr4-python2.git
mkdir -p /opt/antlr
cd /opt/antlr
git clone git@github.com:parrt/antlr4.git
git clone git@github.com:parrt/antlr4-python3.git
git clone git@github.com:parrt/antlr4-python2.git
cd antlr4
./bild.py mkjar
cd /opt/antlr/antlr4-python3
sudo python3 setup.py install
antlr4 -Dlanguage=Python3 Hello.g4
python3 execute.py hello-me
hello World
// Define a grammar called Hello
grammar Hello;
r : 'hello' ID ; // match keyword hello followed by an identifier
ID : [a-z]+ ; // match lower-case identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment