import java.io.*; import java.util.*; // ********************************************************************** // The ASTnode class defines the nodes of the abstract-syntax tree that // represents a C-- program. // // Internal nodes of the tree contain pointers to children, organized // either in a linked list (for nodes that may have a variable number // of children) or as a fixed set of fields. // // The nodes for literals and ids contain line and character number // information; for string literals and identifiers, they also contain a // string; for integer literals, they also contain an integer value. // // Here are all the different kinds of AST nodes and what kinds of children // they have. All of these kinds of AST nodes are subclasses of "ASTnode". // Indentation indicates further subclassing: // // Subclass Kids // -------- ---- // ProgramNode DeclListNode // DeclListNode linked list of DeclNode // DeclNode: // VarDeclNode TypeNode, IdNode, int // FnDeclNode TypeNode, IdNode, FormalsListNode, FnBodyNode // FormalDeclNode TypeNode, IdNode // // FormalsListNode linked list of FormalDeclNode // FnBodyNode DeclListNode, StmtListNode // StmtListNode linked list of StmtNode // ExpListNode linked list of ExpNode // // TypeNode: // IntNode -- none -- // BoolNode -- none -- // VoidNode -- none -- // // StmtNode: // ReadStmtNode ExpNode // WriteStmtNode ExpNode // AssignStmtNode ExpNode, ExpNode // IfStmtNode ExpNode, DeclListNode, StmtListNode // IfElseStmtNode ExpNode, DeclListNode, StmtListNode, // DeclListNode, StmtListNode // WhileStmtNode ExpNode, DeclListNode, StmtListNode // CallStmtNode CallExpNode // ReturnStmtNode ExpNode // // ExpNode: // IntLitNode -- none -- // StrLitNode -- none -- // TrueNode -- none -- // FalseNode -- none -- // IdNode -- none -- // ArrayExpNode IdNode, ExpNode // CallExpNode IdNode, ExpListNode // UnaryExpNode ExpNode // UnaryMinusNode // NotNode // BinaryExpNode ExpNode ExpNode // PlusNode // MinusNode // TimesNode // DivideNode // AndNode // OrNode // EqualsNode // NotEqualsNode // LessNode // GreaterNode // LessEqNode // GreaterEqNode // // Here are the different kinds of AST nodes again, organized according to // whether they are leaves, internal nodes with linked lists of kids, or // internal nodes with a fixed number of kids: // // (1) Leaf nodes: // IntNode, BoolNode, VoidNode, IntLitNode, StrLitNode, // TrueNode, FalseNode, IdNode // // (2) Internal nodes with (possibly empty) linked lists of children: // DeclListNode, FormalsListNode, StmtListNode, ExpListNode // // (3) Internal nodes with fixed numbers of kids: // ProgramNode, VarDeclNode, FnDeclNode, FormalDeclNode, // FnBodyNode, TypeNode, ReadStmtNode, WriteStmtNode // AssignStmtNode, IfStmtNode, IfElseStmtNode, WhileStmtNode, // CallStmtNode, ReturnStmtNode, ArrayExpNode, CallExpNode, // UnaryExpNode, BinaryExpNode, UnaryMinusNode, NotNode, // PlusNode, MinusNode, TimesNode, DivideNode, // AndNode, OrNode, EqualsNode, NotEqualsNode, // LessNode, GreaterNode, LessEqNode, GreaterEqNode // // ********************************************************************** // ********************************************************************** // ASTnode class (base class for all other kinds of nodes) // ********************************************************************** abstract class ASTnode { // every subclass must provide an unparse operation abstract public void unparse(PrintWriter p, int indent); // this method can be used by the unparse methods to do indenting protected void doIndent(PrintWriter p, int indent) { for (int k=0; k