PA3: Decaf Parser
Objective
The goal of our semester-long project is to gain experience in compiler implementation by constructing a simple compiler.
The goal of this part of the project is to gain experience implementing syntax analysis by implementing a recursive-descent parser as the second pass of our Decaf compiler.
Introduction
The semester-long project for this course is to build a compiler for the Decaf language. In this project, you will implement a parser for Decaf, which will be the second phase in our compiler. You will do this by transforming the given Decaf grammar into an LL(1) grammar and implementing a recursive descent parser.
Thoroughly read the Decaf grammar in the reference document. For this assignment, you will need to construct a parser that will accept a stream of tokens and return an abstract syntax tree (AST). There are various AST objects, each of which represents some construct in the Decaf grammar.
You should download the project starter files from the "Files" tab in Canvas. The starter files include many new .java files as well as .class files representing compiled solutions.
Before you begin writing code, you should spend some time reading through all of the infrastructure files provided in the starter files. Our compiler this semester is a large project. Your solution will need to interface with several other classes, and you will need to thoroughly read through those classes to understand their interactions. Also, there are utility functions that you may find useful.
If you have questions about the existing code, please ask them on the Piazza forum (see the link on the sidebar in Canvas).
Assignment
WARNING: You should only proceed with actual development once you are SURE you understand exactly what your task is and how your code should interact with the rest of the system.
Implement the MyDecafParser
class by providing an implementation
for the following function:
public ASTProgram parseProgram(Queue<Token> tokens) throws InvalidSyntaxException
This function takes as input a Queue
of Token
objects from the lexing phase. The function should return an AST, rooted in an
ASTProgram
object. This function represents the root non-terminal
parsing routine in your recursive descent parser.
For this particular project, you should pay special attention to the following classes:
DecafParser
- Base class for your parser. Provides some useful helper methods for manipulating the queue of tokens.ASTNode
- Abstract syntax tree (AST) base class. All other AST node objects inherit from this one.InvalidSyntaxException
- Special exception that your code should throw when you detect invalid syntax.
You will first want to make sure you understand the grammar in the Decaf reference document. Then, you will want to spend some time modifying it so that it is more useful to you for this assignment (i.e., converting it to be LL(1) with proper precedence and associativity). Then, you should begin writing your parser, using your LL(1) grammar as a guide as discussed in class and the textbook.
Sample Input
def int add(int x, int y) { return x + y; } def int main() { int a; a = 3; return add(a, 2); }
Sample Output
Program [add.decaf:1:1] Function: add : int (x:int, y:int) [add.decaf:1:1] Block [add.decaf:2:1] Return [add.decaf:3:5] BinaryExpr: + [add.decaf:3:14] Location: x [add.decaf:3:12] Location: y [add.decaf:3:16] Function: main : int () [add.decaf:6:1] Block [add.decaf:7:1] Variable: a : int [add.decaf:8:5] Assignment: a = 3 [add.decaf:9:5] Location: a [add.decaf:9:5] Literal: 3 : int [add.decaf:9:9] Return [add.decaf:10:5] FunctionCall: add (a, 2) [add.decaf:10:12] Location: a [add.decaf:10:16] Literal: 2 : int [add.decaf:10:19]
Submission
DUE DATE: Friday, February 17, 23:59 EDT (11:59PM)
Please submit your .java
file to the appropriate assignment on
Canvas.