• 【编译原理】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", "");
    
        }
    }
  • 相关阅读:
    html 第一阶段 学习使用总结
    Appium环境搭建python(一)
    sendmail发送邮件
    将mysql添加到centos系统服务
    Starting nagios:This account is currently not available.
    如何删除linux中的邮件
    被监控机上安装nagios插件和nrpe(nrpe添加为xinetd服务)
    疑问:进程间通信
    Gerrit error when ChangeId in commit messages are missing
    string 和 char *两者的区别是什么 ?什么时候用string好? 什么时候用 char * 合适?什么时候同时都能用?
  • 原文地址:https://www.cnblogs.com/robotpaul/p/11752818.html
Copyright © 2020-2023  润新知