Assignment 2: Environment configuration


Rate this product

Assignment 2: Environment configuration
ANTLR Installation and Configuration
You can ignore this session if you have aleardy configured ANTLR
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
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.

GraphViz installization
The package of GraphViz will be used to generate the AST tree figure for syntax analysis. If you use CSIL workstations, the graphviz has installed. To install the graphviz, type following line in Ubuntu terminal:

$ sudo apt-get install graphviz
Syntax analysis with ANTLR
Extract the assignment #1:

$ tar -zxvf a2.tar.gz
$ cd a2
In the directory a2, it contains those files:

A2Syntax.g4: Defining the grammar items
Makefile: Compiling the generated java source files Automatically run syntax analyizer on test_cases and generate the syntax tree figure.
test_cases: The directory contains a few test case files for debugging.
The current version of A2Syntax.g4 is a simple example that only supports a simple syntax grammer. In your assignment, you need to create your own version of A2Syntax.g4 to meet the assignment requirements.

Generate and compile with the grammar definition file A2Syntax.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 run the syntax analyzer with test cases, let’s test the case of test_cases/simple_assign.txt:

class Program {
int a,b;
void call ( )
b = 0;
a = a / b;
a = a + 1;
To parse the above source code, we need to modify the file of A2Syntax.g4, inside the A2Syntax.g4, we have created the API for generating the tree structure and the pre-defined lexical matching rules, you only need to add the grammer rules. For example, if we want to parse the syntax of line a=a/b, we need the rules of:

<statement – <loc <assign_op <expr; # ‘a=a/b’, ‘a’ is <loc, ‘=’ is <assign_op, ‘a/b’ is the <expr
<expr – <expr <bin_op <expr # ‘a/b’, ‘a’ and ‘b’ are both <expr, ‘/’ is <bin_op
<expr – <location
<location – <id
According to above rules, we can add those grammer definitions to A2Syntax.g4:

// <statement – <location <assign_op <expr ;
statement returns [int id] // the statement instance returns the exclusive id
: location eqOp expr ‘;’ // the patterns of keywords or (lexical tokens)
$id = PrintNode(“Assign”); // Node title “Assign”, variable starts with ‘$’
PrintEdge($id, $; // first child, link the statement node to the location node by id
PrintEdge($id, PrintNode($eqOp.text)); // second child, link the nodes and print its attribute (text)
PrintEdge($id, $; // third child

// <expr – <expr <bin_op <expr
// <expr – <location
// <expr – <literal
expr returns [int id]
: e1=expr binOp e2=expr { // <expr – <expr <bin_op <expr, If the keywords
// has same type <expr, rename to different items
$id = PrintNode(“Bin_expr”);
PrintEdge($id, $;
PrintEdge($id, PrintNode($binOp.text));
PrintEdge($id, $;
| location { // <expr – <location
$id = PrintNode(“Loc_expr”);
PrintEdge($id, $;
|literal { // <expr – <literal
$id = PrintNode(“Const_expr”);
PrintEdge($id, PrintNode($literal.text));

// <location – <id
location returns [int id]
:Ident // Match the lexical token (‘Ident’)
$id = PrintNode(“Loc”);
PrintEdge($id, PrintNode($Ident.text));
The A2Syntax.g4 have already defined the remaining grammers, now we can run our syntax analyzer on the test case of test_cases/simple_assign.txt:

$ python test_cases/simple_assign.txt test_cases/simple_assign.png
Now the syntax figure is generated and can be found in test_cases/simple_assign.png, it depicts the syntax tree:

Syntax Tree

If you modified the A2Syntax.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 A2Syntax.g4 based on the grammar defined in the question:, Note that in A2Syntax.g4, we have already create the tree plotting code (API) and Lexical matching, you should not modify those sessions, you only need to add your grammer rules in A2Syntax.g4 (line 118 – line 369, Session 2)

Submit your assginment
Please rename your version of A2Syntax.g4 to <student_id.g4 and submit it via

ANTLR documentation:

Open chat
Need help?
Can we help you?