1 //ExplLexicalAnalyzer.cpp
2 #include
3 #include
4 #include
5 #include
6 #include "ExplLexicalAnalyzer.h"
7
8 using namespacestd;9
10 typedef enum{11 START, INASSIGN, INCOMMENT, INNUM, INID, DONE12 } StateType;13
14 char tokenString[MAXTOKENLEN + 1];15
16 #define BUFLEN 256
17
18 static charlineBuf[BUFLEN];19 static int linepos = 0;20 static int bufsize = 0;21 static int EOF_flag = false;22 static stringfilename;23 static fstream get;24 static int lineno = 0;25 static int columnpos = 0;26 bool TraceScan = true;27 StateType state;28
29 static struct{30 const char *str;31 TokenType tok;32 } reservedWords[MAXRESERVED]33 = {{"if", IF},34 {"then", THEN},35 {"else", ELSE},36 {"end", END},37 {"repeat", REPEAT},38 {"until", UNTIL},39 {"read", READ},40 {"write", WRITE},41 {"begin", BEGIN},42 {"var", VAR},43 {"interger", INTEGER},44 {"double", DOUBLE},45 {"string", STRING}};46
47
48 static char
49 getNextChar() {50 if (linepos >=bufsize) {51 lineno = 0;52 if (state !=START)53 return \' \';54 if (get.getline(lineBuf, BUFLEN - 1)) {55 printf("%d: %s\\n", columnpos++, lineBuf);56 bufsize = (int) strlen(lineBuf);57 linepos = 0;58 return lineBuf[linepos++];59 } else{60 returnEOF;61 }62 } else return lineBuf[linepos++];63 }64
65
66 static TokenType reservedLookup(char *s) {67 inti;68 for (i = 0; i < MAXRESERVED; i++)69 if (!strcmp(s, reservedWords[i].str))70 returnreservedWords[i].tok;71 returnID;72 }73
74 //退回一个字符
75 static void ungetNextChar(void) { if (!EOF_flag) linepos--; }76
77 //打印分析结果
78 void printToken(TokenType token, const char *tokenString) {79 switch(token) {80 caseIF:81 caseTHEN:82 caseELSE:83 caseEND:84 caseREPEAT:85 caseUNTIL:86 caseREAD:87 caseWRITE:88 caseBEGIN:89 caseVAR:90 caseINTEGER:91 caseDOUBLE:92 caseSTRING:93 printf("reserved word: %s\\n", tokenString);94 break;95 caseDEFINE:96 printf(":\\n");97 break;98 caseCOMMA:99 printf(",\\n");100 break;101 caseASSIGN:102 printf(":=\\n");103 break;104 caseLT:105 printf("
147
148 //词法分析
149 TokenType getToken(void) {150 int tokenStringIndex = 0;151 TokenType currentToken;152 state =START;153 boolsave;154 while (state !=DONE) {155 char c =getNextChar();156 save = true;157 switch(state) {158 caseSTART:159 if(isdigit(c))160 state =INNUM;161 else if(isalpha(c))162 state =INID;163 else if (c == \':\')164 state =INASSIGN;165 else if ((c == \' \') || (c == \'\\t\') || (c == \'\\n\'))166 save = false;167 else if (c == \'{\') {168 save = false;169 state =INCOMMENT;170 } else{171 state =DONE;172 switch(c) {173 caseEOF:174 returnENDFILE;175 case \',\':176 currentToken =COMMA;177 break;178 case \'=\':179 currentToken =EQ;180 break;181 case \'
268
269 int
270 main() {271 if (cin >> filename && filename == "q") {272 filename = "......";273 }274 get.open(filename, ios::in);275 while (getToken() !=ENDFILE);276 }