




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、現在在學編譯原理,做了個簡單的詞法分析器。可以自定義關鍵字、界 符以及運算符。效果如下:請輸入要解析的文件名template, txt請輸入該編程語言的關鍵字if while else return請輸入該編程語言的運算符,格式為符號名稱 符號+ jia - jian * cheng / chu = fuzhi = deng請輸入該編程語言的界符,格式為符號名稱 符號(left ) right $ comma ; fengao dakuohao xiaokuohao 遼azint$標識符main$標識符($界符left)$界 fright$ 界符 dakuohaoint$柿識符a$棕識符;$
2、界符 f engaoint$襪識符b$棕識符;$ 界符 f engaoint$襪識符c$棕識符;$ 界符 f engaoc$襪識符+$運算待jiac$標識符=$運算符fuzhia$標識符;$ 界符 f engaowhile$關讎字、1($界符leftc$襪識符=$運算符fuzhi=$運負持fuzhi1$整型)$界 fright$ 界符 dakuohaoa$棕識符有一點不滿意的地方是必須輸入兩次ctrl+z,原來打算的是按下回車就結束輸入的 還有一點想法是不輸入這些設置,而是讀取一個配置文件,識別關鍵字, 界符以及運算符 代碼如下:#include<iostream>#include
3、<vector>#inelude <map>#include<iterator>#i nclude<sstream>#includcstring>#includefstream> using namespace std;vector <string> keywords;vector <pair<string, string> > symble;vector <pair<string,string> > bound; char ch;int iskeyword(const st
4、ring& s, int& n)if(keywords empty ()for(vector<string>:iterator it = keywordsbegin();it !二 keywords end() ;it+, n+)if(s = *it)return 1;return t;int issymble(const string& s, string& result)for(vector<pair<string, string> >:i terator i t 二symble. begin ();it !二 symbl
5、e. encl() ;it+)if(s = (*it).first)result 二(*it)second; return 0;int isbound(const string& s, string& result)!for(vector<pair<string, string> >:iterator it 二 bound. begin ();it !二 bound.end();it+)if (s = (*it) first)result = (*it)second;return 1;retum 0;void analyse (file *fp)stri
6、ng temp 二string str 二string result 二 ;int id = 0;while(ch = fgetc(fp) != eof) temp =;str = ch;id 二 0;if (ch 二二,| | ch 二二t,| | ch 二二n ch 二二,r ) while (ch 二二i ch 二二t' | | ch 二二n'| ch 二二')ch = fgetc(fp);;fseek(fp,-ll, seek_cur);else if(isalpha(ch)while(i salpha(ch) | | isdigi t (ch)temp 二 t
7、emp + ch;ch 二 fgetc (fp);fscek(fp,-ll, seek_cur);if(iskeyword(temp, id) = 1)endl;cout << temp << t$關鍵字,"idelsecout << temp << 七$標識符 « endl:else if(isdigit (ch)while(isdigit (ch)temp = temp + ch;ch = fgetc (fp);fscck(fp,-ll, seek_cur);cout << temp << “t$整
8、型 << endl:else if(issymble (str, result)cout << ch << t$ << 運算符"<< result << endl;/*case '+' :cout « ch << “t$add << endl;break;case: cout « ch « t$subtractendl;break;case:cout « ch << t$divide << endl;break
9、;case:cout « ch << t$assign << endl;break;case,(':cout << ch << t$lpar << endl;break;case:cout << ch << t$rpar << endl;break;case:cout << ch « “t$lsb << endl;break;case:cout << ch << “t$rsb << endl:break;case
10、:cout << ch « t$semicolon << endl;break;case:cout << ch << “t$dot << endl:break;case:cout << ch << t$c0mma << endl;break;case,':cout << ch << t$lbrace << endl; break:case:cout ch << “t$rbrace << endl;break;: cout
11、« ch « t$multiplyendl;break;case:cout << ch « t$unknow << endl;*/defaultelse if(isbound(str, result)cout << ch t$ « 界符 << result « endl;el secout << ch << t$未知 « endl;int mainostring line,symbelline, boundline, word, filename, symblen
12、ame, symbleld, boundn ame, boundld;cout請輸入要解析的文件名endl:cin > filename;cout « 請輸入該編程語言的關鍵字 endl;whi le(getline(cin, 1 ine)istringstream stream (line);while(stream >> word)keywords. push back(word);cin. clear();cout « 請輸入該編程語言的運算符,格式為符號名稱 符號 « endl;while(get1ine(cin, symbelline)
13、istringstream stream(symbelline);while(stream >> symblename >> symbleld)symble push back(pair<string, string> (symblename, s ymbleld);cin. clear ();cout請輸入該編程語言的界符,格式為符號名稱符號 « endl;while(getline(cin, boundline)istringstream stream (boundline);whilc(stream >> boundxamo >> boundtd)bound, push-back(pair<string, str
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 平臺護欄改造方案(3篇)
- 農場項目規劃方案(3篇)
- 理賠流程方案模板(3篇)
- 完善住宅工程質量檢測市場信息透明度
- 提高標準化冷藏車運營效率的技術手段
- 領導考察調研報道
- 關于科研課題研究方案
- 教育信息技術論文題目
- 小學語音教學論文
- 會計學成教本科畢業論文
- 基于UHPC的蝶形腹板混凝土拱橋創新設計研究
- 口腔科針刺傷處理流程
- 2025-2030中國雷達告警接收機行業市場發展趨勢與前景展望戰略研究報告
- 一例高血壓合并糖尿病患者的個案護理課件
- 2025年中考地理務必掌握的答題思路與模板
- 臨時占地免責協議書
- 工會法律知識培訓課件
- 檔案管理員實操能力考試題試題及答案
- 供應鏈風險管理知識點及試題及答案
- 隱患排查五定制度
- BRCGS全球標準食品安全第9版標準要求
評論
0/150
提交評論