实验1 词法分析程序设计
【開發(fā)語言及實(shí)現(xiàn)平臺(tái)或?qū)嶒?yàn)環(huán)境】
C++/Clion
【實(shí)驗(yàn)?zāi)康摹?/strong>
(1)理解詞法分析在編譯程序中的作用
(2)加深對(duì)有窮自動(dòng)機(jī)模型的理解
(3)掌握詞法分析程序的實(shí)現(xiàn)方法和技術(shù)
【實(shí)驗(yàn)內(nèi)容】
對(duì)一個(gè)簡(jiǎn)單語言的子集編制一個(gè)一遍掃描的詞法分析程序。
【實(shí)驗(yàn)要求】
(1)待分析的簡(jiǎn)單語言的詞法
begin if then while do end
:= + - * / < <= > >= <> = ; ( ) #
ID=letter(letter|digit)*
NUM=digitdigit*
(2)各種單詞符號(hào)對(duì)應(yīng)的種別編碼
(3)詞法分析程序的功能
輸入:所給文法的源程序字符串
輸出:二元組(syn,token或sum)構(gòu)成的序列。
syn為單詞種別碼;
token為存放的單詞自身字符串;
sum為整形常數(shù)。
例如:對(duì)源程序begin x:=9;if x>0 then x:=2*x+1/3;end# 經(jīng)詞法分析后輸出如下序列:(1,begin)(10,’x’) (18,:=) (11,9) (26,; ) (2,if)……
【實(shí)驗(yàn)步驟】
(1)根據(jù)圖1.1構(gòu)建主程序框架
代碼提示:
(2)關(guān)鍵字表置初值
關(guān)鍵字作為特殊標(biāo)識(shí)符處理,把它們預(yù)先安排在一張表格中(關(guān)鍵字表),當(dāng)掃描程序識(shí)別標(biāo)識(shí)符時(shí),查關(guān)鍵字表。如能查到匹配的單詞,則為關(guān)鍵字,否則為一般標(biāo)識(shí)符。
(3)編寫掃描子程序
代碼提示:
(4)調(diào)試程序,驗(yàn)證輸出結(jié)果。
【實(shí)驗(yàn)代碼】
#include <iostream> #include <string>using namespace std;// 關(guān)鍵字表置初始值 string keyword[30] = {"#", "begin", "if", "then", "while", "do", "end", "", "", "","letter(letter|digit)*", "digitdigit*", "", "+", "-", "*", "/",":", ":=", "", "<", "<>", "<=", ">", ">=", "=", ";", "(", ")"};class word { public:int syn{};string token; };// 處理單詞的函數(shù) word letterAnalysis(const string &subCode) {word item;if (subCode.substr(0, 5) == "begin") {item.syn = 1;} else if (subCode.substr(0, 2) == "if") {item.syn = 2;} else if (subCode.substr(0, 4) == "then") {item.syn = 3;} else if (subCode.substr(0, 5) == "while") {item.syn = 4;} else if (subCode.substr(0, 2) == "do") {item.syn = 5;} else if (subCode.substr(0, 3) == "end") {item.syn = 6;} else {// 如果是其它單詞,截取到第一個(gè)非字符for (int i = 0; i < subCode.length(); ++i) {if (!(subCode[i] > 'a' && subCode[i] < 'z')) {item.syn = 10;keyword[item.syn] = subCode.substr(0, i);break;}}}item.token = keyword[item.syn];return item; }// 處理數(shù)字的函數(shù) word numberAnalysis(string subCode) {word item;item.syn = 11;for (int i = 0; i < subCode.length(); ++i) {// 截取到第一個(gè)非數(shù)字字符if (!(subCode[i] >= '0' && subCode[i] <= '9')) {keyword[item.syn] = subCode.substr(0, i);break;}}item.token = keyword[item.syn];return item; }// 處理字符的函數(shù) word charAnalysis(string subCode) {word item;switch (subCode[0]) {case '#':item.syn = 0;break;case '+':item.syn = 13;break;case '-':item.syn = 14;break;case '*':item.syn = 15;break;case '/':item.syn = 16;break;case ':':if (subCode[1] == '=') {item.syn = 18;} else {item.syn = 17;}break;case '<':if (subCode[1] == '>') {item.syn = 21;} else if (subCode[1] == '=') {item.syn = 22;} else {item.syn = 20;}break;case '>':if (subCode[1] == '=') {item.syn = 24;} else {item.syn = 23;}break;case '=':item.syn = 25;break;case ';':item.syn = 26;break;case '(':item.syn = 27;break;case ')':item.syn = 28;break;}item.token = keyword[item.syn];return item; }// 詞法分析 void scanner(const string &code) {for (int i = 0; i < code.length(); ++i) {word item;if (code[i] > 'a' && code[i] < 'z') {// 處理單詞item = letterAnalysis(code.substr(i, code.length() - i + 1));} else if (code[i] >= '0' and code[i] <= '9') {// 處理數(shù)字item = numberAnalysis(code.substr(i, code.length() - i + 1));} else if (code[i] == ' ') {// 如果是空格,直接跳過continue;} else {// 處理特殊符號(hào)item = charAnalysis(code.substr(i, code.length() - i + 1));}i += int(item.token.length()) - 1;cout << "(" << item.syn << "," << item.token << ")" << endl;} }int main() {string code;cout << "Please input string:";// 讀入一行代碼,因?yàn)榇a中有空格,所以要用 getlinegetline(cin, code);scanner(code);return 0; }【運(yùn)行結(jié)果】
總結(jié)
以上是生活随笔為你收集整理的实验1 词法分析程序设计的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 124. Binary Tree Max
- 下一篇: 实验2 递归下降语法分析程序设计