设有文法GAA→iBeB→SBS→eCiC→eCe编写一个递归下降分析程序对给定的单词序列判断其是否符合该文法的要求?如果是输出Success否则输出Error。输入格式:一个字符串为单词序列每个符号为一个单词输出格式:Success或者Error。使用Java语言进行代码的编写
import java.util.Scanner;
public class Main {
private static String input;
private static int index;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
input = scanner.next();
index = 0;
if (G_A()) {
if (index == input.length()) {
System.out.println("Success");
} else {
System.out.println("Error");
}
} else {
System.out.println("Error");
}
}
private static boolean G_A() {
if (input.charAt(index) == 'i') {
index++;
if (B()) {
if (input.charAt(index) == '*') {
index++;
if (E()) {
return true;
}
}
}
}
return false;
}
private static boolean B() {
if (input.charAt(index) == 'S') {
index++;
if (B()) {
return true;
}
} else if (input.charAt(index) == '.') {
index++;
if (input.charAt(index) == 'i') {
index++;
if (C()) {
return true;
}
}
}
return false;
}
private static boolean C() {
if (E()) {
if (C()) {
return true;
}
} else {
return true;
}
return false;
}
private static boolean E() {
if (input.charAt(index) == 'e') {
index++;
return true;
}
return false;
}
}
``
原文地址: https://www.cveoy.top/t/topic/hmZE 著作权归作者所有。请勿转载和采集!