LL1语法分析程序实验报告Word文件下载.docx
《LL1语法分析程序实验报告Word文件下载.docx》由会员分享,可在线阅读,更多相关《LL1语法分析程序实验报告Word文件下载.docx(10页珍藏版)》请在冰豆网上搜索。
#include"
stdio.h"
malloc.h"
conio.h"
structLchar{
charchar_ch;
structLchar*next;
}Lchar,*p,*h,*temp,*top,*base;
charcurchar;
charcurtocmp;
intright;
inttable[5][8]={{1,0,0,1,0,0},
{0,1,0,0,1,1},
{1,0,0,1,0,0},
{0,1,1,0,1,1},
{1,0,0,1,0,0}};
inti,j;
voidpush(charpchar)
{
temp=(structLchar*)malloc(sizeof(Lchar));
temp->
char_ch=pchar;
next=top;
top=temp;
}
voidpop(void)
curtocmp=top->
char_ch;
if(top->
char_ch!
='
#'
)
top=top->
next;
voiddoforpush(intt)
switch(t)
case0:
push('
A'
);
T'
break;
case5:
case11:
+'
case20:
B'
F'
case23:
case32:
*'
case40:
i'
case43:
)'
E'
('
voidchangchartoint()
switch(curtocmp)
case'
:
i=1;
i=3;
i=0;
i=2;
i=4;
switch(curchar)
j=0;
j=1;
j=2;
j=3;
j=4;
j=5;
voiddosome(void)
intt;
for(;
;
pop();
curchar=h->
printf("
\n%c\t%c"
curchar,curtocmp);
if(curtocmp=='
&
&
curchar=='
||curtocmp=='
if(curtocmp!
changchartoint();
if(table[i][j])
t=10*i+j;
doforpush(t);
continue;
else
right=0;
=curchar)
h=h->
voidmain(void)
charch;
cout<
<
"
*文件名称:
语法分析"
endl;
"
/*程序相关说明*/"
cout<
---------------------------------------------------------------------"
-/*A=E’B=T’*/"
-*目的:
对输入LL
(1)文法字符串,本程序能自动判断所给字符串是-"
-*否为所给文法的句子,并能给出分析过程。
-"
-*-------------------------------------------------------------------"
表达式文法为:
E->
E+T|T"
T->
T*F|F"
F->
(E)|i"
请在下行输入要分析的串(#号结束):
right=1;
base=(structLchar*)malloc(sizeof(Lchar));
base->
next=NULL;
char_ch='
next=base;
h=(structLchar*)malloc(sizeof(Lchar));
h->
p=h;
do{
ch=getch();
putch(ch);
if(ch=='
||ch=='
-'
/'
char_ch=ch;
next=temp;
temp=p->
\nInputawrongchar!
Inputagain:
\n"
if(temp!
=NULL)
%c"
temp->
char_ch);
temp=temp->
}while(ch!
p=p->
h=p;
dosome();
if(right)
\n成功!
\n错误!
getch();
截图: