【编译原理】TEST递归下降演示
作者:互联网
MyUtil.java
package Util; import Value_Final.RRule; public class MyUtil { /** * 判断字符串是否是关键字 * @param key 要判断的字符串 * @return 是否找到 * */ public static boolean isKeyValue(String key){ for(int i=0;i< RRule.KEY_VALUE.length;i++){ if(key.equals(RRule.KEY_VALUE[i])){ return true; } } return false; } /** * 判断字符串是否是双分界符 * @param key 要判断的字符串 * @return 是否找到 * */ public static boolean isDoubleDivider(String key){ for(int i=0;i< RRule.DOUBLE_DIVIDER.length;i++){ if(key.equals(RRule.DOUBLE_DIVIDER[i])){ return true; } } return false; } /** * 判断字符串是否是单分界符 * @param key 要判断的字符串 * @return 是否找到 * */ public static boolean isSignalDivider(String key){ for(int i=0;i< RRule.SIGNAL_DIVIDER.length;i++){ if(key.equals(RRule.SIGNAL_DIVIDER[i])){ return true; } } return false; } /** * 判断字符串是否是字母还是数字 * @param key * @return 1 是字母 0 是数字 -1 异常 * */ public static int isWordsOrNumber(char key){ int number=(int)key; if((number>='A'&&number<='Z')||(number>='a'&&number<='z')){ return 1; }else if(number>='0'&&number<='9'){ return 0; }else { return -1; } } /** * 判断字符串是否是数字 * @param ss * @return * */ public static Boolean isNumber(String ss){ for(int i=0;i<ss.length();i++){ if(isWordsOrNumber(ss.charAt(i))!=0){ return false; } } return true; } }
RRule.java
package Value_Final; /** * 一些常用变量规则 * */ public class RRule { //关键字 规则 public static final String[] KEY_VALUE={"abstract","assert","boolean","break","byte","case","catch","char", "class","const","continue","default","do","double","else","enum","extends","final","finally", "float","for","goto","if","implements","import","instanceof","int","interface","long","native", "long","native","new","package","private","protected","public","return","short","static","super", "switch","this","throw","throws","try","void","while"}; public static final String[] SIGNAL_DIVIDER={"=","+","-","*","(",")","{","}",";",":",","}; public static final String[] DOUBLE_DIVIDER={"==",">=","<=","!="}; }
Analysis,java
import Util.MyUtil; import java.util.Scanner; public class Analysis { public static void main(String[] args) { String inputCode=getInputString(); String spiltItem[]=inputCode.split(" "); for(String item:spiltItem){ if (MyUtil.isKeyValue(item)){ System.out.println(item);//输出关键字 }else if(MyUtil.isSignalDivider(item)){ System.out.println(item); }else {//不是关键字则有可能是好几种情况 if(item.contains(";")){//解析a+b=c; String addstring=""; for(int i=0;i<item.length();i++){ if(MyUtil.isSignalDivider(item.charAt(i)+"")){//如果是单分隔符 System.out.println(item.charAt(i)); addstring=""; continue; } addstring=addstring+item.charAt(i); if(MyUtil.isDoubleDivider(addstring)){//判断是不是双分隔符 System.out.println(item.charAt(i)); addstring=""; continue; } if(item.charAt(i+1)==';'){ if(MyUtil.isNumber(addstring)){ System.out.println("数字:"+addstring); }else { System.out.println("变量:"+addstring); } System.out.println(";"); } } } } } } public static String getInputString() { System.out.println("请输入一段代码,并以最后一行为end结束"); Scanner scanner = new Scanner(System.in); String firstLine = scanner.nextLine(); StringBuffer ss = new StringBuffer(firstLine+" "); while (!firstLine.equals("end")) { firstLine = scanner.nextLine(); ss.append(firstLine+" "); } scanner.close(); System.out.println("读取的是"+ss.toString()); return ss.toString().replace("end", ""); } }
标签:return,String,递归,static,RRule,key,TEST,演示,public 来源: https://www.cnblogs.com/robotpaul/p/11752818.html