1431递归下降语法分析程序设计

时间:2021-02-25 17:06:28

#include<stdio.h>
char curr;
char LL1[1000];
int N=0;

void scaner();

int digital(){ //panduanshifouweishuzi

if(curr >= '0' && curr <= '9')
{
return 1;
}
else {
return 0;
}

}

int english(){
if(curr >= 'a' && curr<='z' || curr >= 'A' && curr <= 'Z')
{
return 1;
}
else
return 0;

}
int yuansuanf(){
if(curr=='+'||curr=='-'||curr=='*'||curr=='/')
{
return 1;
}

else{
return 0;
}

}
int end(){
if(curr!='#'){
return 1;
}
else
return 0;
}


int check(){

if(digital()==1){
scaner();
if(end()==1){
if(yuansuanf()==1){

return 1;
}
else if(yuansuanf()==0){
return 0;

}
}
else if(end()==0){
return 1;
}

}


return 0;


}

void checking(){
int number;
qq: scaner();
if(digital()==1){
while(curr!='#'){
number=check();
if(number==1){
goto qq;
}
else if(number==0){

break;
}
}
}


if(number==1){

printf("无错误");
}
else
printf("%c错误",curr);


}

 

void main()
{ char ch;
int i=0;
printf("请输入需要分析的语法:(#结束)\n");

do{
scanf("%c",&ch);
LL1[i] = ch;
i++;
}while(ch != '#');
scaner();
checking();

}

void scaner(){ //用于读取源函数的下一个字符

N++;
if(LL1[N] == ' '){
N++;
}else
{
curr = LL1[N];
}
}