資源簡介
用c++寫的C-語法分析器源代碼,用c++寫的C-語法分析器源代碼.

代碼片段和文件信息
#include
#include
#include
#include
#include
#include
using?namespace?std;
struct?token//token結(jié)構(gòu)體
{
int?code;
int?num;
token?*next;
};
token?*token_head*token_tail;//token隊列
struct?number//number結(jié)構(gòu)體
{
int?num;
int?value;
number?*next;
};
number?*number_head*number_tail;//number隊列
struct?str//string結(jié)構(gòu)體
{
int?num;
string??word;
str?*next;
};
str?*string_head*string_tail;//string隊列
struct?ivan//表達(dá)式結(jié)構(gòu)體
{
char?left;
string?right;
int?len;
};
ivan?css[20];//20個表達(dá)式
struct?pank//action表結(jié)構(gòu)體
{
char?sr;
int?state;
};
pank?action[46][18];//action表
int?go_to[46][11];//go_to表
struct?ike//分析棧結(jié)構(gòu)體,雙鏈
{
ike?*pre;
int?num;
int?word;
ike?*next;
};
ike?*stack_head*stack_tail;//分析棧首尾指針
void?scan();//按字符讀取源文件
int?judge(char?ch);//判斷輸入字符的類型
void?out1(char?ch);//寫入token.txt
void?out2(char?chstring?word);//寫入number.txt
void?out3(char?chstring?word);//寫入string.txt
void?input1(token?*temp);//插入結(jié)點到隊列token
void?input2(number?*temp);//插入結(jié)點到隊列number
void?input3(str?*temp);//插入結(jié)點到隊列string
void?output();//輸出三個隊列的內(nèi)容
void?outfile();//輸出三個隊列的內(nèi)容到相應(yīng)文件中
void?yufa_initialize();//初始化語法分析數(shù)據(jù)結(jié)構(gòu)
int?yufa_main(int?a);//語法分析主體部分
int?ID1(int?a);//給輸入字符編號,轉(zhuǎn)化成action表列編號
string?ID10(int?i);//給輸入字符反編號
int?ID2(char?ch);//給非終結(jié)狀態(tài)編號,轉(zhuǎn)化成go_to表列編號
int?ID20(char?ch);//給非終結(jié)狀態(tài)編號
char?ID21(int?j);//給非終結(jié)狀態(tài)反編號
void?add(ike?*temp);//給ike分析棧鏈表增加一個結(jié)點
void?del();//給ike分析棧鏈表刪除一個結(jié)點
FILE?*fp;//文件
int?wordcount;//標(biāo)志符計數(shù)
int?numcount;//整型常數(shù)計數(shù)
int?err;//標(biāo)志詞法分析結(jié)果正確或錯誤
int?nl;//讀取行數(shù)
void?main()
{
token_head=new?token;
token_head->next=NULL;
token_tail=new?token;
token_tail->next=NULL;
number_head=new?number;
number_head->next=NULL;
number_tail=new?number;
number_tail->next=NULL;
string_head=new?str;
string_head->next=NULL;
string_tail=new?str;
string_tail->next=NULL;//初始化三個隊列的首尾指針
wordcount=0;//初始化字符計數(shù)器
numcount=0;//初始化常數(shù)計數(shù)器
err=0;//初始化詞法分析錯誤標(biāo)志
nl=1;//初始化讀取行數(shù)
scan();
if(err==0)
{
char?m;
cout<<“詞法分析正確完成!“< cin>>m;
output();
if(m==‘y‘)
{
cout<<“結(jié)果同時保存在token.txt、number.txt和sting.txt三個文件中,請打開查看“< outfile();
}
}
yufa_initialize();//初始化語法分析數(shù)據(jù)結(jié)構(gòu)
token?*temp;
temp=new?token;
temp=token_head->next;
int?pq;
p=0;
q=0;
cout< while(temp!=NULL)
{
int?w;
w=ID1(temp->code);
p=yufa_main(w);
if(p==1)?break;
if(p==0)
temp=temp->next;
if(temp==NULL)?q=1;
}//語法分析
if(q==1)
while(1)
{
p=yufa_main(17);
if(p==3)?break;
}//最后輸入$來完成語法分析
cout< system(“pause“);
}
void?scan()
{
char?ch;
string?word;
char?document[50];
int?flag=0;
cout<<“請輸入源文件路徑及名稱:“;
cin>>document;
cout< if((fp=fopen(document“rt“))==NULL)
{
err=1;
cout<<“無法找到該文件!“< return;
}
while(!feof(fp))
{
word=““;
ch=fgetc(fp);
flag=judge(ch);
if(flag==1)
out1
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????113??2005-06-13?17:45??語法分析器\ivan.txt
?????文件??????19761??2005-06-13?17:50??語法分析器\語法分析器.cpp
?????目錄??????????0??2005-06-13?17:40??語法分析器
-----------?---------??----------?-----??----
????????????????19874????????????????????3
評論
共有 條評論