词法分析器

单词状态转换图

词法分析器_第1张图片

算法描述

0、单词以字母开头(保留字、标识符),通过查保留字表可以确定是哪种
1、单词以数字开头进行判断是小数、正整数、科学计数
2、遇到运算符、界符直接存到相关表
3、遇到>得判断后面的符号是不是和>是一个整体(>=),如果是将整体存到界符运算符表中,如果不是就只将>存到表中。(<、&、|类似)
4、上图中其它符号会作为一个新单词的开始继续扫描
5、遇到空格会直接跳过寻找下一个单词。

代码实现


import java.io.*;
import java.util.LinkedHashMap;
import java.util.Map;

public class Word1 {

    public static String [] retainWords = new String[]{//c语言的32个保留字
                "auto", "break", "case", "char", "const", "continue",
                "default", "do", "double", "else", "enum", "extern",
               "float", "for", "goto", "if", "int", "long",
                "register", "return", "short", "signed", "sizeof", "static",
                "struct", "switch", "typedef", "union", "unsigned", "void",
                "volatile", "while"
             };
    public static String [] operatorWords = new String []{//界符
            "+", "-", "*", "/", "<", "<=", ">", ">=", "=", "==",
            "!=", ";", "(", ")", "^", ",", "\"", "\'", "#", "&",
            "&&", "|", "||", "%", "~", "<<", ">>", "[", "]", "{",
            "}", "\\", ".", ":", "!"};
    public String headFile="";
    public String define="";
    public Map remainWordMap = new LinkedHashMap();//保留字
    public Map identifierMap = new LinkedHashMap();//标识符
    public Map numMap = new LinkedHashMap();//整数
    public Map operatorMap = new LinkedHashMap();//界符
    public String strToken = "";//存放构成单词符号的字符串
    //判断是否是字母
    public boolean isLetter(char ch){
        return Character.isLetter(ch);
    }
    //判断是否是数字
    public boolean isDigit(char ch){
        return Character.isDigit(ch);
    }
    //判断是否是空格
    public boolean isBC(char ch){
        return " ".equals(ch+"");
    }
    //连接字符
    public void concat(char ch){
        strToken += ch+"";
    }
    //清清除strToken
    public void clearStrToken(){
        strToken = "";
    }
    //判断是否是保留字(返回保留字对应的code)
    public int reserve(){
        for(int i = 0;i < retainWords.length;i++){
            if(strToken.equals(retainWords[i])){
                return i+1;//是保留字
            }
        }

        return -1;//标识符
    }

    public String getHeadFile() {
        return headFile;
    }

    public String getDefine() {
        return define;
    }

    //返回界符对应的code

    public int isOperator(){
        for (int i = 0; i '){//> 和>=和 >>
               if(all.charAt(i+1)=='='){
                   strToken += ">=";
                   operatorMap.put(strToken,isOperator());
                   i += 2;
               }else if(all.charAt(i+1)=='>'){
                   strToken += ">>";
                   operatorMap.put(strToken,isOperator());
                   i += 2;
               } else{
                   concat('>');
                   operatorMap.put(strToken,isOperator());
                   i++;
               }
               clearStrToken();
           }
            if(all.charAt(i)=='!'){//!和!=
                if(all.charAt(i+1)=='='){
                    strToken += "!=";
                    operatorMap.put(strToken,isOperator());
                    i += 2;
                }else{
                    concat('!');
                    operatorMap.put(strToken,isOperator());
                    i++;
                }
                clearStrToken();
            }

           if(all.charAt(i)=='&'){//&和&&
               if(all.charAt(i+1)=='&'){
                   strToken += "&&";
                   operatorMap.put(strToken,isOperator());
                   i += 2;
               }else{
                   concat('&');
                   operatorMap.put(strToken,isOperator());
                   i++;
               }
               clearStrToken();
           }

            if(isBC(all.charAt(i))){//判断是否是空格
                i++;
                continue;
            }



        }
    }
    public static void main(String[] args) throws IOException {
        Word1 w = new Word1();
        System.out.println(w.filter());

        System.out.println("包含头文件如下:");
        System.out.println(w.getHeadFile());
        System.out.println("包含宏定义如下:");
        System.out.println(w.getDefine());
        System.out.println("过滤后字符串长度:"+w.filter().length());
        w.scanner(w.filter());
        System.out.println("保留字如下:");

        BufferedWriter out = new BufferedWriter(new FileWriter("d:\\remainWord.txt"));
        //保留字
        for(Map.Entry mEntry:w.remainWordMap.entrySet()){
            String key=mEntry.getKey();
            Integer value =mEntry.getValue();
            out.write("("+key+","+value+")");
            out.newLine();
            System.out.println("("+key+","+value+")");
        }out.close();
        out = new BufferedWriter(new FileWriter("d:\\operatorWord.txt"));

        //界符
        System.out.println("界符和运算符如下:");
        for(Map.Entry mEntry:w.operatorMap.entrySet()){
            String key=mEntry.getKey();
            Integer value =mEntry.getValue();
            out.write("("+key+","+value+")");
            out.newLine();
            System.out.println("("+key+","+value+")");
        }out.close();

        out = new BufferedWriter(new FileWriter("d:\\numWord.txt"));
        System.out.println("数字如下:");
        //数字
        for(Map.Entry mEntry:w.numMap.entrySet()){
            String key=mEntry.getKey();
            String value =mEntry.getValue();
            out.write("("+key+","+value+")");
            out.newLine();
            System.out.println("("+key+","+value+")");
        }out.close();

        out = new BufferedWriter(new FileWriter("d:\\identifierWord.txt"));
        System.out.println("标识符如下:");
        //标识符
        for(Map.Entry mEntry:w.identifierMap.entrySet()){
            String key=mEntry.getKey();
            Integer value =mEntry.getValue();
            out.write("("+key+","+value+")");
            out.newLine();
            System.out.println("("+key+","+value+")");
        }out.close();


    }
}


你可能感兴趣的:(词法分析器)