简易词法分析器

Posted Accustomed

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简易词法分析器相关的知识,希望对你有一定的参考价值。

private static void search(char[] ch) {
        String s = "" ;
        int de = 0;
        char a;
        boolean isImport = false;//是否为关键字
        boolean isNum =false;//是否为数字
        boolean isId = false;//是否为字符串
        boolean isBi= false;//是否为单个操作符
        boolean isBiM = false;//是狗为组合操作符 
        String[] proter = { "int", "main", "char", "if", "else", "for", "while" };
        int i = 0;
        
        
        while (i < ch.length) {
            String pr = "";
            
            while (ch[i] != ‘ ‘ ){
                
                if((ch[i] <= ‘z‘) && (ch[i]>=‘a‘) || (ch[i] <=‘Z‘) && (ch[i] >=‘A‘))
                    //System.out.print(ch[i]);
                    isId = true;
                if((ch[i]<=‘9‘) && (ch[i]>=‘0‘))
                    //System.out.print(ch[i]);
                    isNum = true;
            
                switch (ch[i]) {
                case ‘=‘:
                    if(ch[i+1] == ‘=‘){
                        de=39;
                        s="==";
                        isBiM = true;
                        i++;
                        //System.out.print(s);
                    }
                else
                    de = 21;
                    isBi = true;
                    break;
                case ‘>‘:
                    if(ch[i+1] == ‘=‘){
                        de=37;
                        s=">=";
                        isBiM = true;
                        i++;
                        //System.out.print(s);
                    }
                else
                    de=35;
                    isBi = true;
                    
                    break;
                case ‘]‘:
                    de=29;
                    isBi=true;
                    break;
                case ‘{‘:
                    de=30;
                    isBi=true;
                    break;
                case ‘}‘:
                    de=31;
                    isBi=true;
                    break;
                case ‘:‘:
                    de=33;
                    isBi=true;
                    break;
                case ‘;‘:
                    de=34;
                    isBi=true;
                    break;
                case ‘.‘:
                    de=32;
                    isBi=true;
                    break;
                case ‘<‘:
                    if(ch[i+1] == ‘=‘){
                        de=38;
                        s="<=";
                        isBiM = true;
                        i++;
                        //System.out.print(s);
                    }
                    else
                    de=36;
                    isBi=true;
                    break;
                case ‘*‘:
                    de=24;
                    isBi = true;
                    break;
                    
                case ‘+‘:
                    de=22;
                    isBi=true;
                    break;
                case ‘-‘:
                    de=23;
                    isBi = true;
                    break;
                case ‘/‘:
                    de=25;
                    isBi = true;
                    break;
                case ‘(‘:
                    de=26;
                    isBi = true;
                    break;
                case ‘)‘:
                    de=27;
                    isBi = true;
                    break;
                case ‘#‘:
                    de=0;
                    isBi =true;
                    break;
                }
                if((ch[i] == ‘!‘) && (ch[i+1] == ‘=‘)){
                    de=40;
                    s="!=";
                    isBiM = true;
                    i++;
                }
                if(isBi){
                    break;
                }
                pr += ch[i++];
                
                
            }
            
            
            // System.out.print(pr);
            ////判断关键字
            for (int j = 0; j < proter.length; j++) {
                if (pr.equals(proter[j])){
                    isImport = true;    
                    System.out.println("(" + (j + 1) + "," + proter[j] + ")");
                    }
            }
            
            //字母或字母数字串
            if(isId && !isImport)
                System.out.println("(" +10+ "," + pr+ ")");
            //数字
             if(!isId && isNum )
                System.out.println("(" +20+ "," + pr+ ")");
            //单个专用符号
                if(isBi && !isBiM)
                System.out.println("("+de+","+ch[i]+")");
            //组合符号
             if(isBiM)
                System.out.println("("+de+","+s+")");
            //else    System.out.println("("+-1+","+"ERRO"+")");
                i++;
            isId = false;
            isNum = false;
            isImport = false;
            isBi = false;
            isBiM=false;
            // System.out.print("("+);
        }
        

    }

 

以上是关于简易词法分析器的主要内容,如果未能解决你的问题,请参考以下文章

简易的词法分析程序

C语言编译器开发之旅:词法分析扫描器

编译原理 实验一 java语言实现对C语言词法分析

编译原理 实验一 java语言实现对C语言词法分析

Java 实现《编译原理》简单词法分析功能

用java实现一个简易编译器1-词法解析入门