CS 340 - Assignment 5

Due: Friday, November 16th by 11:59 PM

Abstract Syntax Trees

In Assignment 4, you implemented a recursive-descent parser for the YCPL language.  The syntax trees constructed by your parser precisely match the grammar rules used to derive the input strings accepted by the parser, and contain all of the nonterminal and terminal symbols involved in the derivation.

In order to implement an interpreter or compiler for a language, it is generally possible to use the syntax tree as a representation of the program.  However, the full syntax tree often contains more information than is necessary, and its structure may not be particularly convenient.  For these reasons, interpreters and compilers typically construct an abstract syntax tree (AST) as a more compact representation of the program.  An abstract syntax tree is, as the name suggests, a higher-level representation of the program which simplifies the structure of the program, and discards extraneous details.

Your Task

Your task is to write methods to translate full syntax trees (ParseNodes) into abstract syntax trees (Expressions).

Because YCPL is a functional language, expressions are the most important construct in the language.  Therefore, its ASTs are based on expressions.

In the ExpressionImpl class, you will see a static method called convertExpressionToAST.  The job of this method, which you must implement, is to take ParseNode representing the full syntax tree of an expression (the parse node's type is EXPRESSION), and return an Expression object, which is the abstract syntax tree for the expression.

There are 6 kinds of Expression nodes in an AST, each corresponding to a specific grammar rule:

Expression type Grammar Rule Explanation
AssignmentExpression expression →
IDENTIFIER ASSIGNMENT expression
Represents an assignment of a value to a variable.  The identifier and the expression should be attached to the AssignmentExpression node.
FunctionCallExpression expression →
IDENTIFIER LEFT_PARENTHESIS opt_arg_list RIGHT_PARENTHESIS
Represents a call to a function.  The identifier (function name) and each argument expression should be attached to the FunctionCallExpression node, in order.
FunctionExpression expression →
KEYWORD("func") LEFT_PARENTHESIS opt_param_list RIGHT_PARENTHESIS LEFT_BRACE statement RIGHT_BRACE
Represents a function.  Each parameter should be attached to the FunctionExpression node, in order.  Also, the expression that is a child of the statement should be attached.
IfThenElseExpression expression →
KEYWORD("if") LEFT_PARENTHESIS expression RIGHT_PARENTHESIS KEYWORD("then") expression KEYWORD("else") expression
Represents an if/then/else construct.  The expressions representing the condition, the "then" expression, and the "else" expression, should each be attached to the IfThenElseExpression node.
IntegerLiteralExpression expression → INTEGER_LITERAL Represents a literal integer value.  The value of the literal integer should be set in the IntegerLiteralExpression node using the setValue method.
VariableReferenceExpression expression → IDENTIFIER Represents a variable reference.  The identifier should be attached to the VariableReferenceExpression node.

Getting started

The assignment is designed to be completed within the Eclipse Java IDE.

Download CS340_Assign5.zip.  Within Eclipse, choose File->Import...->Existing Projects into Workspace.  Click Select archive file, Browse..., choose CS340_Assign5.zip, and click Finish.  You should see a new project called ycpl in the Package Explorer.

You will need to modify LexerImpl.java so that it contains the changes you made as part of Assignment 2.

You will need to modiify ParserImpl.java so that it contains the changes you made as part of Assignment 4.

Testing

Run the tests in ASTTests.java.  You are done when all tests pass.

As with the previous assignment, when an AST your program has constructed does not match the expected AST, the output printed to the Eclipse console window will show the structure of your tree and the expected tree, so you can compare them for differences.

Submitting

When you are done:

Export your project to a zip file.  In Eclipse, right-click on the project ycpl in the Package Explorer, and choose Export...->Archive File.  Enter the name/path of the zip file you want to save your project in.  Click Finish.

Upload your saved zip file to the Marmoset server as Project 5.