构造递归下降解析器以解析以下语法。对于所有的epsilon作品(e),有没有一种方法可以返回而不传回任何东西? (考虑到这种方法,我正在进行解析,如代码所示)
E = TG
G = + TG | Ë
T = FH
H = * FH | Ë
F =(E)| ID
#include <stdio.h>
char* next;
int terminal(char);
int E();int G();int G1();int G2();int T();int H();int H1();int H2();int F();int F1();int F2();
int main(int argc, char const *argv[])
{
char str[10];
printf("Enter an expression to be parsed : ");
scanf("%s", str);
next = &str[0]
(*next == '\0' && E() == 1) ? printf("Parsed Successfully\n") : printf("Parsed Unsuccessfully\n");
return 0;
}
int terminal(char token){return *next++ == token;}
int E(){return T() && G();}
int G(){char* temp = next; return (next = temp, G1()) || (next = temp, G2());}
int G1(){return terminal('+') && T() && G();}
int G2(){return;} //ERROR : non-void function should return a value
int T(){return F() && H();}
int H(){char* temp = next; return (next = temp, H1()) || (next = temp, H2());}
int H1(){return terminal('*') && F() && H();}
int H2(){return;} //ERROR : on-void function should return a value
int F(){char* temp = next; return (next = temp, F1()) || (next = temp, F2());}
int F1(){return terminal('(') && E() && terminal(')');}
int F2(){return terminal('a');}
答案 0 :(得分:0)
代码不可读。但逻辑表明你应该返回一个布尔值。空制作总是成功的。所以它应该返回true。例如,
int H2(){return true;}
当然,内联这些琐碎的作品是有意义的。