學習計算機或者軟件工程的人都知道編譯原理是一門很是重要的課程,下面是本人學習編譯原理事後寫的詞法分析的源碼,喜歡的能夠看看,順便我附上一個下載地址:ios
地址一:http://www.quzhuanpan.com/home/sourceList.jsp?type=6c++
地址二: http://www.quzhuanpan.com/download/checkResult.action?id=29&type=6git
####include <iostream> #####include <string.h> #####include <stdio.h> #####include <stdlib.h> #####include <map> #####include <set> #####include <sstream> using namespace std; class Content { private: int id; int forToken; string content; char character; char *token; char *characterSet; int c; string str; map<int,string> store; public: string getStr(); long getLength(); int getC(); void display(); bool operator==(char rch); Content(string content); ~Content(); void stringToChar(); char get_Char(); bool getbe(); void concatenation(); bool letter(); bool digit(); int reserve(); void retract(); void buildList(); string error(); string analysis(); }; //bool Content::operator==(char rch){ // char lch; // if(strcpy(&lch,&rch)==0){ // return 1; // } // else // return 0; //} Content::Content(string _content) { content=_content; c=0; id=0; character=' '; forToken=0; str=""; characterSet=new char[content.length()+1];//最後又'\0' token=new char[100]; } Content::~Content() { free(characterSet); free(token); delete []characterSet; delete []token; } void Content::stringToChar() { strcpy(characterSet,content.c_str()); //返回的是const char*,也就是字符數組的首地址 } /* www.panmama.com 轉載請務必告知 */ int Content::getC() { return c; } long Content::getLength() { return content.length(); } char Content::get_Char() { character=characterSet[c]; if(!(character==' ')||!(character!='\n')||!(character!='\t')) { c++; }//非空符號,指針向後加1 return character; } bool Content::getbe() { if(character==' '||character=='\n'||character=='\t') { c++; return 1; } return 0; } void Content::display() { for(int i=0; i<content.length(); i++) { cout<<content.length(); cout<<characterSet[i]<<endl; } } void Content::concatenation() { token[forToken]=character; forToken++; } string Content::getStr(){ int i=0; string s=""; while(token[i]!=' ') { s+=token[i]; i++; } return s; } bool Content::digit() { if(character>='0'&&character<='9') return true; return false; } bool Content::letter() { if((character>='a'&&character<='z')||(character>='A'&&character<='Z')) { return true; } return false; } int Content::reserve() { // int i=0; // str=""; // while(token[i]!=' ') // { // str+=token[i]; // i++; // } str=getStr(); if(str=="if") { return 4; } else if(str=="int") { return 1; } else if(str=="for") { return 6; } else if(str=="char") { return 3; } else if(str=="void") { return 12; } else if(str=="main") { return 2; } else if(str=="else") { return 5; } else if(str=="case") { return 9; } else if(str=="while") { return 7; } else if(str=="float") { return 11; } else if(str=="const") { return 12; } else if(str=="break") { return 13; } else if(str=="switch") { return 8; } else if(str=="return") { return 14; } else if(str=="switch") { return 8; } else if(str=="double") { return 15; } else if(str=="string") { return 16; } else if(str=="printf") { return 17; } else { return 0; } } void Content::retract() { c--; character=' '; } void Content::buildList() { //id=18; store.insert(pair<int,string>(1,"int")); store.insert(pair<int,string>(2,"main")); store.insert(pair<int,string>(3,"char")); store.insert(pair<int,string>(4,"if")); store.insert(pair<int,string>(5,"else")); store.insert(pair<int,string>(6,"for")); store.insert(pair<int,string>(7,"while")); store.insert(pair<int,string>(8,"switch")); store.insert(pair<int,string>(9,"case")); store.insert(pair<int,string>(11,"float")); store.insert(pair<int,string>(12,"const")); store.insert(pair<int,string>(13,"break")); store.insert(pair<int,string>(14,"return")); store.insert(pair<int,string>(15,"double")); store.insert(pair<int,string>(16,"string")); store.insert(pair<int,string>(17,"printf")); store.insert(pair<int,string>(id,str)); // if(id!=20) // id++; // else // id=21; } string Content::error() { return "有誤"; } string Content::analysis() { char s; string num; int k; for(int i=0; i<100; i++) { token[i]=' '; } s=get_Char();//從數組中取一個字符 if(getbe())//是空格 return "";//是空格返回「」 switch(s) { case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': while(letter()||digit()) { concatenation(); get_Char(); } forToken=0; k=reserve(); if(k==0) { buildList(); return "(10,ID)"; } else { buildList(); stringstream ss; string first; string second; ss<<k; first=ss.str();//以上爲將int型轉化爲string型 map<int,string>::iterator iter; for(iter=store.begin(); iter!=store.end(); iter++) { if(iter->first==k) second=iter->second; } return "("+first+","+second+")"; } break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': while(digit()) { concatenation(); get_Char(); } retract(); //budlist();對其登記 num=getStr(); return "("+num+",NUM)"; break; case'+': return "(22,+)"; break; case'-': return "(23,-)"; break; case'*': return "(24,*)"; break; case'/': return "(25,/)"; break; case'(': return "(26,()"; break; case')': return "(27,))"; break; case'[': return "(28,[)"; break; case']': return "(29,])"; break; case'{': return "(30,{)"; break; case'}': return "(31,})"; break; case'"': return "(41,\")"; break; case',': return "(32,,)"; break; case':': return "(33,:)"; break; case';': return "(34,;)"; break; case'<': get_Char(); if(character=='=') { return "(38,<=)"; } else { retract(); return "(36,<)"; } break; case'>': get_Char(); if(character=='=') { return "(37,>=)"; } else { retract(); return "(35,>)"; } break; case'=': get_Char(); if(character=='=') { return "(39,==)"; } else { retract(); return "(21,=)"; } break; case'!': get_Char(); if(character=='=') { return "(40,!=)"; } else { retract(); } break; case '\0': return "1000"; break; default: error(); } } int main() { string temp="int main(){int c=33;if(c=33){printf();}}"; Content b=(temp); b.stringToChar(); //c.display(); while(b.getC()<b.getLength()){ string s=b.analysis(); if(s!="") cout<<s<<endl; } return 0; }
本人建個qq羣,歡迎你們一塊兒交流技術, 羣號:512245829 喜歡微博的朋友關注:轉盤娛樂便可數組