?? temp.txt
字號:
1 ############################################################# 2 # file: Makefile 3 # author: wuzuyang 4 ############################################################# 5 6 CC = g++ 7 OBJS = Lexer.o Parser.o SyntaxTree.o SyntaxTreeNode.o 8 PROG = compiler 9 10 $(PROG):$(OBJS) 11 $(CC) -o $(PROG) $(OBJS) main.cpp 12 # strip $(PROG) 13 14 Lexer.o: Lexer.h Lexer.cpp 15 $(CC) -c Lexer.cpp 16 17 SyntaxTreeNode.o: SyntaxTreeNode.h SyntaxTreeNode.cpp 18 $(CC) -c SyntaxTreeNode.cpp 19 20 SyntaxTree.o: SyntaxTree.h SyntaxTree.cpp 21 $(CC) -c SyntaxTree.cpp 22 23 Parser.o: Parser.h Parser.cpp 24 $(CC) -c Parser.cpp 25 26 clean: 27 rm -f $(PROG) $(OBJS) 28 29 /************************************************************ 30 * file: common.h 31 * date: 2006-03-28 32 * author: wuzuyang 33 * describe: none; 34 *************************************************************/ 35 #ifndef COMMON_H 36 #define COMMON_H 37 38 typedef enum{ 39 ERROR, UNKNOWN, 40 LEXER_DONE, // success! 41 IF, THEN, ELSE, WHILE, DO, BEGIN, END, //9 42 ID, NUM, //11 43 AND, OR, 44 LT, LE, GT, GE, EQ, //18 45 ASSIGN, PLUS, MINUS, MUL, DIV, //23 46 LP, RP, SEMI 47 }TokenType; 48 49 50 typedef struct{ 51 TokenType type; 52 char *name; 53 }Token; 54 55 56 typedef enum{ 57 INERROR, 58 START, 59 DONE, 60 INID, 61 INNUM, 62 INLE, // LT & LE 63 INGE, // GT & GE 64 INEQ, // EQ & ASSIGN 65 INCOMMENT 66 }ScannerState; 67 68 69 typedef enum{ 70 ST_ERROR, 71 ST_NORMAL, // undefined, but not error 72 ST_EXP, // expression 73 ST_IF, ST_THEN, ST_WHILE, ST_ASSIGN, ST_BEGIN 74 }StatementType; 75 76 #endif 77 78 79 80 /************************************************************ 81 * file: Lexer.h 82 * date: 2006-03-31 83 * author: wuzuyang 84 * describe: none; lexer 85 *************************************************************/ 86 87 #include "common.h" 88 89 class Lexer{ 90 private: 91 char *src; 92 int length; 93 int index; 94 char *buf; 95 96 public: 97 Lexer(char *filename); 98 ~Lexer(); 99 void reset(); // rewind index 100 bool isReady(); 101 bool isFinished(); 102 char *getSrc(); 103 int getIndex(); 104 105 Token nextToken(); 106 }; 107 108 109 110 /************************************************** 111 * file: Parser.h 112 * date: 2006-04-12 113 * author: wuzuyang 114 * describe: none; parser 115 **************************************************/ 116 117 #include "common.h" 118 #include "Lexer.h" 119 #include "SyntaxTree.h" 120 121 122 class Parser{ 123 private: 124 Lexer *lexer; 125 Token currentToken; 126 127 Token nextToken(); 128 129 SyntaxTree* Statement(); 130 SyntaxTree* Assign(); 131 SyntaxTree* Expression(); 132 SyntaxTree* T(); 133 SyntaxTree* F(); 134 SyntaxTree* Condition(); 135 SyntaxTree* Boolean(); 136 SyntaxTree* T2(); 137 SyntaxTree* F2(); 138 SyntaxTree* While(); 139 SyntaxTree* Begin(); 140 SyntaxTree* Block(); 141 142 void printError(const char *error); 143 144 public: 145 Parser(char* sourcefile); 146 ~Parser(); 147 148 void printError(); 149 150 SyntaxTree* parse(); 151 }; 152 153 154 155 156 /************************************************************ 157 * file: SyntaxTree.h 158 * date: 2006-04-12 159 * author: wuzuyang 160 * describe: none; SyntaxTree 161 *************************************************************/ 162 163 #include "common.h" 164 #include "SyntaxTreeNode.h" 165 166 class SyntaxTree{ 167 private: 168 SyntaxTreeNode *root; 169 void display(SyntaxTreeNode *n, int tabcount, FILE *fo=stdout); 170 171 public: 172 SyntaxTree(TokenType t, int val=0); 173 // set root node with n. 174 SyntaxTree(SyntaxTreeNode *n); 175 SyntaxTree(SyntaxTree *tree); 176 SyntaxTree(); // with root.type = ERROR 177 ~SyntaxTree(); 178 179 void display(FILE *fo=stdout); 180 181 void setRootNode(); 182 void setRootNode(TokenType t, int val=0); 183 // set root node with n. 184 void setRootNode(SyntaxTreeNode *n); 185 186 void addLeft(TokenType t, int val=0); 187 // add the subtree n to this tree's left 188 void addLeft(SyntaxTree *n); 189 190 void addRight(TokenType t, int val=0); 191 // add the subtree n to this tree's right 192 void addRight(SyntaxTree *n); 193 194 void addChild3(TokenType t, int val=0); 195 // add the subtree n to this tree's child3 196 void addChild3(SyntaxTree *n); 197 198 SyntaxTreeNode* getRootNode(); 199 200 SyntaxTree* getLeft(); 201 SyntaxTree* getRight(); 202 SyntaxTree* getChild3(); 203 204 }; 205 206 207 208 /************************************************************ 209 * file: SyntaxTreeNode.h 210 * date: 2006-04-12 211 * author: wuzuyang 212 * describe: none; SyntaxTreeNode 213 *************************************************************/ 214 215 #include <stdio.h> 216 #include "common.h" 217 218 class SyntaxTreeNode{ 219 private: 220 TokenType type; 221 int value; // identifiers will be given distinguish values. 222 SyntaxTreeNode *left, *right, *child3; 223 224 public: 225 SyntaxTreeNode(); 226 SyntaxTreeNode(TokenType t, int val); 227 ~SyntaxTreeNode(); 228 229 void setType(TokenType t); 230 void setValue(int val); 231 232 TokenType getType(); 233 int getValue(); 234 235 void addLeft(TokenType t, int val); 236 // add the subtree n to this tree's left 237 void addLeft(SyntaxTreeNode *n); 238 239 void addRight(TokenType t, int val); 240 // add the subtree n to this tree's right 241 void addRight(SyntaxTreeNode *n); 242 243 void addChild3(TokenType t, int val); 244 // add the subtree n to this tree's child3 245 void addChild3(SyntaxTreeNode *n); 246 247 SyntaxTreeNode* getRootNode(); 248 249 SyntaxTreeNode* getLeft(); 250 SyntaxTreeNode* getRight(); 251 SyntaxTreeNode* getChild3(); 252 253 friend class SyntaxTree; 254 }; 255 256 257 258 /************************************************************ 259 * file: common.h 260 * date: 2006-03-28 261 * author: wuzuyang 262 * describe: none; 263 *************************************************************/ 264 #ifndef COMMON_H 265 #define COMMON_H 266 267 typedef enum{ 268 ERROR, UNKNOWN, 269 LEXER_DONE, // success! 270 IF, THEN, ELSE, WHILE, DO, BEGIN, END, //9 271 ID, NUM, //11 272 AND, OR, 273 LT, LE, GT, GE, EQ, //18 274 ASSIGN, PLUS, MINUS, MUL, DIV, //23 275 LP, RP, SEMI 276 }TokenType; 277 278 279 typedef struct{ 280 TokenType type; 281 char *name; 282 }Token; 283 284 285 typedef enum{ 286 INERROR, 287 START, 288 DONE, 289 INID, 290 INNUM, 291 INLE, // LT & LE 292 INGE, // GT & GE 293 INEQ, // EQ & ASSIGN 294 }ScannerState; 295 296 297 typedef enum{ 298 ST_ERROR, 299 ST_NORMAL, // undefined, but not error 300 ST_EXP, // expression 301 ST_IF, ST_THEN, ST_WHILE, ST_ASSIGN, ST_BEGIN 302 }StatementType; 303 304 #endif 305 306 307 308 /************************************************************ 309 * file: Lexer.h 310 * date: 2006-03-31 311 * author: wuzuyang 312 * describe: none; lexer 313 *************************************************************/ 314 315 #include "common.h" 316 317 class Lexer{ 318 private: 319 char *src; 320 int length; 321 int index; 322 char *buf; 323 324 public: 325 Lexer(char *filename); 326 ~Lexer(); 327 void reset(); 328 bool isReady(); 329 bool isFinished(); 330 char *getSrc(); 331 int getIndex(); 332 333 Token nextToken(); 334 }; 335 336 337 338 /************************************************************ 339 * file: Lexer.cpp 340 * date: 2006-03-31 341 * author: wuzuyang 342 * describe: none; 343 *************************************************************/ 344 345 #include "Lexer.h" 346 #include <stdio.h> 347 #include <string.h> 348 349 #define MAXRESERVED 7 350 #define TOKENBUFSIZE 64 351 352 void getSingleOperator(char c, Token &token); 353 void reservedLookup(Token &token); 354 355 356 static Token ReservedWords[MAXRESERVED] = { 357 {IF, "if"}, 358 {THEN, "then"}, 359 {ELSE, "else"}, 360 {WHILE, "while"}, 361 {DO, "do"}, 362 {BEGIN, "begin"}, 363 {END, "end"} 364 }; 365 366 367 Lexer::Lexer(char *filename){ 368 buf = new char[TOKENBUFSIZE]; 369 FILE *fp = fopen(filename, "r"); 370 if(fp==NULL){ 371 src = NULL; 372 printf("\n\n********************************************\n"); 373 printf("* FATAL ERROR! LEXER COULD NOT OPEN FILE!!!\n"); 374 printf("********************************************\n\n"); 375 length = 0; 376 }else{ 377 int i = 0; 378 while(fgetc(fp)!=EOF){ 379 i++; 380 } 381 length = i; 382 src = new char[i+1]; 383 384 rewind(fp); 385 i = 0; 386 while(src[i] = fgetc(fp)){ 387 if(src[i] == EOF){ 388 src[i] = '\0'; 389 break; 390 } 391 i++; 392 } 393 394 fclose(fp); 395 } 396 index = 0; 397 } 398 399 Lexer::~Lexer(){ 400 delete[] src; 401 } 402 403 void Lexer::reset(){ 404 index = 0; 405 } 406 407 bool Lexer::isFinished(){ 408 return (index == length - 1); 409 } 410 411 bool Lexer::isReady(){ 412 return (src != NULL); 413 } 414 415 char* Lexer::getSrc(){ 416 return src; 417 } 418 419 int Lexer::getIndex(){ 420 return index; 421 } 422 423 424 /***=======================================================****/ 425 426 427 Token Lexer::nextToken(){ 428 Token token; 429 ScannerState state = START; 430 int bufindex = 0; 431 bool next = true; // index++ 432 char c; 433 434 if(index==length-1){ 435 token.type = ERROR; 436 token.name = "NO CHAR LEFT."; 437 return token; 438 } 439 440 c = src[index]; 441 while(c==' ' || c=='\n' || c=='\r' || c=='\t'){ 442 index ++; 443 c = src[index]; 444 } 445 446 // get started 447 if((c>='a' && c<='z') || (c>='A' && c<='Z')){ 448 state = INID; 449 token.type = ID; 450 buf[bufindex++] = c; 451 }else if(c>='0' && c<='9'){ 452 state = INNUM; 453 token.type = NUM; 454 buf[bufindex++] = c; 455 }else if(c=='='){ 456 state = INEQ; 457 }else if(c=='<'){ 458 state = INLE; 459 }else if(c=='>'){ 460 state = INGE; 461 }else{ 462 state = DONE; 463 getSingleOperator(c, token); 464 } 465 index ++; 466 467 while(state!=DONE){ 468 c = src[index]; 469 switch(state){ 470 case INEQ: 471 if(c=='='){ 472 token.type = EQ; 473 token.name = "=="; 474 }else{ 475 token.type = ASSIGN; 476 token.name = "="; 477 next = false; 478 } 479 state = DONE; 480 break; 481 case INLE: 482 if(c=='='){ 483 token.type = LE; 484 token.name = "<="; 485 }else{ 486 token.type = LT; 487 token.name = "<"; 488 next = false; 489 } 490 state = DONE; 491 break; 492 case INGE: 493 if(c=='='){ 494 token.type = GE; 495 token.name = ">="; 496 }else{ 497 token.type = GT; 498 token.name = ">"; 499 next = false; 500 } 501 state = DONE; 502 break; 503 case INID: 504 if((c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9')){ 505 buf[bufindex++] = c; 506 }else{ 507 state = DONE; 508 next = false; 509 } 510 break; 511 case INNUM: 512 if(c>='0' && c<='9'){ 513 buf[bufindex++] = c; 514 }else{ 515 state = DONE; 516 next = false; 517 } 518 break; 519 default: 520 state = DONE; 521 token.type = ERROR; 522 token.name = "ERROR!"; 523 printf("Error! Because no state is define! This should never happen! \ 524 Current character is: %c\n", src[index]); 525 break; 526 }// end scanner state 527 index ++;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -