資源描述:
《編譯原理-詞法分析源碼》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、編譯原理?詞法分析lexicalAnalvsis.h2svmboltable.h6main.cpp10PS:此實驗為湖南大學(xué)編譯原理課程實驗創(chuàng)建工程,將三個文件導(dǎo)入后可直接使用lexicalAnalysis.h#inelude#ineludeusingnamespacestd;#inelude"symbol_table.h"/*關(guān)鍵字判斷*/booljudge_key_word(stringword){stringkey_word[6]={"begin","if","then","while"
2、,"do","end"};if(word=="begin"){insert("begin",l);returntrue;}else訐(word=="if"){insert(”if”,2);returntrue;}else訐(word=="then"){insert("then",3);returntrue;}else訐(word=="while'*){insertC'while"^);returntrue;}else訐(word=="do"){insertC'do"^);returntrue;}elseif(word=="en
3、d”){insert("end",6);returntrue;}else{returnfalse;}}/*判斷運算符和界符*/booljudge_operator_and_edgeSymbol(stringword)訐(word=="+"){insert(”+”,13);returntrue;}elseif(word=={insert(":",14);returntrue;}elseif(word=="*n){insert("*",15);returntrue;}elseif(word=="/"){insert("/"/16)
4、;returntrue;}else訐(word=={insert(”:“,17);returntrue;}else訐(word=={insert(”:-',18);returntrue;}else訐(word=="<"){insert("<",20);returntrue;}else訐(word=="<>"){insert("o"z21);returntrue;}else訐(word=="<="){insert("<="z22);returntrue;}else訐(word==">"){insert(">",23);retur
5、ntrue;}else訐(word==">="){insert(">=",24);returntrue;}else訐(word=="="){insert("=",25);returntrue;}elseif(word=={insert(”;”,26);returntrue;}elseif(word=={insert("(",27);returntrue;}else訐(word==")"){insert(")",28);returntrue;}else訐(word=="#"){insert("#HzO);returntrue;}
6、else{returnfalse;}}/*判斷單個數(shù)字*/booljudge_single_number(charnum){廠〃寫錯了,以為數(shù)字在ASCALL碼中對應(yīng)0到9,結(jié)果程序無法識別數(shù)字,實際上0~9對應(yīng)的ASCALL碼值是49^57訐(num>=0&&num<=9){returntrue;}else{returnfalse;}*/訐(num>=49&&num<=57){returntrue;}else{returnfalse;}/*判斷單個字母*/booljudge_single」etter(charletter){
7、/*使用letter>=a&&letter<=z程序運行時無法判斷l(xiāng)etter是否為字母chara,z;if(letter>=a&&letter<=z){returntrue;}else{returnfalse;}*//*上述辦法無法實現(xiàn)該功能,故采用字母所対應(yīng)的ASCALL碼值作為判斷依據(jù)*/if(letter>=97&&letter<=122){returntrue;}else{returnfalse;}}/*數(shù)字判斷7booljudge_number(stringnumber){intcount=0;//計數(shù)charnu
8、m;while(number[count]!=、0‘){if(!judge_single_number(number[count])){returnfalse;}count++;}insert(numbecll);returntrue;}/*判斷標(biāo)示符booljudge」de