Last active
August 29, 2015 14:05
-
-
Save jeroendeswaef/563cd2ab68ab895aedff to your computer and use it in GitHub Desktop.
Antlr 4 + Python setup on ubuntu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
java -cp "/opt/antlr/antlr4/dist/antlr-4.4-complete.jar:$CLASSPATH" org.antlr.v4.Tool $* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Python target support for ANTLR landed in version 4.4. | |
In this short writeup, I would like to document the installation process. | |
git clone [email protected]:parrt/antlr4-python2.git | |
mkdir -p /opt/antlr | |
cd /opt/antlr | |
git clone [email protected]:parrt/antlr4.git | |
git clone [email protected]:parrt/antlr4-python3.git | |
git clone [email protected]: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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hello World |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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