


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Project # Due 4/5/2005 8:00 PM
In this assignment you will build a set of functions to operate on nodes of an expression tree (also called an abstract syntax tree). The trees in this assignment can be used to represent expressions in a simple calculator. Here is a sample tree:
In this example, there are two binary operator nodes (+ and *), two integer nodes (42 and 29), and one variable node (abc).
There are two parts to this assignment. First you will implement the API described to build these trees. The purpose of this assignment is to provide practice writing pointer- based data structures in C. The second part of the assignment is to write test cases for this API. Your test cases will be graded based on two criteria.
The first grading criterion is the degree of code coverage provided by your tests. Code coverage is a metric of how many of the lines in your API implementation are executed by the test cases you write. The more lines of code your test cases are able to cover, the higher your score on this part of the assignment. To record code coverage, you will need to invoke the gcc compiler with the following additional options: - fprofile-arcs -ftest- coverage. These should be added to your CFLAGS macro in the project Makefile. You can use the lcov visual code coverage browser to see what see what statements were covered or not.
The second criterion in grading your test suite will be to use it to find bugs in a set of buggy implementations of the API that we will provide you. We will supply 3 tests cases in binary form (which will be the public tests for this assignment) and you will link your test suite to the buggy versions.
abc 29
ASTnode *createOperatorNode(opType operator, ASTnode *left, ASTNode *right) Create a new node with the passed children. Here are the operators and their definitions:
Operator Definition plusOperator Add the two sub-tress minusOperator Subtract left sub-tree from right multOperator Multiply the two sub-trees divOperator Perform integer division of the left / right sub-tree
ASTnode *createConstantNode(int constant) Create a leaf node of type constant.
ASTnode *createVariableNode(char *variableName) Create a symbolic variable expression. You should make a copy of the variableName character array.
ASTnode *copyTree(ASTnode *node) Create a copy of the passed tree. The copy should share no data with the item it is being copied from (i.e. copy all sub-trees and other dynamically allocated space).
void freeTree(ASTnode *node) Free the tree node rooted. This should free all children nodes (and any auxiliary memory such as variable name strings.
void printRPN(ASTnode *node) Print out the tree rooted at node using reverse polish notation (for example, the tree given in the above example would be printed as: (42 (abc 29 *) +)
void printInfix(ASTnode *node) Print out the tree rooted at node using infix notation. (for example, the tree given in the above example would be printed as: 42 + abc * 29. Your solution should only print parentheses when required by operator precedence. For example, you should print 42 + abc * 29 rather than 42 + (abc * 29).
int evaluateNode(ASTnode *node, int *result) Evaluate the tree rooted as node. If the evaluation is an integer value (i.e. there are no variable expressions in the tree), return a value of 0 and store the result of evaluating the expression in result. If the expression is contains one or more variable terms, then return -1.
int simplify(ASTnode *node, int subTree)
When you call evaluate, any expression that contains a variable expression that has a value in the associative array should return that value.