資源簡介
這是我自己寫的編譯原理詞法分析程序代碼,功能應該比較全,使用是在“test.txt"文本中輸入要分析的程序代碼段,程序運行后會,會把結果輸入不同的文本中,其中“Operator.txt"為運算符,“LimitSign.txt"為界限符...."error.txt"為輸入的錯誤。

代碼片段和文件信息
#include
#include
#include???????????????????//引入文件流頭文件
#include???????????????????//引入特殊字符頭文件
#include???????????????????//引入操縱符頭文件
using?namespace?std;
#define?MAXBUF?255?????????????????//定義的數組的大小參數
string?key[]={“auto““break““continue““char““case““const““class““default““double““do““define““extern“
??????????????“enum““else““friend““float““for““goto““include““int““if““long““namespace““protected“
??“public““private““register““return““static““short““signed““switch““sizeof““struct““typedef“
??“unsigned““union““using““void““while“};??//定義關鍵字,共40個
char?arr[MAXBUF];?//定義的緩沖數組
char?temp;
int?line=1;???????//定義的行
int?j=0;??????????//初始字符數組的下標
int?i=0;????????????//用于全文的變量
class?Lex
{
public:?
Lex(){};???????????????????????????????????????//Lex初始化構造函數
void?Initial();????????????????????????????????//初始化程序??
void?Key_Identifier(char?chifstream?&fin);????//處理關鍵字和標識符函數
void?Operator(char?chifstream?&fin);??????????//處理操作符函數
void?LimitSign(char?chifstream?&fin);?????????//處理界限符函數
void?Constant(char?chifstream?&fin);??????????//處理常數函數
void?Ch_Constant(char?chifstream?&fin);???????//字符常數函數
????????void?Handle();?????????????????????????????????//開始處理函數
void?Output(double?nint?jchar?arr[]);????????//輸出函數
private:
char?ch;
ifstream?fin;
ofstream?fout;
};
void?Lex::Output(double?nint?jchar?arr[])
{
char?*temp=new?char[j+1];
for(i=0;i temp[i]=arr[i];?//把字符數組賦值給動態的temp字符數組
temp[j]=‘\0‘;
switch((int)n)
{
case?1:fout.open(“KeyWord.txt“ios::app);??????????//輸入到關鍵字文件夾中
???break;
case?2:fout.open(“Identifier.txt“ios::app);???????//輸入到標識符文件夾中
???break;
case?3:fout.open(“Constant.txt“ios::app);?????????//輸入到常數文件夾中
???break;
case?4:fout.open(“Operator.txt“ios::app);?????????//輸入到運算符文件夾中
???break;
case?5:fout.open(“LimitSign.txt“ios::app);????????//輸入到界限符文件夾中
???break;
}
fout< fout.close();
free(temp);
}
void?Lex::Key_Identifier(char?chifstream?&fin)
{
bool?keyFlag=true;?//設置關鍵字的標識
int?m=0;???????????//用來控制關鍵字數組中的開頭字母的序號,例如a開頭m=0;b開頭m=1?
int?n=0;???????????/*用來控制關鍵字數組中以某個字母開頭的關鍵字數,例如設定的以c開頭的關鍵字共有
???????????????????????continuecharcaseconstclass.?5個,則n為5*/
int?k;?????????????????
bool?flag[40];?????????????//用于監控關鍵字的標記,因為共設定了40個關鍵字,則大小也為40
for(i=0;i<40;i++)
flag[i]=true;??????????//初始時都為true
switch(ch)
{
case?‘a‘:m=0;n=1;break;
case?‘b‘:m=1;n=1;break;
case?‘c‘:m=2;n=5;break;
case?‘d‘:m=7;n=4;break;
case?‘e‘:m=11;n=3;break;
case?‘f‘:m=14;n=3;break;
case?‘g‘:m=17;n=1;break;
case?‘i‘:m=18;n=3;break;
case?‘l‘:m=21;n=1;break;
case?‘n‘:m=22;n=1;break;
case?‘p‘:m=23;n=3;break;
case?‘r‘:m=26;n=2;break;
????????case?‘s‘:m=28;n=6;break;
case?‘t‘:m=34;n=1;break;
case?‘u‘:m=35;n=3;break;
case?‘v‘:m=38;n=1;break;
case?‘w‘:m=39;n=1;break;
default:keyFlag=false;break;??//
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????50??2009-05-25?22:39??詞法分析\Constant.txt
?????文件?????598079??2009-05-25?22:39??詞法分析\Debug\Lexical.exe
?????文件?????832768??2009-05-25?22:39??詞法分析\Debug\Lexical.ilk
?????文件?????338516??2009-05-25?22:39??詞法分析\Debug\Lexical.obj
?????文件????2157048??2009-04-18?22:55??詞法分析\Debug\Lexical.pch
?????文件????1156096??2009-05-25?22:39??詞法分析\Debug\Lexical.pdb
?????文件??????82944??2009-05-25?22:39??詞法分析\Debug\vc60.idb
?????文件?????118784??2009-05-25?22:39??詞法分析\Debug\vc60.pdb
?????文件??????????0??2009-05-25?22:39??詞法分析\error.txt
?????文件??????????0??2009-05-25?22:39??詞法分析\Identifier.txt
?????文件??????????0??2009-05-25?22:39??詞法分析\keyWord.txt
?????文件??????19768??2009-05-25?22:39??詞法分析\Lexical.cpp
?????文件???????3413??2009-05-25?22:39??詞法分析\Lexical.dsp
?????文件????????522??2009-05-25?22:39??詞法分析\Lexical.dsw
?????文件??????50176??2009-05-25?22:39??詞法分析\Lexical.ncb
?????文件??????48640??2009-05-25?22:39??詞法分析\Lexical.opt
?????文件????????751??2009-05-25?22:39??詞法分析\Lexical.plg
?????文件?????????46??2009-05-25?22:39??詞法分析\LimitSign.txt
?????文件??????????0??2009-05-25?22:39??詞法分析\Operator.txt
?????文件?????????13??2009-05-25?22:39??詞法分析\test.txt
?????文件???????4305??2009-04-18?20:33??詞法分析\詞法分析.dsp
?????文件????????524??2009-04-18?19:36??詞法分析\詞法分析.dsw
?????文件??????41984??2009-05-25?22:05??詞法分析\詞法分析.ncb
?????文件??????48640??2009-05-25?22:05??詞法分析\詞法分析.opt
?????目錄??????????0??2009-05-25?22:40??詞法分析\Debug
?????目錄??????????0??2009-05-25?22:40??詞法分析
-----------?---------??----------?-----??----
??????????????5503067????????????????????26
- 上一篇:湖南農大-編譯原理2010期末考試試卷
- 下一篇:pppoe獲取ipv6抓包分析
評論
共有 條評論