Assignment 1: ANTLR Configuration
Installation and Configuration
The following tutorial will use Ubuntu as operation system, the ANTLR requires JAVA (version 1.6 or higher), to install JDK, type following in your terminal:
$ sudo apt-get install openjdk-8-jdk
Let’s create a new directory and download the antlr library:
$ cd /usr/local/lib
$ wget http://www.antlr.org/download/antlr-4.5.3-complete.jar
Then add antlr-4.5.3-complete.jar to your environment variable CLASSPATH and create aliases for the ANTLR Tool::
$ export CLASSPATH=”.:/usr/local/lib/antlr-4.5.3-complete.jar:$CLASSPATH”
$ alias antlr4=’java -Xmx500M -cp “/usr/local/lib/antlr-4.5.3-complete.jar:$CLASSPATH” org.antlr.v4.Tool’
$ alias grun=’java org.antlr.v4.gui.TestRig’
You can add above lines into the bash file ~/.bashrc and the environment variable will be automatically loaded when you start.
Lexical analysis with ANTLR
Extract the assignment #1:
$ tar -zxvf a1.tar.gz
$ cd a1
In the directory a1, it contains those files:
A1Lexer.g4: Defining the grammar
Makefile: Compiling the generated java source files
test_cases: The directory contains a few test case files for debugging.
The current version of A1Lexer.g4 is a simple example that only supports a few grammar fragment. In your assignment, you need to create your own version of A1Lexer.g4 to meet the assignment requirements.
Now we can generate and compile with the grammar definition file A1Lexer.g4:
$ make # generate the java source files and token file, and compile the java source
The above commond will automatically generate the JAVA source files and token files. Then we can generate the lexical analysis results with test cases, let’s test the case of test_cases/demo0:
$ grun A1Lexer tokens -tokens test_cases/demo0 test_cases/demo0.out
The above command will generate the lexical analysis results in test_cases/demo0.out, it reads:
The text inside the ” is the matched keywords, and the token index in < indicates the type of the token that defined in A1Lexer.tokens.
If you modified the A1Lexer.g4 , you should re-compile the source files by using:
$ make clean # remove the compiled java sources files.
$ make # re-compile the updated source files.
Please create your own A1Lexer.g4 based on the grammar defined in the question: https://vault.sfu.ca/index.php/s/Rllquy5M9CEFNIo
Submit your assginment
Please rename your version of A1Lexer.g4 to <student_id.g4 and submit it via canvas.sfu.ca.
ANTLR documentation: https://github.com/antlr/antlr4/blob/master/doc/index.md