/* * E => T + E | T - E | T * T => F * T | F / T | F * F => I | L | (E) * I => a | ... | z * L => 0 | ... | 9 */ /* * * C version * * */ #include #include bool E(void); bool T(void); bool F(void); bool I(void); bool L(void); char *c; int main(int argc, char *argv[]) { c = argv[1]; if (E() && *c == '\0') { printf("The string \"%s\" is in the language.\n", argv[1]); } else { printf("The string \"%s\" is not in the language.\n", argv[1]); } return 0; } bool E() { if (T()) { if (*c == '+' || *c == '-') { ++c; if (E()) { return true; } else { return false; } } return true; } return false; } bool T() { if (F()) { if (*c == '*' || *c == '/') { ++c; if (T()) { return true; } else { return false; } } return true; } return false; } bool F() { if (I()) { return true; } else if (L()) { return true; } else if (*c == '(') { ++c; if (E()) { if (*c == ')') { ++c; return true; } } } return false; } bool I() { if (*c >= 'a' && *c <= 'z') { ++c; return true; } return false; } bool L() { if (*c >= '0' && *c <= '9') { ++c; return true; } return false; } /* * * C++ version * * */ #include using std::cout; using std::endl; bool E(void); bool T(void); bool F(void); bool I(void); bool L(void); char *c; int main(int argc, char *argv[]) { c = argv[1]; if (E() && *c == '\0') { cout << "The string \"" << argv[1] << "\" is in the language." << endl; } else { cout << "The string \"" << argv[1] << "\" is not in the language." << endl; } return 0; } bool E() { if (T()) { if (*c == '+' || *c == '-') { ++c; if (E()) { return true; } else { return false; } } return true; } return false; } bool T() { if (F()) { if (*c == '*' || *c == '/') { ++c; if (T()) { return true; } else { return false; } } return true; } return false; } bool F() { if (I()) { return true; } else if (L()) { return true; } else if (*c == '(') { ++c; if (E()) { if (*c == ')') { ++c; return true; } } } return false; } bool I() { if (*c >= 'a' && *c <= 'z') { ++c; return true; } return false; } bool L() { if (*c >= '0' && *c <= '9') { ++c; return true; } return false; } /* * * Java version * * / public class Main { public static void main(String[] args) { s = args[0]; if (E() && i == s.length()) { System.out.println("The string \"" + args[0] + "\" is in the language."); } else { System.out.println("The string \"" + args[0] + "\" is not in the language."); } } private static boolean E() { if (T()) { if (i < s.length() && (s.charAt(i) == '+' || s.charAt(i) == '-')) { ++i; if (E()) { return true; } else { return false; } } return true; } return false; } private static boolean T() { if (F()) { if (i < s.length() && (s.charAt(i) == '*' || s.charAt(i) == '/')) { ++i; if (T()) { return true; } else { return false; } } return true; } return false; } private static boolean F() { if (I()) { return true; } else if (L()) { return true; } else if (i < s.length() && s.charAt(i) == '(') { ++i; if (E()) { if (i < s.length() && s.charAt(i) == ')') { ++i; return true; } } } return false; } private static boolean I() { if (i < s.length() && s.charAt(i) >= 'a' && s.charAt(i) <= 'z') { ++i; return true; } return false; } private static boolean L() { if (i < s.length() && s.charAt(i) >= '0' && s.charAt(i) <= '9') { ++i; return true; } return false; } private static String s; private static int i; }