📄 gen_maple.cc
字号:
{ // The time is the time complexity of the replacing statement case STATEMENT_LABEL : maple_label = STATEMENT; num_children = 1; break; // The time is the time complexity of the sum of the parts case SEQUENTIAL_COMPOSITION_LABEL : maple_label = SEQUENTIAL_STATEMENT; num_children = 2; break; // There is an error in the label assignment default : cerr << "Bad STATEMENTS_LABEL child label." << endl; break; } // successful completion return (0);}// ********************************************************************// *// * CompoundStmtLabel ()// * // * Description : This function sets the mapleNode labels and number// * of children for the mapleExpressionTree based// * on the values in the occurrence of a COMPOUND_STMT_LABEL// * syntaxNode label// *int CompoundStmtLabel ( int& maple_label, int& num_children, syntaxNode* tree ){ // This is basically a unit productions as far as the expression // tree is concerned maple_label = STATEMENTS; num_children = 1; // successful completion return (0);}// ********************************************************************// *// * StatementPartLabel ()// * // * Description : This function sets the mapleNode labels and number// * of children for the mapleExpressionTree based// * on the values in the occurrence of a STATEMENT_PART_LABEL// * syntaxNode label// *int StatementPartLabel ( int& maple_label, int& num_children, syntaxNode* tree ){ // This is basically a unit production as far as the expression // tree is concerned maple_label = COMPOUND_STMT; num_children = 1; // successful completion return (0);}// ********************************************************************// *// * IdentLabel ()// * // * Description : This function sets the mapleNode labels and number// * of children for the mapleExpressionTree based// * on the values in the occurrence of a IDENT_LABEL// * syntaxNode label// *int IdentLabel ( int& maple_label, int& num_children, syntaxNode* tree ){ maple_label = IDENT; num_children = 0; // successful completion return (0);}// ********************************************************************// *// * ProcOrFuncLabel ()// * // * Description : This function sets the mapleNode labels and number// * of children for the mapleExpressionTree based// * on the values in the occurrence of a IDENT_LABEL// * syntaxNode label// *int ProcOrFuncLabel ( int& maple_label, int& num_children, syntaxNode* tree ){ // Although the PROC_OR_FUNC_LABEL node in the syntax tree has two // children...we are only interested in the body of the node. maple_label = PROC_OR_FUNC; num_children = 1; push_node = FALSE; // successful completion return (0);}// ********************************************************************// *// * BlockLabel ()// * // * Description : This function sets the mapleNode labels and number// * of children for the mapleExpressionTree based// * on the values in the occurrence of a IDENT_LABEL// * syntaxNode label// *int BlockLabel ( int& maple_label, int& num_children, syntaxNode* tree ){ maple_label = BLOCK; // Since this is really a unit production we dod not need to worry // about it. num_children = -1; // successful completion return (0);}// ********************************************************************// *// * BodyLabel ()// * // * Description : This function sets the mapleNode labels and number// * of children for the mapleExpressionTree based// * on the values in the occurrence of a IDENT_LABEL// * syntaxNode label// *int BodyLabel ( int& maple_label, int& num_children, syntaxNode* tree ){ maple_label = BODY; // Since this is really a unit production we dod not need to worry // about it. num_children = -1; // successful completion return (0);}// ********************************************************************// *// * ActualParamsLabel ()// * // * Description : This function sets the mapleNode labels and number// * of children for the mapleExpressionTree based// * on the values in the occurrence of a IDENT_LABEL// * syntaxNode label// *int ActualParamsLabel ( int& maple_label, int& num_children, syntaxNode* tree ){ // This code was modified to through away parameters in procedure / function // calls. The "Actuals List" is throwm away prior to reaching this node in // the syntax tree so we basically ignore this one. Had to be done so we could // deal with function calls. maple_label = ACTUAL_PARAMS; switch ( tree -> GetChildLabel () ) { case EMPTY_LABEL : num_children = 0; break; case ACTUALS_LIST_LABEL : num_children = 1; break; default : cerr << "Bad child label for ACTUAL_PARAMS_LABEL.\n"; break; } // successful completion return (0);}// ********************************************************************// *// * ActualParamLabel ()// * // * Description : This function sets the mapleNode labels and number// * of children for the mapleExpressionTree based// * on the values in the occurrence of a IDENT_LABEL// * syntaxNode label// *int ActualParamLabel ( int& maple_label, int& num_children, syntaxNode* tree ){ maple_label = ACTUAL_PARAM; switch ( tree -> GetChildLabel () ) { case EXPRESSION_LABEL : num_children = 1; break; case COLON_THINGS_LABEL : num_children = 2; break; default : cerr << "Bad child label for ACTUAL_PARAM_LABEL.\n"; break; } // successful completion return (0);}// ********************************************************************// *// * ActualsListLabel ()// * // * Description : This function sets the mapleNode labels and number// * of children for the mapleExpressionTree based// * on the values in the occurrence of a IDENT_LABEL// * syntaxNode label// *int ActualsListLabel ( int& maple_label, int& num_children, syntaxNode* tree ){ maple_label = ACTUALS_LIST; switch ( tree -> GetChildLabel () ) { case SEQUENTIAL_PARAMS_LABEL : num_children = 2; break; case ACTUAL_PARAM_LABEL : num_children = 1; break; default : cerr << "Bad child label for ACTUALS_LIST_LABEL.\n"; break; } // successful completion return (0);}// ********************************************************************// *// * ColonThingsLabel ()// * // * Description : This function sets the mapleNode labels and number// * of children for the mapleExpressionTree based// * on the values in the occurrence of a IDENT_LABEL// * syntaxNode label// *int ColonThingsLabel ( int& maple_label, int& num_children, syntaxNode* tree ){ maple_label = COLON_THINGS; switch ( tree -> GetChildLabel () ) { case EXPRESSION_LABEL : num_children = 1; break; case SEQUENTIAL_COLON_LABEL : num_children = 2; break; default : cerr << "Bad child label for COLON_THINGS_LABEL.\n"; break; } // successful completion return (0);}// *************************************************************// *// * SetFunctionCall ();// * Global access : sTable.............symbolTable// * procStack..........stack<char*>// *int SetFunctionCall ( mapleNode *mapleChild, syntaxNode *sourceTree ){ // local variables symbolNode *symbol_entry; char *proc_name = 0; string mapleString, exprString; syntaxNode *treePtr = sourceTree; boolean done = FALSE; syntaxNode **children; char indentStr[25] = ""; int i = 0; // Here we need to insert a pointer to the associated // maple function name. if ( NOT procStack.IsEmpty () ) proc_name = procStack.Pop (); else { cerr << "Procedure Name stack is empty!!! Cannot Pop.\n"; return ( -1 ); } symbol_entry = sTable.Search ( proc_name ); if ( symbol_entry == 0 ) { cerr << proc_name << " does not exist in global symbol Table." << endl; return ( -1 ); } else { // If the procedure / function has not been declared, then // this will probably cause a segmentation fault when we // try to print out the maple expression. mapleString = symbol_entry->GetMapleName (); } // The sourceTree should be the statement syntax tree for the // procedure / function call. We will need to traverse the // tree to the leftmost ocurrence of the ACTUAL_PARAM_LABEL and // then copy the ASCII version of the expression into a string // to be stored for later. // Root node is the STATEMENT_LABEL. This should go to PROCEDURE_CALL. treePtr = *(treePtr->GetChildren()); // Root node should be PROCEDURE_CALL. This should go to ACTUAL_PARAMS. treePtr = *(treePtr->GetChildren()+1); // Traverse the tree to get the first parameter. while ( NOT done ) { children = treePtr -> GetChildren (); if ( children != 0 ) { // The stopping condition should be that we are at the leftmost // child from the root of the expression which is a ACTUALS_LIST_LABEL if ( (*(children))->GetLabel() != ACTUALS_LIST_LABEL ) done = TRUE; else treePtr = *(children); } else { cerr << "Leaf node found before loop exit." << endl; exit ( -1 ); } } // At this point we should have the syntax tree for the first // parameter. Now we need to place the expression tree into // a character string and replace the occurence of n in expr_string // with the character version of the expression syntax tree. // Finally set the mapleChild's expression string to be the contents // of the array expr_string. treePtr -> expression2string ( exprString ); // Now we must split the mapleString and replace n by exprString. // If this looks confusing you should look at stringcl.h and // stringcl.cc for the / and % operators on string objects. mapleString = ( mapleString / '(' ) + '(' + exprString + ')'; // Finally set the mapleChild's expression string to be the contents // of the array expr_string. mapleChild -> SetExprString ( (char*)mapleString ); // Cleanup pointers delete proc_name; // successful completion return ( 0 );}// *************************************************************// *// * SetFunctionDeclaration ();// * Global access : sTable.............symbolTable// * procStack..........stack<char*>// *int SetFunctionDeclaration ( mapleNode *mapleChild ){ // local variables symbolNode* symbol_entry; char *proc_name = 0; if ( NOT procStack.IsEmpty () ) proc_name = procStack.Pop (); else { cerr << "Procedure Name stack is empty!!! Cannot Pop.\n"; return ( -1 ); } symbol_entry = sTable.Search ( proc_name ); delete proc_name; if ( symbol_entry == 0 ) { cerr << "FATAL : " << proc_name << "does not exist in global symbol Table.\n"; return ( -1 ); } else { symbol_entry -> SetMapleExpression ( mapleChild ); } // successful completion return ( 0 );}//******** end of gen_maple.cc ***************************************//********************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -