資源簡介
文法為:
0:S->E
1:E->E+E
2:E->E*E
3:E->(E)
4:E->id
運行時只需輸入待驗證的句子即可
如要實現其他SLR(1)文法的識別,只需修改頭文件和錯誤處理函數即可
作者:WMD
日期:2018-6-1

代碼片段和文件信息
/*
文法為:
0:S->E
1:E->E+E
2:E->E*E
3:E->(E)
4:E->id
運行時只需輸入待驗證的句子即可
如要實現其他SLR(1)文法的識別,只需修改頭文件和錯誤處理函數即可
作者:WMD
日期:2018-6-1
*/
#include?“slr1.h“
linkList?L;????????????????//帶頭結點的雙向鏈表
linkList?tail;?????????????//記錄鏈表的尾
int?ip=0;??????????????????//訪問輸入串元素
char?thetext[20];??????????//用于接收用戶輸入
char?space[20];????????????//用于控制輸出縮進
int text_len=0;
int?status_num=0; ???//棧中元素個數(狀態棧和符號棧中元素個數相同)
int?status_top=0;??????????//記錄狀態棧棧頂
/******************************************************************************************/
int?main()?????????????????????//主函數
{
menu();
return?0;
}
/******************************************************************************************/
void?menu()???????????????????//菜單函數
{
initialList(L);
while(1)
{
cout<<“請輸入要驗證的句子輸入‘0‘退出:“< cin>>thetext;
if(strcmp(thetext“0“)==0)
return;
cout<<“輸入的句子為:?“<
ip=0;
while(thetext[ip]!=‘\0‘)?????//在句子末尾加入#
ip++;
thetext[ip]=‘#‘;
thetext[ip+1]=‘\0‘;
ip=0;
text_len=strlen(thetext);
for(ip=0;ip<(20-text_len);ip++)
{
space[ip]=‘?‘;
}
space[ip]=‘\0‘;
ip=0;
initial();????????????//對棧進行初始化,并將初始狀態入棧
push(0‘#‘);
tail=L->next;
cout<<“狀態棧“<<“??????????????“<<“符號棧“<<“??????????????“<<“輸入串“<<“????????????????????????“<<“動作說明“< cout<<“------------------------------------------------------------------------------------------“<
analyze();
system(“pause“);
system(“cls“);
}
}
/******************************************************************************************/
int?initialList(linkList?&L)??//棧初始化函數
{
L=new?LNode;
if(!L)
return?ERROR;
L->next=NULL;
L->prev=NULL;
return?OK;
}
/******************************************************************************************/
void?initial()????????????????//對棧進行清空
{
linkList?pq;
p=L;
while(p->next!=NULL)
{
q=p->next;
p->next=q->next;
delete?q;
}
status_num=0;
}
/******************************************************************************************/
void?push(int?statuschar?symbol) ??//入棧
{
linkList?pointer=new?LNode;
pointer->status=status;
pointer->symbol=symbol;
pointer->next=L->next;
L->next=pointer;
pointer->prev=L;
if(pointer->next!=NULL)
pointer->next->prev=pointer;
status_num++;
}
/******************************************************************************************/
void?pop()????????????????????//出棧
{
linkList?pointer;
pointer=L->next;
L->next=pointer->next;
pointer->next->prev=L;
free(pointer);
status_num--;
}
/******************************************************************************************/
void?analyze()????????????????//分析
{
int?result_tab=0;?????????????????//記錄查表結果
int?location_tab=0;???????????????//記錄在終結符表中的位置
int?status_new=0;?????????????????//記錄查詢goto后獲取的狀態?
int?i=0fre=0;????????????????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????553014??2018-06-01?15:05??SLR1\Debug\SLR1.exe
?????文件?????797952??2018-06-01?15:05??SLR1\Debug\SLR1.ilk
?????文件?????199582??2018-06-01?15:05??SLR1\Debug\slr1.obj
?????文件????2181788??2018-06-01?15:05??SLR1\Debug\SLR1.pch
?????文件????1115136??2018-06-01?15:05??SLR1\Debug\SLR1.pdb
?????文件??????74752??2018-06-04?13:52??SLR1\Debug\vc60.idb
?????文件?????110592??2018-06-01?15:05??SLR1\Debug\vc60.pdb
?????文件????????190??2018-06-10?14:49??SLR1\readme.txt
?????文件???????7624??2018-06-10?14:50??SLR1\slr1.cpp
?????文件???????4319??2018-06-01?15:04??SLR1\SLR1.dsp
?????文件????????516??2018-06-01?15:02??SLR1\SLR1.dsw
?????文件???????1332??2018-06-01?15:03??SLR1\slr1.h
?????文件??????50176??2018-06-10?14:50??SLR1\SLR1.ncb
?????文件??????48640??2018-06-10?14:50??SLR1\SLR1.opt
?????文件????????873??2018-06-01?15:05??SLR1\SLR1.plg
?????目錄??????????0??2018-06-01?15:05??SLR1\Debug
?????目錄??????????0??2018-06-10?14:50??SLR1
-----------?---------??----------?-----??----
??????????????5146486????????????????????17
評論
共有 條評論