else { } return t; }
DFAState Scanner :: charType(char c)//返回字符的类型 {
if(' '==c || '\\n'==c || '\\t'==c ||'\\r'==c) }
char Scanner :: getNextChar() {
if(charIndex void Scanner :: backToLastChar() return '\\0'; char ch = sourseString[charIndex]; charIndex++; if('\\n'==ch) lineCount++; return ch; return START; return INNUM; return INID; return INDBSYM; return DONE; else if(c>='0'&&c<='9') else if((c>='A'&&c<='Z')||(c>='a'&&c<='z')) else if(c=='<' || c=='>' || c=='=' || c=='!') else t = ERROR; { if(charIndex>0) { } } void Scanner :: printToken() { ofstream fout_Token(\ ifstream fin(\ string temp; int lineCount = 0; int index = 0; while(getline(fin,temp)) { fout_Token< Token t = tokenList.at(index); if(lineCount==t.lineNo) { fout_Token<<\ \ [\index++; int width = 10; string headS = \ if(t.tokenType>=1&&t.tokenType<=6)//关键字 { string tp = \ for(int i = 0; i tp += \ char ch = sourseString[charIndex-1]; charIndex--; if('\\n'==ch) lineCount--; } fout_Token<<\:\ \ else if(t.tokenType>=7&&t.tokenType<=27)//符号 { } else if(t.tokenType==28)//NUM { } else if(t.tokenType==29)//ID { } else if(t.tokenType==30)//错误 { string tp = \ for(int i = 0; i tp += \ error] : \ fout_Token<<\string tp = \ for(int i = 0; i tp += \ ID]:\ fout_Token<<\string tp = \ for(int i = 0; i tp += \ NUM] :\ fout_Token<<\string tp = \ for(int i = 0; i tp += \ : \ fout_Token<<\ \ \ \ \ } } } } else if(t.tokenType==ENDFILE)//结束 { } fout_Token<<\ \fout_Token< \ if(lineCount lineCount++; fin.close(); fout_Token.close(); } scanner.h: #include //定义的Token的类型(31种),分别对应于else、if、int、return、void、while、+、-、*、/、<、<=、>、>=、==、!=、=、;、,、(、)、[、]、{、}、/*、*/、num、id、错误、结束 typedef enum { ELSE = 1,IF,INT,RETURN,VOID,WHILE, PLUS,MINUS,TIMES,OVER,LT,LEQ,GT,GEQ,EQ,NEQ,ASSIGN,SEMI,COMMA,LPAREN,RPAREN,LMBRACKET,RMBRACKET,LBBRACKET,RBBRACKET,LCOMMENT,RCOMMENT, NUM,ID,ERROR,ENDFILE } TokenType; typedef enum { START = 1, INNUM, INID, INDBSYM, DONE } DFAState;