递归下降语法分析器源代码
#i nclude "stdio.h"
#i nclude "malloc.h"
void e();
void f();
void t();
void t1();
void e1();
struct Lchar{
char char_ch;
struct Lchar *next;
}Lchar,*p,*h,*temp;
char ch;
int right;
void e(void)
{
t();
e1();
}
void t(void)
{
f();
t1();
}
void t1(void)
{
if(h->char_ch=='*')
{
h=h->next;
f();
t1();
}
else
if(h->char_ch!='#'&&h->char_ch!=')'&&h->char_ch!='+')
right=0;
}
void e1(void)
{
if(h->char_ch=='+')
{
h=h->next;
t();
e1();
}
else
if(h->char_ch=='#'||h->char_ch==')')
return;
else
right=0;
}
void f()
{
if(h->char_ch=='i')
h=h->next;
else
if(h->char_ch=='(')
{
h=h->next;
e();
if(h->char_ch==')')
h=h->next;
else
right=0;
}
else
right=0;
}
void main(void)
{
right=1;
h=malloc(sizeof(Lchar));
h->next=NULL;
p=h;
do{
ch=getch();
putch(ch);
if(ch=='i'||ch=='+'||ch=='*'||ch=='('||ch==')'||ch=='#')
{
temp=malloc(sizeof(Lchar));
temp->next=NULL;
temp->char_ch=ch;
h->next=temp;
h=h->next;
}
else
{
temp=p->next;
printf("\nInput a wrong char!Input again:\n");
for(;;)
{
if (temp!=NULL)
printf("%c",temp->char_ch);
else
break;
temp=temp->next;
}
}
}while(ch!='#');
p=p->next;
h=p;
e();
if(h->char_ch=='#'&&right)
printf("\nOK!\n");
else
printf("\nError!\n");
getchar();
}