#include<stdio.h>
char curr;
char LL1[1000];
int N=-1;
void fanyi(){
N++;
if(LL1[N] == ' '){
N++;
}else{
curr = LL1[N];
}
}
void panduan1(){
panduan2();
char1();
}
void zifu(){
if(curr == '('){
fanyi();
panduan1();
if(curr == ')'){
fanyi();
}else{
printf("%d %c之后的文法有错!
",N,curr);
}
}else{
while(1){
if(curr >= 'a' && curr<='z' || curr >= 'A' && curr <= 'Z' || curr >= '0' && curr <= '9'){
fanyi();
}else{
break;
}
}
}
}
void char2(){
if(curr == '*'){
fanyi();
zifu();
panduan2();
}else if(curr == '/'){
fanyi();
zifu();
panduan2();
}else if(LL1[N+1] == '#' || LL1[N+1] == '+' || LL1[N+1] == '/' || LL1[N+1] == '*' || LL1[N+1] == '-'){
if(LL1[N] != '(' && LL1[N] != ')')
printf("%d %c之后的文法有错!
",N,curr);
}
}
void panduan2(){
zifu();
char2();
}
void char1(){
if(curr == '+'){
fanyi();
panduan2();
char1();
}
else if(curr == '-'){
fanyi();
panduan2();
char1();
}else if(LL1[N+1] == '#' || LL1[N+1] == '+' || LL1[N+1] == '/' || LL1[N+1] == '*' || LL1[N+1] == '-'){
if(LL1[N] != '(' && LL1[N] != ')')
printf("%d %c之后的文法有错!
",N,curr);
}
}
main()
{ char ch;
int i=0;
printf("请输入需要分析的curr语法:(以#结束)
");
do{
scanf("%c",&ch);
LL1[i] = ch;
i++;
}while(ch != '#');
fanyi();
panduan1();
if(curr == '#')
printf("成功!
");
else{
printf("文法有误!!
");
printf("%d %c之后的文法有错!
",N,curr);
}
}