E→te '
E ' →+te ' |-te ' |ε
T→ft '
T ' →*ft ' |/ft ' |ε
f→ (E) |i
#include <stdio.h> #include <string> char str[10]; int index=0; void E (); void X (); void T (); void Y (); void F (); int main () {int len; int m; printf ("Please enter the number of times to test:"); scanf ("%d", &m); while (m--) {printf ("Please enter an arithmetic expression:"); scanf ("%s", str); Len=strlen (str); str[len]= ' # '; str[len+1]= ' + '; E (); printf ("correct statement! \ n "); strcpy (str, ""); index=0; } return 0; } void E () {T (); X (); } void X () {if (str[index]== ' + ' | | | str[index]== '-') {index++; T (); X (); }} void T () {F (); Y (); } void Y () {if (str[index]== ' * ' | | | str[index]== '/') {index++; F (); Y (); }} void F () {if (str[index]== ' I ') {index++; } else if (str[index]== ' (') {index++; E (); Ifstr[index]== ') {index++; }else{printf ("\ nthe analysis failed!\n"); Exit (0); }} else{printf ("Parse failed!\n"); Exit (0); } }
1231-Recursive descent syntax analysis