Changeset c9383ee
- Timestamp:
- Jun 29, 2017, 9:39:17 AM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 288eede, 8c680e9, ae47a23
- Parents:
- c89503c
- Location:
- tools/prettyprinter
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/prettyprinter/filter.cc
rc89503c rc9383ee 10 10 // Created On : Tue Apr 9 22:33:44 2002 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Jun 28 22:56:58201713 // Update Count : 7 212 // Last Modified On : Thu Jun 29 08:56:46 2017 13 // Update Count : 73 14 14 // 15 15 … … 22 22 23 23 24 void (* filter)( Token *tree ) = 0;25 26 27 void freeTree( Token * tree ) {// postfix tree traversal24 void (* filter)( Token * tree ) = 0; 25 26 27 void freeTree( Token * tree ) { // postfix tree traversal 28 28 if ( tree == NULL ) return; 29 29 if ( tree->down != NULL ) freeTree( tree->down ); … … 34 34 35 35 36 void Identity( Token * tree ) {// prefix tree traversal36 void Identity( Token * tree ) { // prefix tree traversal 37 37 if ( tree == NULL ) return; 38 38 // print only the terminals … … 43 43 44 44 45 static void Parse_Tree1( Token * tree, int indent ) { // prefix tree traversal45 static void Parse_Tree1( Token * tree, int indent ) { // prefix tree traversal 46 46 cout << string( indent, ' ' ); 47 if ( tree->isTerminal() ) { // terminals47 if ( tree->isTerminal() ) { // terminals 48 48 cout << "\"" << tree->getText() << "\""; 49 } else { // non-terminals49 } else { // non-terminals 50 50 cout << tree->getText(); 51 51 } // if … … 57 57 } // Parse_Tree1 58 58 59 void Parse_Tree( Token * tree ) {59 void Parse_Tree( Token * tree ) { 60 60 if ( tree == NULL ) return; 61 61 Parse_Tree1( tree, 0 ); … … 63 63 64 64 65 void Nocode( Token * tree ) {// prefix tree traversal65 void Nocode( Token * tree ) { // prefix tree traversal 66 66 static bool declprt = true; 67 67 if ( tree == NULL ) return; 68 68 69 if ( tree->isTerminal() ) { // terminals69 if ( tree->isTerminal() ) { // terminals 70 70 cout << tree->getWS() << tree->getText(); 71 } else { // non-terminals71 } else { // non-terminals 72 72 switch ( tree->getKind() ) { 73 73 case _RHS: { 74 int first = 0; // first RHS after ':' or '|' treated specially74 int first = 0; // first RHS after ':' or '|' treated specially 75 75 int push = 0, pop = 0; 76 for ( Token * i = tree->down; i != 0; i = i->left ) {76 for ( Token * i = tree->down; i != 0; i = i->left ) { 77 77 switch ( i->getKind() ) { 78 78 case _ACTION: … … 82 82 break; 83 83 case _PREC: 84 Nocode( i->down ); // print verbatim85 break; 86 case '|': // start of alternative and special case84 Nocode( i->down ); // print verbatim 85 break; 86 case '|': // start of alternative and special case 87 87 first = 0; 88 case ';': // print with whitespace88 case ';': // print with whitespace 89 89 cout << string( (push * 4 + pop * 3) / 7, '\t' ); 90 90 push = pop = 0; … … 95 95 if ( i->getText() == "push" ) { 96 96 push += 1; 97 if ( first == 0 ) { // first RHS after ':' or '|' ?97 if ( first == 0 ) { // first RHS after ':' or '|' ? 98 98 cout << i->getComment(); // ignore rhs but print its comment, if any 99 99 } // if … … 101 101 } else if ( i->getText() == "pop" ) { 102 102 pop += 1; 103 if ( first == 0 ) { // first RHS after ':' or '|' ?103 if ( first == 0 ) { // first RHS after ':' or '|' ? 104 104 cout << i->getComment(); // ignore rhs but print its comment, if any 105 105 } // if … … 107 107 } // if 108 108 } // if 109 // If there is a comment or this is the first RHS after 110 // ':' or '|', then include the whitespace before the 111 // token. Otherwise, fold the token onto the same line 112 // separated with a blank. 109 // If there is a comment or this is the first RHS after ':' or '|', then include the whitespace 110 // before the token. Otherwise, fold the token onto the same line separated with a blank. 113 111 string t1( i->getText() ); 114 112 if ( i->isComment() || first == 0 ) { … … 128 126 break; 129 127 } 130 case _LITERALBLOCK: // ignore code but print its comment, if any128 case _LITERALBLOCK: // ignore code but print its comment, if any 131 129 cout << tree->down->getComment(); 132 130 break; 133 case _DECLARATION: { // ignore certain declarations134 int kind = tree->down->getKind(); // get kind of declaration131 case _DECLARATION: { // ignore certain declarations 132 int kind = tree->down->getKind(); // get kind of declaration 135 133 if ( kind != UNION && kind != TYPE ) { 136 134 declprt = true; 137 Nocode( tree->down ); // print verbatim138 } else if ( declprt ) { // ignore declaration but print its comment, if any135 Nocode( tree->down ); // print verbatim 136 } else if ( declprt ) { // ignore declaration but print its comment, if any 139 137 declprt = false; 140 138 cout << tree->down->getComment(); … … 142 140 break; 143 141 } 144 case _USERSECTION_OPT: // ignore but add newline at the end142 case _USERSECTION_OPT: // ignore but add newline at the end 145 143 cout << endl; 146 144 break; … … 153 151 154 152 155 void LaTeX( Token * tree ) {// prefix tree traversal156 if ( tree == NULL ) return; 157 158 if ( tree->isTerminal() ) { // terminals153 void LaTeX( Token * tree ) { // prefix tree traversal 154 if ( tree == NULL ) return; 155 156 if ( tree->isTerminal() ) { // terminals 159 157 cout << tree->getWS() << tree->getText(); 160 158 if ( tree->getKind() == IDENTIFIER ) { … … 162 160 cout << "\\(\\index{" << id << "@\\protect\\LGbegin\\protect\\lgrinde\\)" << id << "\\(\\protect\\endlgrinde\\protect\\LGend{}}\\)"; 163 161 } // if 164 } else { // non-terminals162 } else { // non-terminals 165 163 switch ( tree->getKind() ) { 166 164 case _RHS: { 167 int first = 0; // first RHS after ':' or '|' treated specially165 int first = 0; // first RHS after ':' or '|' treated specially 168 166 int push = 0, pop = 0; 169 for ( Token * i = tree->down; i != 0; i = i->left ) {167 for ( Token * i = tree->down; i != 0; i = i->left ) { 170 168 switch ( i->getKind() ) { 171 169 case _ACTION: … … 173 171 break; 174 172 case _PREC: 175 LaTeX( i->down ); // print verbatim176 break; 177 case '|': // start of alternative and special case173 LaTeX( i->down ); // print verbatim 174 break; 175 case '|': // start of alternative and special case 178 176 first = 0; 179 177 push = pop = 0; 180 case ';': // print with whitespace178 case ';': // print with whitespace 181 179 cout << i->getWS() << i->getText(); 182 180 break; … … 191 189 } // if 192 190 } // if 193 // If there is a comment or this is the first RHS after 194 // ':' or '|', then include the whitespace before the 195 // token. Otherwise, fold the token onto the same line 196 // separated with a blank. 191 // If there is a comment or this is the first RHS after ':' or '|', then include the whitespace 192 // before the token. Otherwise, fold the token onto the same line separated with a blank. 197 193 string t1( i->getText() ); 198 194 if ( i->isComment() || first == 0 ) { … … 216 212 break; 217 213 } 218 case _LITERALBLOCK: // ignore code but print its comment, if any214 case _LITERALBLOCK: // ignore code but print its comment, if any 219 215 cout << tree->down->getComment(); 220 216 break; 221 case _DECLARATION: { // ignore certain declarations222 int kind = tree->down->getKind(); // get kind of declaration217 case _DECLARATION: { // ignore certain declarations 218 int kind = tree->down->getKind(); // get kind of declaration 223 219 if ( kind != UNION && kind != TYPE ) { 224 LaTeX( tree->down ); // print verbatim220 LaTeX( tree->down ); // print verbatim 225 221 } // if 226 222 break; 227 223 } 228 case _USERSECTION_OPT: // ignore but add newline at the end224 case _USERSECTION_OPT: // ignore but add newline at the end 229 225 cout << endl; 230 226 break; … … 237 233 238 234 239 void HTML( Token * tree ) {// prefix tree traversal235 void HTML( Token * tree ) { // prefix tree traversal 240 236 cerr << "ERROR: html style not implemented" << endl; 241 237 } // HTML -
tools/prettyprinter/filter.h
rc89503c rc9383ee 10 10 // Created On : Tue Apr 9 22:31:18 2002 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Jun 28 22:57:04201713 // Update Count : 912 // Last Modified On : Thu Jun 29 08:28:32 2017 13 // Update Count : 10 14 14 // 15 15 … … 19 19 #include "parser.hh" 20 20 21 extern void (* filter)( Token *tree );// pointer to filter for parse tree22 void freeTree( Token * tree );// free storage for parse tree21 extern void (* filter)( Token * tree ); // pointer to filter for parse tree 22 void freeTree( Token * tree ); // free storage for parse tree 23 23 24 void Identity( Token * tree );// parse-tree filters25 void Parse_Tree( Token * tree );26 void Nocode( Token * tree );27 void LaTeX( Token * tree );28 void HTML( Token * tree );24 void Identity( Token * tree ); // parse-tree filters 25 void Parse_Tree( Token * tree ); 26 void Nocode( Token * tree ); 27 void LaTeX( Token * tree ); 28 void HTML( Token * tree ); 29 29 30 30 #endif // __FILTER_H__ -
tools/prettyprinter/main.cc
rc89503c rc9383ee 10 10 // Created On : Wed Jun 28 22:57:26 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Jun 28 22:58:40201713 // Update Count : 312 // Last Modified On : Thu Jun 29 09:02:37 2017 13 // Update Count : 15 14 14 // 15 15 16 16 #include <iostream> 17 #include <fstream> 17 18 #include <string> 18 19 using namespace std; 20 #include <unistd.h> // close 19 21 #include "filter.h" 20 22 23 extern FILE * yyin; 24 extern int yylineno; 21 25 extern int yydebug; 22 26 extern int yyparse( void ); 23 27 24 28 int main( int argc, char *argv[] ) { 25 switch ( argc ) { 26 case 2: { 27 string arg( argv[1] ); 29 yyin = stdin; 30 filter = Nocode; 28 31 29 if ( arg == "-identity" ) { 30 filter = Identity; 31 } else if ( arg == "-parse_tree" ) { 32 filter = Parse_Tree; 33 } else if ( arg == "-nocode" ) { 34 filter = Nocode; 35 } else if ( arg == "-latex" ) { 36 filter = LaTeX; 37 } else if ( arg == "-html" ) { 38 filter = HTML; 39 } else { 40 cerr << "Unknown printer option: " << argv[1] << endl; 41 goto usage; 42 } // if 43 break; 44 } 45 usage: 46 default: 47 cerr << "Usage: " << argv[0] << " [-identity | -parse_tree | -nocode | -latex | -html] < bison.y" << endl; 48 exit( -1 ); 49 } 32 try { 33 switch ( argc ) { 34 case 3: 35 yyin = fopen( argv[ 2 ], "r" ); 36 if ( yyin == nullptr ) { 37 throw ios_base::failure( "unknown printer option arguments" ); 38 } // if 39 // FALL THROUGH 40 case 2: { 41 string arg( argv[1] ); 42 43 if ( arg == "-identity" ) { 44 filter = Identity; 45 } else if ( arg == "-parse_tree" ) { 46 filter = Parse_Tree; 47 } else if ( arg == "-nocode" ) { 48 filter = Nocode; 49 } else if ( arg == "-latex" ) { 50 filter = LaTeX; 51 } else if ( arg == "-html" ) { 52 filter = HTML; 53 } else { 54 throw ios_base::failure( "unknown printer option arguments" ); 55 } // if 56 break; 57 } 58 default: 59 throw ios_base::failure( "wrong number of arguments" ); 60 } // switch 61 } catch( ios_base::failure err ) { 62 cerr << err.what() << endl; 63 cerr << "Usage: [" << argv[0] 64 << "-identity |" 65 << "-parse_tree |" 66 << "-nocode |" 67 << "-latex |" 68 << "-html" 69 << "] [input-file]" 70 << endl; 71 exit( EXIT_FAILURE ); // TERMINATE 72 } // try 50 73 51 74 //yydebug = 1; 52 75 yyparse(); 53 } 76 77 if ( yyin != stdin ) fclose( yyin ); // close file, do not delete cin! 78 } // main 54 79 55 80 // Local Variables: // -
tools/prettyprinter/parser.yy
rc89503c rc9383ee 10 10 // Created On : Sat Dec 15 13:44:21 2001 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Jun 28 23:27:34201713 // Update Count : 104 312 // Last Modified On : Thu Jun 29 09:26:47 2017 13 // Update Count : 1045 14 14 // 15 15 … … 233 233 $$ = $1; 234 234 } 235 | THONG// bison235 | THONG // bison 236 236 ; 237 237 … … 276 276 ; 277 277 278 namenolist : nameno 279 { 280 //cerr << "namenolist1: " << $1->text << "(" << $1 << ")" << endl; 281 $$ = nameliststart = $1; 282 } 283 | namenolist nameno 284 { 285 //cerr << "namenolist2: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl; 286 $1->left = $2; 287 $$ = $2; 288 } 289 | namenolist ',' nameno 290 { 291 //cerr << "namenolist3: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ") " << $3->text << "(" << $3 << ")" << endl; 292 $1->left = $2; 293 $2->left = $3; 294 $$ = $3; 295 } 296 ; 297 298 nameno : name 299 { 300 $$ = new Token( "nameno", _NAMENO ); 301 $$->down = $1; 302 } 303 | name INTEGER 304 { 305 $$ = new Token( "nameno", _NAMENO ); 306 $1->left = $2; 307 $$->down = $1; 308 } 309 ; 310 311 namelist : name 312 { 313 //cerr << "namelist1: " << $1->text << "(" << $1 << ")" << endl; 314 $$ = nameliststart = $1; 315 } 316 | namelist name 317 { 318 //cerr << "namelist2: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl; 319 $1->left = $2; 320 $$ = $2; 321 } 322 | namelist ',' name 323 { 324 //cerr << "namelist3: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ") " << $3->text << "(" << $3 << ")" << endl; 325 $1->left = $2; 326 $2->left = $3; 327 $$ = $3; 328 } 329 ; 330 331 name : IDENTIFIER 332 | CHARACTER 333 ; 334 335 rulesection : rules 336 { 337 //cerr << "rulesection1: " << $1->text << "(" << $1 << ")" << endl; 338 $$ = new Token( "rulesection", _RULESECTION ); 339 $$->down = $1; 340 } 341 | error /* no rules */ 342 { 343 cerr << "no rules in the input grammar" << endl; 344 exit( -1 ); 345 } 346 ; 278 namenolist : 279 nameno 280 { 281 //cerr << "namenolist1: " << $1->text << "(" << $1 << ")" << endl; 282 $$ = nameliststart = $1; 283 } 284 | namenolist nameno 285 { 286 //cerr << "namenolist2: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl; 287 $1->left = $2; 288 $$ = $2; 289 } 290 | namenolist ',' nameno 291 { 292 //cerr << "namenolist3: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ") " << $3->text << "(" << $3 << ")" << endl; 293 $1->left = $2; 294 $2->left = $3; 295 $$ = $3; 296 } 297 ; 298 299 nameno : 300 name 301 { 302 $$ = new Token( "nameno", _NAMENO ); 303 $$->down = $1; 304 } 305 | name INTEGER 306 { 307 $$ = new Token( "nameno", _NAMENO ); 308 $1->left = $2; 309 $$->down = $1; 310 } 311 ; 312 313 namelist : 314 name 315 { 316 //cerr << "namelist1: " << $1->text << "(" << $1 << ")" << endl; 317 $$ = nameliststart = $1; 318 } 319 | namelist name 320 { 321 //cerr << "namelist2: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl; 322 $1->left = $2; 323 $$ = $2; 324 } 325 | namelist ',' name 326 { 327 //cerr << "namelist3: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ") " << $3->text << "(" << $3 << ")" << endl; 328 $1->left = $2; 329 $2->left = $3; 330 $$ = $3; 331 } 332 ; 333 334 name : 335 IDENTIFIER 336 | CHARACTER 337 ; 338 339 rulesection : 340 rules 341 { 342 //cerr << "rulesection1: " << $1->text << "(" << $1 << ")" << endl; 343 $$ = new Token( "rulesection", _RULESECTION ); 344 $$->down = $1; 345 } 346 | error // no rules 347 { 348 cerr << "no rules in the input grammar" << endl; 349 exit( -1 ); 350 } 351 ; 347 352 348 353 // These grammar rules are complex because the Yacc language is LR(2) due to the optional ';' at the end of rules. The … … 351 356 // (rules, rhs) being built but only one list tail can be returned through $$ for chaining. 352 357 353 rules : lhs rhs 354 { 355 //cerr << "rules1: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl; 356 $$ = rulestart; 357 } 358 | lhs rhs ';' 359 { 360 //cerr << "rules2: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ") " << $3->text << "(" << $3 << ")" << endl; 361 $2->addDownLeftTail( $3 ); 362 $$ = rulestart; 363 } 364 ; 365 366 lhs : IDENTIFIER ':' 367 { 368 //cerr << "lhs: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl; 369 $$ = new Token( "lhs", _LHS ); 370 //cerr << " lhs: " << $$->text << "(" << $$ << ")" << endl; 371 $1->left = $2; 372 $$->down = $1; 373 } 374 ; 358 rules : 359 lhs rhs 360 { 361 //cerr << "rules1: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl; 362 $$ = rulestart; 363 } 364 | lhs rhs ';' 365 { 366 //cerr << "rules2: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ") " << $3->text << "(" << $3 << ")" << endl; 367 $2->addDownLeftTail( $3 ); 368 $$ = rulestart; 369 } 370 ; 371 372 lhs : 373 IDENTIFIER ':' 374 { 375 //cerr << "lhs: " << $1->text << "(" << $1 << ") " << $2->text << "(" << $2 << ")" << endl; 376 $$ = new Token( "lhs", _LHS ); 377 //cerr << " lhs: " << $$->text << "(" << $$ << ")" << endl; 378 $1->left = $2; 379 $$->down = $1; 380 } 381 ; 375 382 376 383 rhs : … … 479 486 ccode_opt : 480 487 // empty 488 {} 481 489 | blocks 482 490 ; -
tools/prettyprinter/token.cc
rc89503c rc9383ee 10 10 // Created On : Wed Jun 28 22:46:23 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Jun 28 23:06:07201713 // Update Count : 812 // Last Modified On : Thu Jun 29 09:33:49 2017 13 // Update Count : 9 14 14 // 15 15 … … 20 20 #include "filter.h" 21 21 22 Token::Token( const string & text, int kind ) : text(text), kind(kind) {22 Token::Token( const string & text, int kind ) : text(text), kind(kind) { 23 23 left = down = NULL; 24 24 } // Token::Token 25 25 26 Token::Token( const string & text, list<string> &ws_list, int kind ) : text(text), kind(kind) {26 Token::Token( const string & text, list<string> & ws_list, int kind ) : text(text), kind(kind) { 27 27 // cerr << "Token3 : text \"" << text << "\""; 28 28 // for ( list<string>::iterator i = ws_list.begin(); i != ws_list.end(); i ++ ) { … … 36 36 } // Token::Token 37 37 38 void Token::addLeftTail( Token * n ) {39 Token * p = this;38 void Token::addLeftTail( Token * n ) { 39 Token * p = this; 40 40 while ( p->left != 0 ) { 41 41 p = p->left; … … 44 44 } // Token::addLeftTail 45 45 46 void Token::addDownLeftTail( Token * n ) {46 void Token::addDownLeftTail( Token * n ) { 47 47 if ( down == 0 ) { 48 48 down = n;
Note: See TracChangeset
for help on using the changeset viewer.