1217 实验四 递归下降语法分析程序设计

import java.util.Scanner;

public class dealMain {

public dealMain() {
// TODO 自动生成的构造函数存根
String allChar = new String();
Scanner input=new Scanner(System.in);//定义键盘接收数据
System.out.print("请输入:");
allChar = input.next();
char[] p =allChar.toCharArray();
for (int i = 0; i < p.length-1; i++) {
char c = p[i];
if((c=='/'||c=='*'||c=='+'||c=='-'||c=='=')){
if(p[i+1]=='/'||p[i+1]=='*'||p[i+1]=='+'||p[i+1]=='-'||p[i+1]=='='){
System.out.println("符号语法错误");
System.exit(0);
}else if(i==0&&c!='-'){
System.out.println("符号语法错误");
System.exit(0);
}
}
if(48<=c&&c<=57){
if((p[i+1]>=65&&p[i+1]<=90)||(p[i+1]>=97&&p[i+1]<=122)){
System.out.println("变量语法错误");
System.exit(0);
}
}
if((p[i+1]>=65&&p[i+1]<=90)||(p[i+1]>=97&&p[i+1]<=122)){
if(48<=c&&c<=57){
System.out.println("变量语法错误");
System.exit(0);
}
}
}
System.out.println("是正确的表达式");
}
public static void main(String[] args) {
new dealMain();

}

}

原文地址:https://www.cnblogs.com/wucanlong/p/5091920.html