码迷,mamicode.com
首页 > 其他好文 > 详细

递归下降分析法

时间:2015-12-30 17:00:19      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

#include<stdio.h>
char curr;
char YF[300];
int N=-1;

void T();
void E();
void F();
void e();
void t();
void error();
void scaner();

void main()
{ char ch;
int i=0;
printf("请输入需要分析的curr语法:(以#结束)\n");
do{
scanf("%c",&ch);
YF[i] = ch;
i++;
}while(ch != ‘#‘);
scaner();
E();
if(curr == ‘#‘)
printf("成功!\n");
else{
printf("文法有误!!\n");
error();
}

}

void scaner(){ 

N++;
if(YF[N] == ‘ ‘){
N++;
}else{
curr = YF[N];
}
}

void E(){
T();
e();
}
void T(){
F();
t();
}

void e(){
if(curr == ‘+‘){
scaner();
T();
e();
}
else if(curr == ‘-‘){
scaner();
T();
e();
}else if(YF[N+1] == ‘#‘ || YF[N+1] == ‘+‘ || YF[N+1] == ‘/‘ || YF[N+1] == ‘*‘ || YF[N+1] == ‘-‘){
if(YF[N] != ‘(‘ && YF[N] != ‘)‘)
error();
}
}

void t(){
if(curr == ‘*‘){
scaner();
F();
T();
}else if(curr == ‘/‘){
scaner();
F();
T();
}else if(YF[N+1] == ‘#‘ || YF[N+1] == ‘+‘ || YF[N+1] == ‘/‘ || YF[N+1] == ‘*‘ || YF[N+1] == ‘-‘){
if(YF[N] != ‘(‘ && YF[N] != ‘)‘)
error();
}
}
//LIE ZHI HUA TI GONG TAI MA
void F(){
if(curr == ‘(‘){
scaner();
E();
if(curr == ‘)‘){
scaner();
// printf("111111");
}else{
error();
}
}else{
while(1){
if(curr >= ‘a‘ && curr<=‘z‘ || curr >= ‘A‘ && curr <= ‘Z‘ || curr >= ‘0‘ && curr <= ‘9‘){
scaner();
}else{
break;
}
}
}
}

void error(){
printf("%d %c之后的文法有错!\n",N,curr);
}

技术分享

 

递归下降分析法

标签:

原文地址:http://www.cnblogs.com/huang123/p/5089274.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!