Last active
August 29, 2015 14:06
-
-
Save danieldietrich/05edf7bc6f71499da922 to your computer and use it in GitHub Desktop.
Antlr Parse Result Printer
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/bash | |
| ############################################################################ | |
| ## !! WARNING !! This script deletes .java files in the current directory ## | |
| ############################################################################ | |
| # | |
| # Properties | |
| # | |
| ANTLR_JAR="antlr-4.4-complete.jar" | |
| MD5=md5 # may be md5sum | |
| # | |
| # Function declarations | |
| # | |
| function execute() { | |
| eval "$*" | |
| if [ $? -ne 0 ] ; then | |
| exit 1 | |
| fi | |
| } | |
| function cleanup() { | |
| GRAMMAR=$1 | |
| rm -f ./"$GRAMMAR"*.java | |
| rm -f ./"$GRAMMAR"*.class | |
| rm -f ./"$GRAMMAR"*.tokens | |
| rm -f ./"$GRAMMAR"*.md5 | |
| } | |
| function ensureExists() { | |
| FILE=$1 | |
| if [ ! -f "$FILE" ] ; then | |
| echo "$FILE does not exist." | |
| exit 1 | |
| fi | |
| } | |
| function hasChanged() { | |
| FILE=$1 | |
| FILE_MD5="${FILE}.md5" | |
| if [ ! -f "$FILE_MD5" ] || [ "$(cat "$FILE_MD5")" != "$($MD5 "$FILE")" ] ; then | |
| return 0 # true(!) | |
| else | |
| return 1 # false(!) | |
| fi | |
| } | |
| function markNotChanged() { | |
| FILE=$1 | |
| FILE_MD5="${FILE}.md5" | |
| $MD5 "$FILE" > "$FILE_MD5" | |
| } | |
| # | |
| # Check arguments | |
| # | |
| if [ $# -eq 1 ] && [ "$1" == "clean" ] ; then | |
| echo "Cleaning all generated files..." | |
| cleanup "" | |
| exit 0 | |
| elif [ $# -eq 2 ] && [ "$1" == "clean" ] ; then | |
| GRAMMAR=$2 | |
| GRAMMAR_FILE="${GRAMMAR}.g4" | |
| echo "Cleaning generated files of ${GRAMMAR_FILE}..." | |
| cleanup "$GRAMMAR" | |
| exit 0 | |
| elif [ $# -eq 2 ] || [ $# -eq 3 ] ; then | |
| GRAMMAR=$1 | |
| GRAMMAR_FILE="${GRAMMAR}.g4" | |
| START_RULE=$2 | |
| INPUT_FILENAME=$3 | |
| ensureExists "$GRAMMAR_FILE" | |
| # | |
| # Download antlr4 jar if necessary | |
| # | |
| if [ ! -f "./${ANTLR_JAR}" ] ; then | |
| execute "curl -O http://www.antlr.org/download/${ANTLR_JAR}" | |
| fi | |
| # | |
| # Check for grammar changes | |
| # | |
| if hasChanged "$GRAMMAR_FILE" ; then | |
| echo "Generating ${GRAMMAR_FILE}..." | |
| cleanup "$GRAMMAR" | |
| execute "java -jar ${ANTLR_JAR} ${GRAMMAR_FILE}" | |
| execute "javac -cp ${ANTLR_JAR} ${GRAMMAR}*.java" | |
| markNotChanged "${GRAMMAR_FILE}" | |
| fi | |
| # | |
| # Parse input | |
| # | |
| if [ -z "$INPUT_FILENAME" ] ; then | |
| echo "Please enter input to be parsed and press <Ctrl>-D when finished." | |
| fi | |
| execute "java -cp .:${ANTLR_JAR} org.antlr.v4.runtime.misc.TestRig ${GRAMMAR} ${START_RULE} -tree ${INPUT_FILENAME}" | |
| exit 0 | |
| else | |
| echo "Usage:" | |
| echo "- run parser of MyGrammar : run MyGrammar startRule [input filename]" | |
| echo "- clean generated files of MyGrammar : run clean MyGrammar" | |
| echo "- clean all generated files : run clean" | |
| exit 1 | |
| fi |
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
| $ alias antlr=./antlr.sh | |
| $ antlr Issue118 start | |
| Please enter input to be parsed and press <Ctrl>-D when finished. | |
| a 1 b | |
| <Ctrl>-D | |
| (start a 1 b) | |
| $ echo "a 1 b" > test.txt | |
| $ antlr Issue118 start test.txt | |
| (start a 1 b) |
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
| grammar Issue118; | |
| start : ID | ID INT ID ; | |
| ID : [a-z]+ ; | |
| INT : [0-9]+ ; | |
| WS : [ \r\n\t]+ -> skip ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment