資源簡介
LR1分析器代碼實現

代碼片段和文件信息
/*-------------------LR(1)?grammar?-------------------------------------
S->E
E->E*E
E->E+E
E->(E)
E->i
??
----------------------------LR(1)?parsing?table-------------------------------------------
state * + ( ) i $ E
0 ? ? s2 ? s3 1
1 s4 s5 r0
2 s7 s8 6
3 r4 r4 r4
4 s2 s3 9
5 r2 r2 r2 10
6 s12 s13 ? s11
7 s7 s8 14
8 r4 r4 r4
9 s4 r1 r1
10 s4 s5 r2
11 r3 r3 r3
12 s7 s8 15
13 s7 s8 16
14 s12 s13 s17
15 s12 r1 r1
16 r2 s13 r2
17 r3 r3 r3
---------------------------------------------------------------------------------------*/
#include?“CPT.h“
//----------------------Global?Declarition---------------------------------
#define?SIZE?20
#define?sSIZE?18?????????//There?are?sSIZE?status
#define?aSIZE?6??????????//There?will?ecounter?aSIZE?symbol
#define?gSIZE?1??????????//May?be?goto?next?gSIZE?status
#define?geSIZE?5?????????//There?are?geSIZE?generate?expression
#define?MAXSIZE?3
//---------------------Finish?defining?struct-------------------------------------
typedef?struct?Ge
{
????????char?head;???????//Leftpart?of?Generate?expression
????????char?gen[4];???????//Rightpart?of?Generate?expression?
}Generate;//--------------------------------Generate?expression?base?datastruct
typedef?struct?A
{
?int?st[aSIZE];???????//aSIZE?status?when?encountering?terminated?symbol
?int?re[aSIZE];???????//Using?reduce?
}Action;//----------------------------------Action?table?base?datastruct
typedef?struct?G
{
?char?head[gSIZE];??????//Nonterminated?symbol?:‘E‘
?int?gt[gSIZE];???????//Mark?the?next?status
}GOTO;//------------------------------------GOTO?table?base?datastruct
int?status[SIZE];??????????????????????????//stack?of?status?????????????????????????????????
int??sta_Index;???????????//top?of?stack?of?status??
char?symbol[SIZE];??????????????????????????//stack?of?symbol
int??sym_Index;????????//Current?index?of?symbol?stack
char?expression[SIZE];??????????????????????//Inputed?expression
int??exp_Index;?????????????????????????????//index?of?inputed?expression
int??exp_top;????????//top?of?expression?that?inputed
int??step;?????????//accumulated?steps
int??IsAccept?=?0;???????//Initlize?accept?flag?to?0
Generate?gene[geSIZE?+1];
Action?act[sSIZE];
GOTO?go[sSIZE];
fstream?outFile;
//------------------------------------------------------------------------
void?GOTOTable(int?sta?char?symb);
void?Inputexpression()
{
?????char?ch;
?????printf(“請輸入分析串“);
?printf(“[包括:{?+?*?(?)i?#?}以‘#‘結束]:\n“);
?????exp_Index?=?0;
??do????????????????????????????????
??{
?????scanf(“%c“&ch);
?if?((ch!=‘i‘)?&&(ch!=‘+‘)?&&(ch!=‘*‘)&&(ch!=‘#‘)&&(ch!=‘(‘)&&(ch!=‘)‘))
?????{
?????printf(“Illegal?Word?inside...Press?any?key?to?EXIT!\n“);
?????getchar();
exit(0);
?????}
?????expression[exp_Index++]=ch;
??}while(ch!=‘#‘);??
}
void?PrintStatus()
{
?long?i?=?0;
?for(i?=?0;?i?<=?sta_Index;?i++)
?{
??p
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2012-01-08?01:21??LR1?analyser\
?????文件????????1049??2011-12-29?20:58??LR1?analyser\CPT.CPP
?????文件?????????325??2011-12-29?20:58??LR1?analyser\CPT.H
?????目錄???????????0??2012-01-08?01:21??LR1?analyser\Debug\
?????文件????????1346??2011-12-29?21:00??LR1?analyser\Debug\cl.command.1.tlog
?????文件???????18246??2011-12-29?21:00??LR1?analyser\Debug\CL.read.1.tlog
?????文件?????????902??2011-12-29?21:00??LR1?analyser\Debug\CL.write.1.tlog
?????文件??????381150??2011-12-29?21:00??LR1?analyser\Debug\CPT.obj
?????文件???????????2??2011-12-29?21:00??LR1?analyser\Debug\li
?????文件???????????2??2011-12-29?21:00??LR1?analyser\Debug\li
?????文件???????????2??2011-12-29?21:00??LR1?analyser\Debug\li
?????文件???????????2??2011-12-29?21:00??LR1?analyser\Debug\li
?????文件???????????2??2011-12-29?21:00??LR1?analyser\Debug\li
?????文件???????????2??2011-12-29?21:00??LR1?analyser\Debug\li
?????文件????????1614??2011-12-29?21:00??LR1?analyser\Debug\li
?????文件????????3356??2011-12-29?21:00??LR1?analyser\Debug\li
?????文件?????????876??2011-12-29?21:00??LR1?analyser\Debug\li
?????文件??????644608??2011-12-29?21:00??LR1?analyser\Debug\LR1?analyser.exe
?????文件?????????406??2011-12-29?21:00??LR1?analyser\Debug\LR1?analyser.exe.em
?????文件?????????472??2011-12-29?21:00??LR1?analyser\Debug\LR1?analyser.exe.em
?????文件?????????381??2011-12-29?21:00??LR1?analyser\Debug\LR1?analyser.exe.intermediate.manifest
?????文件?????1628736??2011-12-29?21:00??LR1?analyser\Debug\LR1?analyser.ilk
?????文件??????????61??2011-12-29?21:00??LR1?analyser\Debug\LR1?analyser.lastbuildstate
?????文件????????3917??2011-12-29?21:00??LR1?analyser\Debug\LR1?analyser.log
?????文件??????502472??2011-12-29?21:00??LR1?analyser\Debug\LR1?analyser.obj
?????文件?????2681856??2011-12-29?21:00??LR1?analyser\Debug\LR1?analyser.pdb
?????文件?????????220??2011-12-29?20:54??LR1?analyser\Debug\LR1?analyser_manifest.rc
?????文件?????????412??2011-12-29?21:00??LR1?analyser\Debug\mt.command.1.tlog
?????文件?????????310??2011-12-29?21:00??LR1?analyser\Debug\mt.read.1.tlog
?????文件?????????310??2011-12-29?21:00??LR1?analyser\Debug\mt.write.1.tlog
?????文件?????????566??2011-12-29?21:00??LR1?analyser\Debug\rc.command.1.tlog
............此處省略24個文件信息
評論
共有 條評論