Skip to content

Instantly share code, notes, and snippets.

@mattak
Last active June 21, 2022 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattak/ef029fac196a53d833074cca153927ca to your computer and use it in GitHub Desktop.
Save mattak/ef029fac196a53d833074cca153927ca to your computer and use it in GitHub Desktop.
Simple script for using antlr4
#!/bin/sh
set -eu
VERSION="4.10.1"
JAR_DIR="/tmp/antlr4"
if [ ! -d "$JAR_DIR" ]; then
mkdir -p "$JAR_DIR"
fi
JAR_FILE="${JAR_DIR}/antlr-${VERSION}-complete.jar"
if [ ! -f "$JAR_FILE" ]; then
curl -L "https://www.antlr.org/download/antlr-${VERSION}-complete.jar" -o "${JAR_FILE}"
fi
CLASSPATH=".:${JAR_FILE}"
java -Xmx500M -cp "${JAR_FILE}:$CLASSPATH" org.antlr.v4.Tool $@
#!/bin/sh
set -eu
VERSION="4.10.1"
JAR_DIR="/tmp/antlr4"
if [ ! -d "$JAR_DIR" ]; then
mkdir -p "$JAR_DIR"
fi
JAR_FILE="${JAR_DIR}/antlr-${VERSION}-complete.jar"
if [ ! -f "$JAR_FILE" ]; then
curl -L "https://www.antlr.org/download/antlr-${VERSION}-complete.jar" -o "${JAR_FILE}"
fi
CLASSPATH=".:${JAR_FILE}"
javac -cp "$CLASSPATH" $@
#!/bin/sh
set -eux
VERSION="4.10.1"
JAR_DIR="/tmp/antlr4"
if [ ! -d "$JAR_DIR" ]; then
mkdir -p "$JAR_DIR"
fi
JAR_FILE="${JAR_DIR}/antlr-${VERSION}-complete.jar"
if [ ! -f "$JAR_FILE" ]; then
curl -L "https://www.antlr.org/download/antlr-${VERSION}-complete.jar" -o "${JAR_FILE}"
fi
CLASSPATH=".:${JAR_FILE}"
java -Xmx500M -cp "$CLASSPATH" org.antlr.v4.gui.TestRig $@
@mattak
Copy link
Author

mattak commented Jun 21, 2022

Download g4 files

$ mkdir tmp && cd tmp
$ curl -LO https://raw.githubusercontent.com/antlr/grammars-v4/master/kotlin/kotlin/KotlinLexer.g4
$ curl -LO https://raw.githubusercontent.com/antlr/grammars-v4/master/kotlin/kotlin/KotlinParser.g4
$ curl -LO https://raw.githubusercontent.com/antlr/grammars-v4/master/kotlin/kotlin/UnicodeClasses.g4

compile

$ antlr4 *.g4
$ antlr4javac *.java

download test file

$ curl -LO https://raw.githubusercontent.com/antlr/grammars-v4/master/kotlin/kotlin/examples/Test.kt

show tree cui

$ grun Kotlin kotlinFile -tree < Test.kt
(kotlinFile \n \n \n (preamble packageHeader importList) (topLevelObject (classDeclaration class (simpleIdentifier Foo) (classBody { \n \n (classMemberDeclaration (anonymousInitializer init (block { (statements (anysemi \n) (statement (blockLevelExpression (expression (disjunction (conjunction (equalityComparison (comparison (namedInfix (elvisExpression (infixFunctionCall (rangeExpression (additiveExpression (multiplicativeExpression (typeRHS (prefixUnaryExpression (postfixUnaryExpression (atomicExpression (simpleIdentifier foo)) (postfixUnaryOperation (callSuffix (valueArguments ( )))))))))))))))))))) (anysemi \n) (statement (declaration (propertyDeclaration val (variableDeclaration (simpleIdentifier c)) = (expression (disjunction (conjunction (equalityComparison (comparison (namedInfix (elvisExpre
...

show graph gui

$ grun Kotlin kotlinFile -gui < Test.kt

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment