#include<stdio.h>
#include<string.h>
lrparser();
yucu();
statement();
expression();
term();
factor();
char prog[80],token[9];
char ch ;
int syn,p,m = 0,n,sum,kk = 0;
char* rwtab[6] = {"function","if","then","while","do","endfunc"};
void scaner()
{
for(n = 0;n < 9;n++)
token[n] = NULL;
while(ch == ' ' || ch=='\n')
ch = prog[p++];
m = 0;
if((ch <= 'z' && ch >= 'a') || (ch <= 'Z' && ch >= 'A'))
{
while((ch <= 'z' && ch >= 'a') || (ch <= 'Z' && ch >= 'A')||(ch <='9' && ch >='0'))
{
token[m++]=ch;
ch = prog[p++];
}
token[m++] = '\0' ;
syn = 10;
for(n = 0;n < 6;n++)
if(strcmp(token,rwtab[n]) == 0)
{
syn = n+1;
break;
}
}
else
if(ch<='9'&&ch>='0')
{
sum = 0;
while(ch<='9'&&ch>='0')
{
sum= sum*10+ch-'0';
ch = prog[p++];
}
syn=11;
}
else
{
switch(ch)
{
case'<': m=0;token[m++]=ch;
ch=prog[++p];
if(ch=='=')
{
syn=22;
token[m+1]=ch;
}
else
{
syn =20;ch=prog[--p];
}
break;
case'>': m=0;token[m++]=ch;
ch=prog[++p];
if(ch== '=')
{
syn=24;
token[m++]=ch;
}
else
{
syn=23;
ch=prog[--p];
}
break;
case'=': m=0;token[m++]=ch;
ch= prog[++p];
if(ch=='=')
{
syn = 25;
token[m++]=ch;
}
else
{
syn =18;
ch =prog[--p];
}
break;
case'!': m=0;token[m++]=ch;
ch=prog[++p];
if(ch=='=')
{
syn =22;
token[m++]=ch;
}
else
syn = -1;
break;
case'+':syn =13;token[0]=ch;break;
case'-':syn =14;token[0]=ch;break;
case'*':syn =15;token[0]=ch;break;
case'/':syn =16;token[0]=ch;break;
case';':syn =26;token[0]=ch;break;
case'(':syn =27;token[0]=ch;break;
case')':syn =28;token[0]=ch;break;
case'#':syn =0;token[0]=ch;break;
default:syn = -1;
}
ch = prog[p++];//获取下一个字符
}
}
lrparser()//判断输入语法是否正确
{
if(syn == 1)//function,是否正常开始
{
scaner();//判断下一个单词(调用函数判断中间内容)
yucu();
if(syn == 6)//endfunc,是否正常结束,且,kk记录中间内容语法是否有错
{
scaner();
if(syn == 0 && kk == 0)
printf("success\n");
}
else
{
if(kk != 1)
{
printf("error!need'endfunc'\n");
kk = 1;
}
}
}
else
{
printf("error!need'function'\n");
kk = 1;
}
}
yucu()
{
statement();
while(syn == 26)//;语句结束
{
scaner();//获取下一句首字母
if(syn!=6)//单词不为endfunc
statement();
}
return;
}
statement()
{
if(syn==10)//字符串
{
scaner();//获取下一个单词首字母
if(syn==18)//=
{
scaner();//获取下一个单词首字母
expression();
}
else
{
printf("error! evaluate tag error\n");
kk=1;
}
}
else
{
printf("error! the statement error!\n");
kk =1;
}
}
expression()
{
term();
while(syn ==13||syn ==14)//+,-
{
scaner();
term();
}
return;
}
term()
{
factor();
while(syn == 15 || syn == 16)//*,/
{
scaner();
factor();
}
return;
}
factor()
{
if(syn == 10 || syn == 11)//字符串或者数字
{
scaner();//获取下一个单词首字母
}
else
{
if(syn == 27)//(
{
scaner();
expression();
if(syn == 28)//)
{
scaner();
}
else
{
printf("error!need another')'\n");
kk = 1;
}
}
else
{
printf("error!expression error!\n");
}
}
}
main()
{
FILE *fp;
p=0;
//if((fp=fopen("E:\\编译原理期末代码\\261.txt","r"))==NULL)
if((fp=fopen("E:\\编译原理期末代码\\262.txt","r"))==NULL)
{
printf("\n 文件打开失败:\n");
return 0;
}
do
{
ch=fgetc(fp);
prog[p++]=ch;
}while(ch!='#');//将文件内容赋值给prog数组
p=0;
ch=prog[p++];//获取第一个字符,得到syn
scaner();//判断该单词属于什么类型(第一个)eg function
lrparser();
}
#include<string.h>
lrparser();
yucu();
statement();
expression();
term();
factor();
char prog[80],token[9];
char ch ;
int syn,p,m = 0,n,sum,kk = 0;
char* rwtab[6] = {"function","if","then","while","do","endfunc"};
void scaner()
{
for(n = 0;n < 9;n++)
token[n] = NULL;
while(ch == ' ' || ch=='\n')
ch = prog[p++];
m = 0;
if((ch <= 'z' && ch >= 'a') || (ch <= 'Z' && ch >= 'A'))
{
while((ch <= 'z' && ch >= 'a') || (ch <= 'Z' && ch >= 'A')||(ch <='9' && ch >='0'))
{
token[m++]=ch;
ch = prog[p++];
}
token[m++] = '\0' ;
syn = 10;
for(n = 0;n < 6;n++)
if(strcmp(token,rwtab[n]) == 0)
{
syn = n+1;
break;
}
}
else
if(ch<='9'&&ch>='0')
{
sum = 0;
while(ch<='9'&&ch>='0')
{
sum= sum*10+ch-'0';
ch = prog[p++];
}
syn=11;
}
else
{
switch(ch)
{
case'<': m=0;token[m++]=ch;
ch=prog[++p];
if(ch=='=')
{
syn=22;
token[m+1]=ch;
}
else
{
syn =20;ch=prog[--p];
}
break;
case'>': m=0;token[m++]=ch;
ch=prog[++p];
if(ch== '=')
{
syn=24;
token[m++]=ch;
}
else
{
syn=23;
ch=prog[--p];
}
break;
case'=': m=0;token[m++]=ch;
ch= prog[++p];
if(ch=='=')
{
syn = 25;
token[m++]=ch;
}
else
{
syn =18;
ch =prog[--p];
}
break;
case'!': m=0;token[m++]=ch;
ch=prog[++p];
if(ch=='=')
{
syn =22;
token[m++]=ch;
}
else
syn = -1;
break;
case'+':syn =13;token[0]=ch;break;
case'-':syn =14;token[0]=ch;break;
case'*':syn =15;token[0]=ch;break;
case'/':syn =16;token[0]=ch;break;
case';':syn =26;token[0]=ch;break;
case'(':syn =27;token[0]=ch;break;
case')':syn =28;token[0]=ch;break;
case'#':syn =0;token[0]=ch;break;
default:syn = -1;
}
ch = prog[p++];//获取下一个字符
}
}
lrparser()//判断输入语法是否正确
{
if(syn == 1)//function,是否正常开始
{
scaner();//判断下一个单词(调用函数判断中间内容)
yucu();
if(syn == 6)//endfunc,是否正常结束,且,kk记录中间内容语法是否有错
{
scaner();
if(syn == 0 && kk == 0)
printf("success\n");
}
else
{
if(kk != 1)
{
printf("error!need'endfunc'\n");
kk = 1;
}
}
}
else
{
printf("error!need'function'\n");
kk = 1;
}
}
yucu()
{
statement();
while(syn == 26)//;语句结束
{
scaner();//获取下一句首字母
if(syn!=6)//单词不为endfunc
statement();
}
return;
}
statement()
{
if(syn==10)//字符串
{
scaner();//获取下一个单词首字母
if(syn==18)//=
{
scaner();//获取下一个单词首字母
expression();
}
else
{
printf("error! evaluate tag error\n");
kk=1;
}
}
else
{
printf("error! the statement error!\n");
kk =1;
}
}
expression()
{
term();
while(syn ==13||syn ==14)//+,-
{
scaner();
term();
}
return;
}
term()
{
factor();
while(syn == 15 || syn == 16)//*,/
{
scaner();
factor();
}
return;
}
factor()
{
if(syn == 10 || syn == 11)//字符串或者数字
{
scaner();//获取下一个单词首字母
}
else
{
if(syn == 27)//(
{
scaner();
expression();
if(syn == 28)//)
{
scaner();
}
else
{
printf("error!need another')'\n");
kk = 1;
}
}
else
{
printf("error!expression error!\n");
}
}
}
main()
{
FILE *fp;
p=0;
//if((fp=fopen("E:\\编译原理期末代码\\261.txt","r"))==NULL)
if((fp=fopen("E:\\编译原理期末代码\\262.txt","r"))==NULL)
{
printf("\n 文件打开失败:\n");
return 0;
}
do
{
ch=fgetc(fp);
prog[p++]=ch;
}while(ch!='#');//将文件内容赋值给prog数组
p=0;
ch=prog[p++];//获取第一个字符,得到syn
scaner();//判断该单词属于什么类型(第一个)eg function
lrparser();
}