📄 syntax.cc
字号:
//// *********************************************************************// *// * Author : Gerald Carter// * cartegw@eng.auburn.edu// * Filename : syntax.cc// * Date Created : 910110// *// * Description : This is the C++ class definition for a syntaxNode// * class. This class will be used to build a syntax tree// * in Bison.// *// * --------------------// * Modifications// * --------------------// * 960208 cartegw@humsci.auburn.edu Gerald Carter// * rewrote PrintTree routine as a pretty printer for the // * syntax tree.// *// * 961208 cartegw@humsci.auburn.edu Gerald Carter// * Added static array to class to hold mapping of macros// * to character strings for node labels.// * // * 970219 cartegw@humsci.auburn.edu Gerald Carter// * Added the method expression2string to convert a syntax// * tree to a character representation of the tree held// * in a string object ( see stringcl.cc ).// *// *********************************************************************//// HEADER FILES#include <iostream.h>#include <stdlib.h>#include <string.h>#include "syntax.h"#include "labels.h"#include "pas.tab.h"// #####################################################################// ## Mappings of node labels to character strings// ###define NUM_LABELS 180labelEntry syntaxNode::labels[NUM_LABELS] = { { PROGRAM_LABEL, "Program" }, { BLOCK_LABEL, "Block" }, { STATEMENT_PART_LABEL, "Statement Part" }, { COMPOUND_STMT_LABEL, "Compound Statement" }, { STATEMENTS_LABEL, "Statements" }, { SEQUENTIAL_COMPOSITION_LABEL, "Sequential Comp" }, { STATEMENT_LABEL, "Statement" }, { ASSIGNMENT_LABEL, "Assignment" }, { PROCEDURE_CALL_LABEL, "Procedure Call" }, { IF_LABEL, "If" }, { IF_ELSE_LABEL, "If...Else" }, { CASE_LABEL, "Case" }, { WHILE_LABEL, "While" }, { REPEAT_LABEL, "Repeat" }, { EXPRESSION_LABEL, "Expression" }, { CASE_LIST_LABEL, "Case List" }, { CASE_LIST_ELEM_LABEL, "Case List Elem" }, { SEQUENTIAL_CASE_OPTION_LABEL, "Sequential Case Option" }, { CASE_OPTION_LABEL, "Case Option" }, { SIMPLE_EXPR_LABEL, "Simple Expr" }, { RELATIONAL_OP_LABEL, "Relational Op" }, { FOR_LABEL, "For" }, { TERM_LABEL, "Term" }, { UNARY_PLUS_LABEL, "Unary +" }, { UNARY_MINUS_LABEL, "Unary -" }, { ADD_OP_LABEL, "Add Op" }, { PLUS_LABEL, "+" }, { MINUS_LABEL, "-" }, { NOT_LABEL, "NOT" }, { FACTOR_LABEL, "Factor" }, { MULTI_OP_LABEL, "Multiply Op" }, { MULTIPLY_LABEL, "*" }, { DIVIDE_LABEL, "/" }, { DIV_LABEL, "DIV" }, { MOD_LABEL, "MOD" }, { AND_LABEL, "AND" }, { VARIABLE_LABEL, "Variable" }, { UNSIGNED_LIT_LABEL, "Unsigned Lit" }, { SET_RULE_LABEL, "Set Rule" }, { NOT_FACTOR_LABEL, "NOT Factor" }, { UNSIGNED_NUM_LABEL, "Unsigned Number" }, { SET_LABEL, "Set" }, { MEMBER_LIST_LABEL, "Member List" }, { MEMBERS_LABEL, "Members" }, { MEMBER_LABEL, "Member" }, { EXPRESSION_DOTDOT_LABEL, "Expr.." }, { PROCEDURE_CALL_PARTS_LABEL, "Procedure Call Parts" }, { ACTUALS_LIST_LABEL, "Actuals List" }, { IDENT_LABEL, "Ident" }, { ARRAY_LABEL, "Array" }, { RECORD_LABEL, "Record" }, { POINTER_LABEL, "Pointer" }, { EXPRESSIONS_LABEL, "Expressions" }, { SEQUENTIAL_EXPR_LABEL, "Sequential Expr" }, { ACTUAL_PARAM_LABEL, "Actual Param" }, { COLON_THINGS_LABEL, ": Things" }, { SEQUENTIAL_COLON_LABEL, "Sequential :" }, { SEQUENTIAL_PARAMS_LABEL, "Sequential Params" }, { SEQUENTIAL_MEMBERS_LABEL, "Sequential Members" }, { NEWIDENT_LABEL, "Newident" }, { ACTUAL_PARAMS_LABEL, "Actual Params" }, { DIRECTION_LABEL, "Direction" }, { GOTO_LABEL, "Goto" }, { EMPTY_LABEL, "Empty" }, { EMPTY_STATEMENT, "Empty Statement" }, { EMPTY_CASE_LIST_ELEM, "Empty Case List" }, { EMPTY_MEMBER_LIST, "Empty Member List" }, { EMPTY_PARAMS, "Empty Params" }, { BLOCK_PART_LABEL, "Block Part" }, { OPT_DECLARATIONS_LABEL, "Opt Declarations" }, { EMPTY_OPT_DECLARATIONS, "Empty Opt Declarations" }, { DECLARATIONS_LABEL, "Declarations" }, { DECLARATION_LABEL, "Declaration" }, { DECLS_DECL_LABEL, "Decls, Decl" }, { LABEL_DCL_PART_LABEL, "Label Decl Part" }, { CONST_DCL_PART_LABEL, "Const Decl Part" }, { TYPE_DCL_PART_LABEL, "Type Decl Part" }, { VAR_DCL_PART_LABEL, "Var Decl Part" }, { PROC_DCL_PART_LABEL, "Proc Decl Part" }, { LABELS_LABEL_LABEL, "Labels, Label" }, { LABELS_LABEL, "Labels" }, { LABEL_LABEL, "Label" }, { CONST_DEFS_LABEL, "Const Defs" }, { CONST_DEF_LABEL, "Const Def" }, { CONSTS_CONST_LABEL, "Consts, Const" }, { CONST_DEF_EQUAL_LABEL, "Const Def =" }, { CONSTANT_LABEL, "Constant" }, { TYPE_DEFS_LABEL, "Type Defs" }, { TYPES_TYPES_LABEL, "Types, Types" }, { TYPE_DEF_LABEL, "Type Def" }, { TYPE_DEF_EQUAL_LABEL, "Type Def =" }, { TYPE_LABEL, "Type" }, { SIMPLE_TYPE_LABEL, "Simple Type" }, { PACKED_STRUCT_TYPE_LABEL, "Packed Struct Type" }, { STRUCT_TYPE_LABEL, "Struct Type" }, { ENUMERATED_LIST_LABEL, "Enum List" }, { CONSTANT_DOTDOT_CONSTANT_LABEL,"Const..Const" }, { ARRAY_OF_TYPE_LABEL, "Array of Type" }, { FIELD_LIST_LABEL, "Field List" }, { SFILE_OF_TYPE_LABEL, "Sfile of Type" }, { INDEX_T_LIST_LABEL, "Index T List" }, { SIMPLES_SIMPLE_LABEL, "Simples, Simple" }, { FIXED_PART_LABEL, "Fixed Part" }, { FIXED_VARIANT_LABEL, "Fixed Variant" }, { VARIANT_PART_LABEL, "Variant Part" }, { FIXED_RECORD_LABEL, "Fixed Record" }, { RECORD_SECTION_LABEL, "Record Section" }, { NEW_IDENT_COLON_TYPE_LABEL, "Newident : Type" }, { EMPTY_RECORD_SECTION, "Empty Record" }, { CASE_TAG_OF_VARIANT_LABEL, "Case Tag of Variant" }, { TAG_FIELD_LABEL, "Tag Field" }, { NEWIDENT_COLON_IDENT_LABEL, "Newident : Ident" }, { VARIANTS_LABEL, "Variants" }, { VARIANTS_VARIANT_LABEL, "Variants, Variant" }, { VARIANT_LABEL, "Variant" }, { CASE_FIELD_LIST_LABEL, "Case Field List" }, { EMPTY_VARIANT, "Empty Variant" }, { VAR_DCLS_LABEL, "Var Decls" }, { VARS_VAR_LABEL, "Vars, Var" }, { VAR_DCL_LABEL, "Var Decl" }, { NEWIDENT_LIST_COLON_TYPE_LABEL,"Newident List : Type" }, { NEW_ID_LIST_LABEL, "New Id List" }, { NEW_IDS_ID_LABEL, "New Ids, Id" }, { PROC_OR_FUNC_LABEL, "Proc or Func" }, { PROC_HEADING_BODY_LABEL, "Proc Heading Body" }, { FUNC_HEADING_BODY_LABEL, "Func Heading Body" }, { PROC_ID_PARAMS_LABEL, "Proc ID Params" }, { FUNC_HEADING_LABEL, "Func Heading" }, { FUNC_FORM_LABEL, "Func Form" }, { EMPTY_FUNC_FORM, "Empty Func Form" }, { FUNC_PARAMS_ID_LABEL, "Func Params Id" }, { BODY_LABEL, "Body" }, { FORMAL_PARAMS_LABEL, "Formal Params" }, { EMPTY_FORMAL_PARAMS, "Empty Formal Params" }, { FORMAL_P_SECTS_LABEL, "Formal P Sects" }, { PSECTS_PSECT_LABEL, "Psects, Psect" }, { PSECT_LABEL, "Psect" }, { PARAM_GROUP_LABEL, "Param Group" }, { VAR_PARAM_GROUP_LABEL, "Var Param Group" }, { NEWID_LIST_COLON_TYPE_LABEL, "NewId List : Type" }, { PARAM_TYPE_LABEL, "Param Type" }, { ARRAY_OF_PARAM_TYPE, "Array of Param Type" }, { PACK_ARRAY_INDEX_OF_ID_LABEL, "Pack Array Index of Id" }, { INDEX_SPECS_LABEL, "Index Specs" }, { INDEX_SPECS_SPEC_LABEL, "Index Specs, Spec" }, { INDEX_SPEC_LABEL, "Index Spec" }, { NEWID_DOTDOT_NEWID_COLON_ID_LABEL,"NewId..NewId : Id" }, { TYPES_TYPE_LABEL, "Types, Type" }, { PROC_HEADING_LABEL, "Proc Heading" }, { NEWIDENT_LIST_LABEL, "Newident List" }, { FUNC_ID_FORM_LABEL, "Func Id Form" }, { FUNCTION_CALL_LABEL, "Function Call" }, { FUNCTION_CALL_PARTS_LABEL, "Func Call Parts" }, { FUNCTION_CALL, "Function Call" }, { CONSTANT_VALUE, "Constant" }, { EXPRESSION, "Expression" }, { FACTOR, "Factor" }, { TERM_PLUS_FACTOR, "Term + Factor" }, { TERM, "Term" }, { SIMPLE_EXPR_PLUS_TERM, "SimpleExpr + Term" }, { SIMPLE_EXPR, "SimpleExpr" }, { SIMPLE_EXPR_PLUS_SIMPLE_EXPR, "SimpleExpr + SimpleExpr" }, { UNIT_STATEMENT, "Unit Statement" }, { STATEMENT, "Statement" }, { SEQUENTIAL_STATEMENT, "Sequential Statement" }, { STATEMENTS, "Statements" }, { COMPOUND_STMT, "Comp Statement" }, { PROCEDURE_CALL, "Procedure Call" }, { FOR_STATEMENT, "For" }, { VARIABLE_, "Variable" }, { UNSIGNED_LIT_, "Unsigned Lit" }, { BODY, "Body" }, { BLOCK, "Block" }, { STATEMENT_PART, "Statement Part" }, { PROC_OR_FUNC, "Func / Proc" }, { IDENT, "Ident" }, { ACTUAL_PARAMS, "Actual Params" }, { ACTUALS_LIST, "Actuals List" }, { ACTUAL_PARAM, "Actual Param" }, { COLON_THINGS, "Colon Things" } };//*********************************************************************syntaxNode** syntaxNode::AdoptChildren (int number, int kid_label, syntaxNode* kids[]){ // local variables int i = 0; // bounds checking for the number given if (number < 0) { cerr << "Invalid number of children. Children given up.\n"; return (0); } else if (number == 0) { num_children = 0; child_label = kid_label; return (0); } // Label the kids child_label = kid_label; // Adopt the kids if (!(family.children = new (syntaxNode *[number]))) { cerr << "Insufficent memory for children.\n"; exit (1); } for (i = 0; i < number; ++i) { family.children[i] = kids[i]; if (family.children[i] == 0x0) { cerr << i << " = " << family.children[i] << "..." << label << "<--->"; cerr << child_label << "\n"; } } // keep track of how many we have num_children = number; // Success return (kids);} // end of syntaxNode::AdoptChildren()//*********************************************************************void syntaxNode::PrintTreeLabels (ostream& S){ // local variables char label_string[100]; if (num_children == 0) S << "Leaf node : " << label2string ( child_label, label_string ) << "\n"; else { for (int i = 0; i < num_children; ++i) family.children[i]->PrintTreeLabels (S); S << "Interior node : " << label2string (label, label_string ) << "<--->" << label2string ( child_label, label_string ) << "\n"; } return;}//*********************************************************************void syntaxNode::PrintTree (ostream& S, char *indent_string, int &i){ // switch statement to examine the nodes current label switch (label) { case PROGRAM_LABEL : i = 0; S << "PROGRAM "; family.children[0]->PrintTree (S, indent_string, i); S << ";\n"; family.children[1]->PrintTree (S, indent_string, i); S << ".\n"; // PrintChildren (S, indent_string, i); break; case COMPOUND_STMT_LABEL : S << indent_string << indent_string << indent_string; S << "BEGIN\n"; indent_string[i++] = ' '; indent_string[i] = '\0'; PrintChildren (S, indent_string, i); indent_string[--i] = '\0'; S << indent_string << indent_string << indent_string; S << "END\n"; break; case STATEMENT_LABEL : switch (child_label) { case STATEMENT_LABEL : case COMPOUND_STMT_LABEL : PrintChildren (S, indent_string, i); break; case ASSIGNMENT_LABEL : S << indent_string << indent_string << indent_string; family.children[0]->PrintTree (S, indent_string, i); S << ";\n"; break; case PROCEDURE_CALL_LABEL : S << indent_string << indent_string << indent_string; family.children[0]->PrintTree (S, indent_string, i); S << ";\n"; break; case GOTO_LABEL : S << indent_string << indent_string << indent_string; S << "goto ###;\n"; break; case IF_LABEL : S << indent_string << indent_string << indent_string; indent_string[i++] = ' '; indent_string[i] = '\0'; S << "if "; family.children[0]->PrintTree (S, indent_string, i); S << "then\n"; family.children[1]->PrintTree (S, indent_string, i); indent_string[--i] = '\0'; break; case IF_ELSE_LABEL : S << indent_string << indent_string << indent_string; indent_string[i++] = ' '; indent_string[i] = '\0'; S << "if "; family.children[0]->PrintTree (S, indent_string, i); S << "then\n"; family.children[1]->PrintTree (S, indent_string, i); indent_string[--i] = '\0'; S << indent_string << indent_string << indent_string; indent_string[i++] = ' '; indent_string[i] = '\0'; S << "else\n"; family.children[2]->PrintTree (S, indent_string, i); indent_string[--i] = '\0'; break; case FOR_LABEL : S << indent_string << indent_string << indent_string; indent_string[i++] = ' '; indent_string[i] = '\0'; S << "for "; family.children[0]->PrintTree (S, indent_string, i); S << " := "; family.children[1]->PrintTree (S, indent_string, i); S << " "; family.children[2]->PrintTree (S, indent_string, i); S << " "; family.children[3]->PrintTree (S, indent_string, i); S << "do\n"; S << indent_string << indent_string << indent_string; family.children[4]->PrintTree (S, indent_string, i); S << "\n"; indent_string[--i] = '\0'; break; case WHILE_LABEL : S << indent_string << indent_string << indent_string; S << "while (...) do\n"; indent_string[i++] = ' '; indent_string[i] = '\0'; family.children[1]->PrintTree (S, indent_string, i); indent_string[--i] = '\0'; break; default : PrintChildren (S, indent_string, i); break; } break; case ASSIGNMENT_LABEL : family.children[0]->PrintTree (S, indent_string, i); S << " := "; family.children[1]->PrintTree (S, indent_string, i); break; case PROCEDURE_CALL_LABEL : family.children[0]->PrintTree (S, indent_string, i); S << " ("; family.children[1]->PrintTree (S, indent_string, i); S << ")"; break; case DIRECTION_LABEL : switch (child_label) { case TO : S << "to"; break; case DOWNTO : S << "downto"; break; } break; case VARIABLE_LABEL : switch (child_label) { case IDENT_LABEL : family.children[0]->PrintTree (S, indent_string, i); break; case ARRAY_LABEL : family.children[0]->PrintTree (S, indent_string, i); S << "["; family.children[1]->PrintTree (S, indent_string, i); S << "]"; break; case RECORD_LABEL : family.children[0]->PrintTree (S, indent_string, i); S << "."; family.children[1]->PrintTree (S, indent_string, i); break; case POINTER_LABEL : family.children[0]->PrintTree (S, indent_string, i); S << "^"; break; } break; case IDENT_LABEL : case NEWIDENT_LABEL : case UNSIGNED_NUM_LABEL : S << GetLexeme()->GetSymbolName(); break; case BLOCK_LABEL : case STATEMENT_PART_LABEL :
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -