A parse tree is a tree constructed from ParseNode objects.
The ParseNode class is an abstract class.
It has a main program and some static utility functions and it defines
an abstract value()
instance method to be implemented by
its concrete subclasses.
The ParseNode class has five concrete subclasses:
value()
method returns this constant.
value()
method returns the sum of the children's
values.
value()
method returns the difference of the
children's values.
value()
method returns the product of the children's
values.
value()
method returns the quotient of the children's
values.
/** * A ParseNode is a node in a parse tree. * For subclasses that define child nodes, a node can also represent the entire * parse tree that is rooted at the node. */ public abstract class ParseNode extends Object { // Implementation: Step 1, static method. private static void printValue(String nm, ParseNode pn) { System.out.println("The value of " + nm + " is " + pn.value()); } // Implementation: incremental, static method. // For this assignment you can ignore the String[] args parameter. public static void main(String[] args) { // Implementation: Step 1. ParseNode n1 = new Constant(7); printValue("n1", n1); // Implementation: Step 2. ParseNode n2 = new Constant(13); ParseNode n3 = new Sum(n1, n2); printValue("n3", n3); ParseNode n4 = new Constant(5); ParseNode n5 = new Sum(n3, n4); printValue("n5", n5); // Implementation: Step 3. ParseNode n6 = new Constant(3); ParseNode n7 = new Difference(n5, n6); printValue("n7", n7); // Implementation: Step 4. ParseNode n8 = new Constant(4); ParseNode n9 = new Product(n7, n8); printValue("n9", n9); // Implementation: Step 5. ParseNode n10 = new Constant(2); ParseNode n11 = new Quotient(n9, n10); printValue("n11", n11); } // Implementation: never implemented. // A compiler just uses this for type checking and making sure that a // concrete class that is declared to extend ParseNode does actually // implement a value() method. // The compiler also assigns a member for the value() method in instance // structures for all instances of classes that extend ParseNode. // This is the basic inheritance mechanism that ensures that methods from // instances of all subclasses can be invoked in the same way. // The address of the value() method is assigned displacement 0. /** * pn.value() returns the value of the parse tree rooted at pn. */ public abstract int value(); } // public class ParseNode
// Implementation: this class is implemented in Step 1. /** * A Constant is a ParseNode that has a constant value. * The value is determined when it is constructed. */ public class Constant extends ParseNode { // Instance struct displacement: 4 private int constant; // Constructor. /** * new Constant(c) returns returns a new Constant with value c. */ public Constant(int c) { constant = c; } // Instance struct displacement: 0 (inherited from ParseNode) /** * pn.value() returns the value of the parse tree rooted at pn. */ public int value() { return constant; } } // public class Constant
// Implementation: this class is implemented in Step 2. /** * A Sum is a ParseNode that has a left child and a right child. * Its children are specified when it is constructed. * Its value is the sum of the values of its children. */ public class Sum extends ParseNode { // Instance struct displacement: 4 private ParseNode left; // Instance struct displacement: 8 private ParseNode right; // Constructor. /** * new Sum(l, r) returns returns a new Sum with children l and r. */ public Sum(ParseNode l, ParseNode r) { left = l; right = r; } // Instance struct displacement: 0 (inherited from ParseNode) /** * pn.value() returns the value of the parse tree rooted at pn. */ public int value() { return left.value() + right.value(); } } // public class Sum
// Implementation: this class is implemented in Step 3. /** * A Difference is a ParseNode that has a left child and a right child. * Its children are specified when it is constructed. * Its value is the difference of the values of its children. */ public class Difference extends ParseNode { // Instance struct displacement: 4 private ParseNode left; // Instance struct displacement: 8 private ParseNode right; // Constructor. /** * new Difference(l, r) returns returns a new Difference with children * l and r. */ public Difference(ParseNode l, ParseNode r) { left = l; right = r; } // Instance struct displacement: 0 (inherited from ParseNode) /** * pn.value() returns the value of the parse tree rooted at pn. */ public int value() { return left.value() - right.value(); } } // public class Difference
// Implementation: this class is implemented in Step 4. /** * A Product is a ParseNode that has a left child and a right child. * Its children are specified when it is constructed. * Its value is the product of the values of its children. */ public class Product extends ParseNode { // Instance struct displacement: 4 private ParseNode left; // Instance struct displacement: 8 private ParseNode right; // Constructor. /** * new Product(l, r) returns returns a new Product with children * l and r. */ public Product(ParseNode l, ParseNode r) { left = l; right = r; } // Instance struct displacement: 0 (inherited from ParseNode) /** * pn.value() returns the value of the parse tree rooted at pn. */ public int value() { return left.value() * right.value(); } } // public class Product
// Implementation: this class is implemented in Step 5. /** * A Quotient is a ParseNode that has a left child and a right child. * Its children are specified when it is constructed. * Its value is the quotient of the values of its children. */ public class Quotient extends ParseNode { // Instance struct displacement: 4 private ParseNode left; // Instance struct displacement: 8 private ParseNode right; // Constructor. /** * new Quotient(l, r) returns returns a new Quotient with children * l and r. */ public Quotient(ParseNode l, ParseNode r) { left = l; right = r; } // Instance struct displacement: 0 (inherited from ParseNode) /** * pn.value() returns the value of the parse tree rooted at pn. */ public int value() { return left.value() / right.value(); } } // public class Quotient