Read the following description carefully before starting on the assignment. Also, go through the brief overview of the project path that describes the steps involved. A list of what materials are available and what you are expected to submit are also given.
This assignment expects you to build a module to construct ASTs for Decaf programs. Recall the description of Decaf's syntax is given in its manual. Your AST construction will begin with the PLY-based lexical analyzer and parser you developed for HW2. Overall, your AST builder will take a single command-line argument, which is a file name; if that file contains a syntactically valid Decaf program, then it will construct an AST for that program. As an AST is being constructed, you will perform additional syntax checks (defined later in this document). Upon successful construction of the AST, it will then print the AST to standard output in a reasonable form (outlined later in this document). The AST itself should follow the structure described below.
Class Table: Contents
The class table is a collection of
records (you may implement this in any appropriate way: dictionaries,
tuples etc.). Each record in the table holds information about a
class. The necessary information for a class are:
The information about individual constructors, methods and fields themselves are stored as record structures with the following information:
Constructor record:
An array type is parameterized by its "base type", which is another type.
For instance:
Your AST builder or parser need not check whether the input program meets this restriction. You can simply assume that any program used with your AST builder will satisfy this restriction.
Your AST builder or parser need not check whether the input program meets this restriction. You can simply assume that any program used with your AST builder will satisfy this restriction.
class A { int x; A () { this.x = 0; } int f() { return this.x + 1; } public int g() { int i; i = this.f(); i++; return i; } } class B extends A { int y; public A s; B () { this.y = 2; this.s = new A(); } public int f(int k) { return super.f() + k; } }AST (printed out):
----------------------------------------------------------------------------- Class Name: A Superclass Name: Fields: FIELD 1, x, A, private, instance, int Constructors: CONSTRUCTOR: 1, private Constructor Parameters: Variable Table: Constructor Body: Block([ Expr( Assign(Field-access(This, x), Constant(Integer-constant(0))) ) ]) Methods: METHOD: 1, f, A, private, instance, int Method Parameters: Variable Table: Method Body: Block([ Return( Binary(add, Field-access(This, x), Constant(Integer-constant(1))) ) ]) METHOD: 2, g, A, private, instance, int Method Parameters: Variable Table: VARIABLE 1, i, local, int Method Body: Block([ Expr( Assign(Variable(1), Method-call(This, f, [])) ) , Expr( Auto(Variable(1), inc, post) ) , Return( Variable(1) ) ]) ----------------------------------------------------------------------------- Class Name: B Superclass Name: A Fields: FIELD 2, y, B, private, instance, int FIELD 3, s, B, private, instance, user(A) Constructors: CONSTRUCTOR: 2, private Constructor Parameters: Variable Table: Constructor Body: Block([ Expr( Assign(Field-access(This, y), Constant(Integer-constant(2))) ) , Expr( Assign(Field-access(This, s), New-object(A, [])) ) ]) Methods: METHOD: 3, f, B, private, instance, int Method Parameters: 1 Variable Table: VARIABLE 1, k, formal, int Method Body: Block([ Return( Binary(add, Method-call(Super, f, []), Variable(1)) ) ]) -----------------------------------------------------------------------------
As usual, complicated programs may not get the full grade unless accompanied by comments documenting it.
We will assume that HW3 will be submitted by the same teams that submitted HW3. If there is any change in the team structure, you must inform the instructor the Monday before the submission deadline (Mon. March 21) and get the submission repositories for the new teams set up.
You may submit the homework multiple times; the last submission will be graded.
By default, fields, methods and constructors are private. The Decaf manual makes this explicit for fields, but does not state this for methods and constructors. For this homework, you can choose the defaults for methods and constructors as you see fit; but making this uniform may be easiest.
The example has now been modified to include explicit public declarations; and the example print-out has been made consistent with Decaf manual's specs.
Modified: Wed Mar 16 23:24:10 EDT 2016:
Modified: Wed Mar 16 23:24:10 EDT 2016: