資源簡介
檢查要求:
a) 啟動程序后,先輸出作者姓名、班級、學(xué)號(可用漢語、英語或拼音);
b) 請求輸入測試程序名,鍵入程序名后自動開始詞法分析并輸出結(jié)果;
c) 輸出結(jié)果為單詞的二元式序列(樣式見樣板輸出1和2);
d) 要求能發(fā)現(xiàn)下列詞法錯誤和指出錯誤性質(zhì)和位置:
非法字符,即不是SAMPLE字符集的符號;
字符常數(shù)缺右邊的單引號(字符常數(shù)要求左、右邊用單引號界定,不能跨行);
注釋部分缺右邊的界符*/(注釋要求左右邊分別用/*和*/界定,不能跨行)。

代碼片段和文件信息
#include
#include
#include
#include
#include
using?namespace?std;
struct?node{???????????
string?word;
string?type;
};
void?display();????//輸出本人的信息
void?file();???????//輸出分析結(jié)果
void?main()
{
????display();
file();
int?s;???
cout<<“請輸入數(shù)字:1.繼續(xù)??0.退出“< ????cin>>s;
????while(s==1){
if(s==1){?
file();
cout<<“請輸入數(shù)字:1.繼續(xù)??0.退出“< ????cin>>s;
}
if(s==0){
??exit(0);
}
}
}
void?file()
{
node?A[35];??//保留字
node?B[22];???//39-60是單界符和雙界符
A[0].word=“and“; A[0].type=“1“;
A[1].word=“array“; A[1].type=“2“;
A[2].word=“begin“; A[2].type=“3“;
A[3].word=“bool“; A[3].type=“4“;
A[4].word=“call“; A[4].type=“5“;
A[5].word=“case“; A[5].type=“6“;
A[6].word=“char“; A[6].type=“7“;
A[7].word=“constant“; A[7].type=“8“;
A[8].word=“dim“; A[8].type=“9“;
A[9].word=“do“; A[9].type=“10“;
A[10].word=“else“; A[10].type=“11“;
A[11].word=“end“; A[11].type=“12“;
A[12].word=“false“; A[12].type=“13“;
A[13].word=“for“; A[13].type=“14“;
A[14].word=“if“; A[14].type=“15“;
A[15].word=“input“; A[15].type=“16“;
A[16].word=“integer“; A[16].type=“17“;
A[17].word=“not“; A[17].type=“18“;
A[18].word=“of“; A[18].type=“19“;
A[19].word=“or“; A[19].type=“20“;
A[20].word=“output“; A[20].type=“21“;
A[21].word=“procedure“; A[21].type=“22“;
A[22].word=“program“; A[22].type=“23“;
A[23].word=“read“; A[23].type=“24“;
A[24].word=“real“; A[24].type=“25“;
A[25].word=“repeat“; A[25].type=“26“;
A[26].word=“set“; A[26].type=“27“;
A[27].word=“stop“; A[27].type=“28“;
A[28].word=“then“; A[28].type=“29“;
A[29].word=“to“; A[29].type=“30“;
A[30].word=“true“; A[30].type=“31“;
A[31].word=“until“; A[31].type=“32“;
A[32].word=“var“; A[32].type=“33“;
A[33].word=“while“; A[33].type=“34“;
A[34].word=“write“; A[34].type=“35“;
B[0].word=“(“; B[0].type=“39“;
B[1].word=“)“; B[1].type=“40“;
B[2].word=“*“; B[2].type=“41“;
B[3].word=“*/“; B[3].type=“42“;
B[4].word=“+“; B[4].type=“43“;
B[5].word=““; B[5].type=“44“;
B[6].word=“-“; B[6].type=“45“;
B[7].word=“.“; B[7].type=“46“;
B[8].word=“..“; B[8].type=“47“;
B[9].word=“/“; B[9].type=“48“;
B[10].word=“/*“; B[10].type=“49“;
B[11].word=“:“; B[11].type=“50“;
B[12].word=“:=“; B[12].type=“51“;
B[13].word=“;“; B[13].type=“52“;
B[14].word=“<“; B[14].type=“53“;
B[15].word=“<=“; B[15].type=“54“;
B[16].word=“<>“; B[16].type=“55“;
B[17].word=“=“; B[17].type=“56“;
B[18].word=“>“; B[18].type=“57“;
B[19].word=“>=“; B[19].type=“58“;
B[20].word=“[“; B[20].type=“59“;
B[21].word=“]“; B[21].type=“60“;
char?ch[100];
ifstream?infile;??????????????//定義一個輸入流對象
cout<<“請輸入需分析的文件路徑及名稱:“< cin>>ch;
infile.open(chios::in);??//打開文件
if(!infile)
{
cout<<“文件不存在或無法打開“< }
cout<
char?buffer[50];??????????????//比較字符串
char*?s=new?char[100]; ??//定義輸入的字符串
int?line=0;
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????74752??2013-12-04?22:48??詞法分析器\Debug\vc60.idb
?????文件?????118784??2013-12-04?18:23??詞法分析器\Debug\vc60.pdb
?????文件?????593983??2013-12-04?18:23??詞法分析器\Debug\詞法分析器.exe
?????文件?????840648??2013-12-04?18:23??詞法分析器\Debug\詞法分析器.ilk
?????文件?????392126??2013-12-04?18:23??詞法分析器\Debug\詞法分析器.obj
?????文件????2177588??2013-12-04?17:21??詞法分析器\Debug\詞法分析器.pch
?????文件????1147904??2013-12-04?18:23??詞法分析器\Debug\詞法分析器.pdb
?????文件????????301??2013-12-02?23:26??詞法分析器\text1.txt
?????文件????????126??2013-12-03?13:27??詞法分析器\text2.txt
?????文件????????145??2013-12-03?13:27??詞法分析器\text3.txt
?????文件???????8398??2013-12-04?18:23??詞法分析器\詞法分析器.cpp
?????文件???????3451??2013-12-04?16:53??詞法分析器\詞法分析器.dsp
?????文件????????528??2013-12-05?00:29??詞法分析器\詞法分析器.dsw
?????文件??????41984??2013-12-05?00:29??詞法分析器\詞法分析器.ncb
?????文件??????48640??2013-12-05?00:29??詞法分析器\詞法分析器.opt
?????文件????????774??2013-12-04?18:23??詞法分析器\詞法分析器.plg
?????目錄??????????0??2013-12-04?18:23??詞法分析器\Debug
?????目錄??????????0??2013-12-05?00:29??詞法分析器
-----------?---------??----------?-----??----
??????????????5450132????????????????????18
評論
共有 條評論