⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 syntax.cc

📁 this is a lp0 compilator new
💻 CC
📖 第 1 页 / 共 2 页
字号:
      case STATEMENTS_LABEL :      case SEQUENTIAL_COMPOSITION_LABEL :	 PrintChildren (S, indent_string, i);	 break;      case EXPRESSION_LABEL :         PrintChildren ( S, indent_string, i );         break;      case RELATIONAL_OP_LABEL :         switch ( child_label )         {            case '=' :            case '<' :            case '>' :               S << ( char )child_label;               break;            case LE :               S << "<= ";               break;            case GE :               S << " >= ";               break;            case NE :               S << " <> ";               break;            case IN :               S << " IN ";               break;         }          break;      case SIMPLE_EXPR_LABEL :         switch ( child_label )         {            case TERM_LABEL :               PrintChildren (S, indent_string, i);               break;            case UNARY_PLUS_LABEL :               S << "+";               PrintChildren (S, indent_string, i);               break;                          case UNARY_MINUS_LABEL :               S << "-";               PrintChildren (S, indent_string, i);               break;            case ADD_OP_LABEL :               PrintChildren (S, indent_string, i);               break;         }         break;      case ADD_OP_LABEL :         switch ( child_label )         {            case '+' :            case '-' :               S << ( char )child_label;               break;            case OR_TOK :               S << " OR ";               break;         }         break;      case TERM_LABEL :         switch ( child_label )         {            case FACTOR_LABEL :               PrintChildren (S, indent_string, i);               break;            case MULTI_OP_LABEL :               PrintChildren (S, indent_string, i);               break;           }         break;      case MULTI_OP_LABEL :         switch ( child_label )         {            case '*' :            case '/' :               S << ( char )child_label;               break;            case DIV :               S << " DIV ";               break;            case MOD :               S << " MOD ";               break;            case AND_TOK :               S << " AND ";               break;         }         break;      case FACTOR_LABEL :         switch ( child_label )         {            case VARIABLE_LABEL :            case UNSIGNED_LIT_LABEL :            case SET_RULE_LABEL :            case FUNCTION_CALL_LABEL :               PrintChildren (S, indent_string, i);               break;            case EXPRESSION_LABEL :               S << "( ";               PrintChildren (S, indent_string, i);               S << " )";               break;            case NOT_FACTOR_LABEL :               S << "NOT ";               PrintChildren (S, indent_string, i);               break;         }         break;      case EMPTY_LABEL :         /* do nothing */         break;      default : /* do nothing */         // S << label << "--->" << child_label;         // S << "....." << num_children;         PrintChildren (S, indent_string, i);	 break;   }       // success   return;}      // end of syntaxNode::PrintTree()//*********************************************************************void syntaxNode::PrintChildren (ostream& S, char* indent_string, int& str_size){   // local variables   int                  i = 0;   // do we have any children?   if (num_children != 0)   {      for (i = 0; i < num_children; ++i)         family.children[i]->PrintTree (S, indent_string, str_size);   }   return;}//*********************************************************************string& syntaxNode::expression2string ( string& exprString ){   // local variables   // None for now.   // Start traversing the tree   switch ( label )   {      case EXPRESSION_LABEL :         ExpressChildren ( exprString );         break;       case RELATIONAL_OP_LABEL :         switch ( child_label )         {            case '=' :            case '<' :            case '>' :               exprString = exprString + (char)child_label;               break;            case LE :               exprString = exprString + " <= " ;               break;            case GE :               exprString = exprString + " >= " ;               break;            case NE :               exprString = exprString + " <> " ;               break;            case IN :               exprString = exprString + " IN " ;               break;         }         break;      case SIMPLE_EXPR_LABEL :         switch ( child_label )         {            case TERM_LABEL :               ExpressChildren ( exprString );               break;               case UNARY_PLUS_LABEL :               exprString = exprString + '+';               ExpressChildren ( exprString );               break;               case UNARY_MINUS_LABEL :               exprString = exprString + '-';               ExpressChildren ( exprString );               break;               case ADD_OP_LABEL :               ExpressChildren ( exprString );               break;         }         break;      case ADD_OP_LABEL :         switch ( child_label )         {            case '+' :            case '-' :               exprString = exprString + (char)child_label;               break;            case OR_TOK :               exprString = exprString + " OR " ;               break;         }         break;      case TERM_LABEL :         switch ( child_label )         {            case FACTOR_LABEL :               ExpressChildren ( exprString );               break;            case MULTI_OP_LABEL :               ExpressChildren ( exprString );               break;         }         break;      case MULTI_OP_LABEL :         switch ( child_label )         {            case '*' :            case '/' :               exprString = exprString + (char)child_label;               break;            case DIV :               exprString = exprString + " DIV " ;               break;            case MOD :               exprString = exprString + " MOD " ;               break;            case AND_TOK :               exprString = exprString + " AND " ;               break;         }         break;      case FACTOR_LABEL :         switch ( child_label )         {            case VARIABLE_LABEL :            case UNSIGNED_LIT_LABEL :            case SET_RULE_LABEL :            case FUNCTION_CALL_LABEL :               ExpressChildren ( exprString );               break;            case EXPRESSION_LABEL :               exprString = exprString + "( " ;               ExpressChildren ( exprString );               exprString = exprString + " )" ;               break;            case NOT_FACTOR_LABEL :               exprString = exprString + "NOT " ;               ExpressChildren ( exprString );               break;         }         break;      case IDENT_LABEL :      case NEWIDENT_LABEL :      case UNSIGNED_NUM_LABEL :          exprString = exprString +  GetLexeme()->GetSymbolName();          break;      case EMPTY_LABEL :         /* do nothing */         break;      default :          ExpressChildren ( exprString );         break;   }   // successful completion   return ( exprString );}//*********************************************************************void syntaxNode::ExpressChildren ( string& expr_string ){   // local variables   int                  i = 0;   // do we have any children?   if (num_children != 0)   {      for (i = 0; i < num_children; ++i)         family.children[i]->expression2string ( expr_string );   }   return;}//*********************************************************************syntaxNode& syntaxNode::operator= (syntaxNode& copy_node){   // local variables   int                 i = 0;   // copy the parameter copy_node   label = copy_node.label;   child_label = copy_node.child_label;   num_children = copy_node.num_children;   // we do not need to copy the family if there are no children   if (num_children > 0)   {      if (!(family.children = new (syntaxNode *[num_children])))      {         cerr << "Insufficent memory for children.\n";         exit (1);      }      for (i = 0; i < num_children; ++i)         family.children[i] = copy_node.family.children[i];   }};      // end of syntaxNode::operator=()//*********************************************************************char* syntaxNode::label2string ( int x, char x_string[] ){   // This section handles token labels before searching the array   if ( x < 900 )       switch ( x )      {         case IDENTIFIER :            strcpy ( x_string, "IDENTIFIER" );            break;         case TO :            strcpy ( x_string, "TO" );            break;         case DOWNTO :            strcpy ( x_string, "DOWNTO" );            break;         case UNSIGNED_INT :            strcpy ( x_string, "UNSIGNED_INT" );            break;         default :            strcpy ( x_string, "DEFAULT" );            cerr << "Unidentified X = " << x << endl;            break;      }   else {		// Perform a binary search on the label_id      int min;      int max;      int guess, compare;      min = 0;      max = NUM_LABELS - 1;      guess = (min + max) / 2;      for (guess=(min+max)/2; min<=max; guess=(min+max)/2)      {         if ( x > labels[guess].label_id )   	    min = guess + 1;         else if ( x < labels[guess].label_id ) 	    max = guess - 1;         else {            strcpy ( x_string, labels[guess].name );	    break;         }      }   }      // return the pointer to x_string   return ( x_string );}//*********************************************************************void syntaxNode::OutputGML ( ostream& outS, int& id ){   // local variables   int			parent_id = id;   int			child_id;   int			i = 0;   char			label_string[100];   // Print the nodes beginning with the root.  Lets do the root,   // then children...left to right   if ( this != 0 ) {      // Print the root node      outS << "\tnode [\n"           << "\t\tid " << id++ << "\n" << "\t\tlabel \""           << label2string ( label, label_string )           << "\"\n\t]\n";      // Print the children.  This part will skipped if there aren't any children      for ( i = 0 ; i < num_children; ++i ) {         child_id = id;         family.children[i] -> OutputGML ( outS, id );         ++id;         outS << "\tedge [\n"	      << "\t\tsource " << parent_id << "\n" 	      << "\t\ttarget " << child_id << "\n"	      << "\t]\n";      }   }}// ####################################################################// ## The PrintGML() method prints the GML representation of the syntax // ## tree.  The GML language specification is outlined in "".// ## The output stream can be read into the VGJ tool developed by// ## a research team led by Dr. Carolyn McCreary at the Computer Science // ## Department, Auburn University. // ##// ## 	http://www.eng.auburn.edu/department/research/VGJ/graph_drawing.html// ##void syntaxNode::PrintGML ( ostream& outS ){   // local variables   static int		id = 1;   int			parent_id,			child_id;   int			i = 0;   syntaxNode**		children;   char			label_string[100];   // Print the <graph> keyword   outS << "graph [\n\n";   // Print the vendor information.  Not currently supported by VGJ.   // outS << "\tvendor \"Compx Timing Tool\"\n\n";   // The tree should be directed   outS << "\tdirected 1\n\n";   // Print the nodes beginning with the root.  Lets do the root,   // then children...left to right   if ( this != 0 ) {      // parent_id = id;      OutputGML ( outS, id );   }   // Print the ending brace to the graph syntax   outS << "]" << endl;}//********** end of syntax.cc *****************************************//*********************************************************************

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -