You are on page 1of 5

HCMC University of Technology

Faculty of Computer Science & Engineering

Assignment 2
AST Generator

Author

Dr. Nguyen Hua Phung

September 18, 2016

Contents
1 Specification

2 Submission
2.1 Phase 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2 Phase 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3
3
4

Assignment 2
version 1.1

After completing this assignment, you will be able to


explain the stucture of parser generated by ANTLR
manipulate (traverse, create) on tree, which has different types of node, using Visitor
write a short program in Scala to generate intermediate code (AST) for a valid
BKOOL program

Specification

In this assignment, you are required to write an AST generator for a program written in
BKOOL. To complete this assignment, you need to:
investigate the structure of a parser generated by ANTLR
try to find out how to generate an AST from a parse tree.
investigate the AST classes defined in Scala.
modify ASTGeneration.scala to generate an AST.
To complete this assignment, you need to:
download initial code (assignment2.zip) and unzip it.
have a look at the folder bkool/utils that contains 2 files AST.scala and Visitor.scala.
AST.scala defines all classes in AST while Visitor.scala is not used in this Assignment
but for the next one.
replace BKOOL.g4 in the folder grammar by your file.
modify bkool/astgen/ASTGeneration.scala to make it generate AST from a parse tree
generated from ANTLR

Submission

2.1

Phase 1

In this phase, you are required to submit at least 3 testcases together with their solutions,
which are the corresponding AST when they are printed. The name of a testcase is a
number with ".txt" extension. For example, 1.txt, 2.txt are valid names of testcases. The
names of testcases must be increasing from 1. The name of a solution is the same number
as the corresponding testcase followed by a "s.txt". For example, 1s.txt is the solution of
the testcase 1.txt. The testcase contains a valid BKOOL program while the solution is the
corresponding AST. For example,
Testcase 1.txt
c l a s s a {}
Solution 1s.txt
Program ( [ C l a s s D e c l ( " a " , [ ] ) ] )
Testcase 2.txt
c l a s s b {}
c l a s s a extends b {
length : float ;
s t a t i c f i n a l int num = 1 ;
int getNum ( ) { return num ; }
}
Solution 2s.txt
Program ( [ C l a s s D e c l ( b , [ ] ) , C l a s s D e c l ( a , b , [ A t t r i b u t e D e c l ( I n s t a n c e , VarDecl (
l e n g t h , FloatType ) ) , A t t r i b u t e D e c l ( S t a t i c , ConstDecl (num , IntType , I n t L i t e r a l ( 1 ) ) ) ,
MethodDecl ( getNum , I n s t a n c e , [ ] , Block ( [ ] , [ Return ( Id (num ) ) ] ) ) ] ) ] )

Among 3 testcases, there must be at least 1 short program (whose AST contains around
3-5 objects of AST), 1 medium program (whose AST contains around 8-20 objects of
AST) and 1 larger program (whose AST contains more than 20 objects including at least
AttributeDecl, MethodDecl, Block, For, If, Assign, Return, BinaryOp, FieldAccess objects).
You are not allowed to use the above example as your testcases.
This phase is not marked but it is a prerequisite for Phase 2, i.e. if students do not
submit Phase 1, they will not be allowed to submit for Phase 2. To complete this phase, students are expected to investigate carefully AST.scala, draw AST for each testcase, predict
how the AST is printed to write manually the corresponding solution.
The deadline of Phase 1 is 16:00 Sept 24th, 2016. All your testcases and solutions
are compressed into an archive naming "phase1.zip" and just submit this file.

2.2

Phase 2

This phase requires you submit your code in 2 files: BKOOL.g4 and ASTGeneration.scala.
You must make sure that your code can generate the valid AST for any correct BKOOL
program.
The deadline of this assignment is 16:30 Apr 2nd, 2016. You are required to submit
just only 2 files BKOOL.g4 and ASTGeneration.scala. Note that you must NOT compress
your files when submit them.
The operating system when cheking the submission is Linux. Make sure that your program can be compiled and run in the environment.
You must complete the assignment by yourself and do not let your work seen by someone
else, otherwise, you will be punished by the university rule for plagiarism.

You might also like