⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lex.java

📁 基于c0文法写的生成p-code的编译器
💻 JAVA
字号:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PushbackReader;
import java.util.LinkedList;
/**
 * @author lilac
 */
public class Lex {
    //type define
    //kind
    public static final int INTEGER = 0;
    public static final int IDSY = 1;
    public static final int STRING = 2;
    //keyword
    public static final int CONST = 3;
    public static final int INT = 4;
    public static final int IF = 5;
    public static final int ELSE = 6;
    public static final int WHILE = 7;
    public static final int VOID = 8;
    public static final int MAIN = 9;
    public static final int SCANF = 10;
    public static final int PRINTF = 11;
    public static final int RETURN = 12;
    //operation   
    public static final int ADD = 13;
    public static final int MINUS = 14;
    public static final int MUL = 15;
    public static final int DIV = 16;
    public static final int EQUAL = 17;
    //divide    
    public static final int Lpar = 18;
    public static final int Rpar = 19;
    public static final int Lbra = 20;
    public static final int Rbra = 21;
    public static final int COMMA = 22;
    public static final int SEMICOLON = 23;
    //relation operation
    public static final int G = 24;
    public static final int L = 25;
    public static final int GE = 26;
    public static final int LE = 27;
    public static final int NE = 28;
    public static final int EE = 29;
    public static final String[] KEYWORD = {
        "const", "int", "if", "else", "while", "void", "main", "scanf", "printf", "return"
    };
    /**
     * @param args
     */
    private PushbackReader codereader;
    private Lexoutput lop;
    public LinkedList<Lexoutput> list;
    public String erroroutput=null;
    //构造函数	
    public Lex(File file) {
        if (!file.exists() || !file.isFile()) {
            return;
        }

        FileReader freader = null;
        try {
            freader = new FileReader(file);
            codereader = new PushbackReader(freader, 1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
    //词法分析子程序
    public void lex_analy() {
        int c = 0;
        String ss = null;

        list = new LinkedList<Lexoutput>();
        char[] cc = new char[500];
        int error = 0;
        try {
            while ((c = codereader.read()) != -1 && error==0) {
                
                lop = new Lexoutput();
                if (c == ' ' || c == '\n' || c == '\t' || c == '\r') {              //舍去各种空格符
                    continue;
                } else if (c == '+' || c == '-') {                               //读到+,-时
                    char a = (char) c;                                 //把符号存为a,考查后面是否关联
                    c = codereader.read();
                    if (c == -1) {
                        if (a == '+') {                             //当后面没有符号时,即为+或-本身,并结束词法分析
                            lop.type = ADD;
                            lop.word = "+";
                        } else {
                            lop.type = MINUS;
                            lop.word = "-";
                        }
                        list.add(lop);
                        break;
                    }
                    //当加减号后面紧跟数字,且加减号前面是=号或(号时,视作有符号的数字;否则视为单个加减号
                    if (c <= '9' && c > '0' && (list.getLast().type == EQUAL || list.getLast().type == Lpar)) {
                        cc[0] = a;
                        int i = 1;
                        for (; (c <= '9' && c >= '0' && c != -1); c = codereader.read(), i++) {
                            cc[i] = (char) c;
                        }
                        if (c == -1) {
                            ss = new String(cc, 0, i);
                            lop.type = INTEGER;
                            lop.word = ss;
                            list.add(lop);
                            break;
                        } else {
                            ss = new String(cc, 0, i);
                            codereader.unread(c);
                            lop.type = INTEGER;
                            lop.word = ss;
                        }
                    } else {
                        codereader.unread(c);
                        if (a == '+') {
                            lop.type = ADD;
                            lop.word = "+";
                        } else {
                            lop.type = MINUS;
                            lop.word = "-";
                        }
                    }
                } else if (c == '*') {
                    lop.type = MUL;
                    lop.word = "*";
                } else if (c == '/') {
                    lop.type = DIV;
                    lop.word = "/";
                } else if (c == '<' || c == '>' || c == '=' || c == '!') {     //当读到比较运算符时预读一个,考查是否仍为比较运算符,若是则组合为一个运算符,否则为单个算符
                    char a = (char) c;
                    c = codereader.read();
                    if (c == '=') {
                        if (a == '<') {
                            lop.type = LE;
                            lop.word = "<=";
                        } else if (a == '>') {
                            lop.type = GE;
                            lop.word = ">=";
                        } else if (a == '=') {
                            lop.type = EE;
                            lop.word = "==";
                        } else if (a == '!') {
                            lop.type = NE;
                            lop.word = "!=";
                        }
                    } else {
                        if (a == '<') {
                            lop.type = L;
                            lop.word = "<";
                        } else if (a == '>') {
                            lop.type = G;
                            lop.word = ">";
                        } else if (a == '=') {
                            lop.type = EQUAL;
                            lop.word = "=";
                        } else if (a == '!') {
                            System.out.println("error'!'");
                        }
                        if (c == -1) {
                            list.add(lop);
                            break;
                        }
                        codereader.unread(c);
                    }
                } //当读到字母时,一直往下读到不是字母或数字为止,再考查是否为关键字,若是则输出对应关键字,否则为标识符
                else if (c == '_' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
                    cc[0] = (char) c;
                    int i = 1;
                    int keywordflag = 0;
                    for (c = codereader.read(); (c == '_' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c <= '9' && c >= '0') && c != -1; c = codereader.read(), i++) {
                        cc[i] = (char) c;
                    }
                    ss = new String(cc, 0, i);
                    //decide iskeyword?
                    for (i = 0; i < KEYWORD.length; i++) {
                        if (ss.equals(KEYWORD[i])) {
                            lop.type = i + 3;
                            lop.word = ss;
                            keywordflag++;
                            break;
                        }
                    }
                    if (keywordflag > 0) {
                        list.add(lop);
                        if (c == -1) {
                            break;
                        }
                        codereader.unread(c);
                        continue;
                    } else {
                        lop.type = IDSY;
                        lop.word = ss;
                        if (c == -1) {
                            list.add(lop);
                            break;
                        }
                        codereader.unread(c);
                    }
                } else if (c <= '9' && c >= '0') {
                    cc[0] = (char) c;
                    int i = 1;
                    for (c = codereader.read(); (c <= '9' && c >= '0' && c != -1); c = codereader.read(), i++) {
                        cc[i] = (char) c;
                    }
                    ss = new String(cc, 0, i);
                    lop.type = INTEGER;
                    lop.word = ss;
                    if (c == -1) {
                        list.add(lop);
                        break;
                    }
                    codereader.unread(c);
                } //当读到双引号时,一直往下读到右双引号时为止,之前的都存为字符串
                else if (c == '\"') {
                    cc[0] = (char) c;
                    int i = 1;
                    for (c = codereader.read(); c != '\"' && c != -1; c = codereader.read(), i++) {
                        cc[i] = (char) c;
                    }
                    cc[i] = '\"';
                    ss = new String(cc, 0, i + 1);
                    lop.type = STRING;
                    lop.word = ss;
                    if (c == -1) {
                        list.add(lop);
                        break;
                    }
                } else if (c == ',') {
                    lop.type = COMMA;
                    lop.word = ",";
                } else if (c == ';') {
                    lop.type = SEMICOLON;
                    lop.word = ";";
                } else if (c == '(') {
                    lop.type = Lpar;
                    lop.word = "(";
                } else if (c == ')') {
                    lop.type = Rpar;
                    lop.word = ")";
                } else if (c == '{') {
                    lop.type = Lbra;
                    lop.word = "{";
                } else if (c == '}') {
                    lop.type = Rbra;
                    lop.word = "}";
                } else {
                    erroroutput="can't recognize " + (char) c + " so jump over it!";
                    error++;
                    continue;
                }
                list.add(lop);

            }
//
//            for (Lexoutput lo : list) {
//                System.out.println(lo);
//            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        // TODO Auto-generated method stub
//        File file = new File("c:\\test2.c");
//        Lex lx = new Lex(file);
//        //lx.readfile();
//        lx.lex_analy();
//        Compile cc = new Compile(lx.list);
//        cc.Prog();
//
//        //打印
//        if (cc.error == 0) {
//            System.out.println("topconst:");
//            for (Constpair pair : cc.topconst) {
//                System.out.println("const " + pair.toString());
//            }
//            System.out.println("topvar:");
//            for (String ss : cc.topvar) {
//                System.out.println("int " + ss);
//            }
//            System.out.println("===============================================");
//            for (Function func : cc.funclist) {
//                System.out.println("FUNCTION:");
//                System.out.println("===============================================");
//                System.out.println(func.retype + " " + func.name);
//                System.out.println("PARA:");
//                for (String para : func.para) {
//                    System.out.println("int " + para + "\t" + func.pmap.get(para));
//                }
//                System.out.println("CONST:");
//                for (Constpair pair : func.con) {
//                    System.out.println("const " + pair.toString());
//                }
//                System.out.println("VAR:");
//                for (String var : func.var) {
//                    System.out.println("int " + var + "\t" + func.vmap.get(var));
//                }
//                for (Four f : func.fourlist) {
//                    if (f.result != null && f.result.startsWith("@")) {
//                        System.out.println("int " + f.result + "\t" + func.vmap.get(f.result));
//                    }
//                }
//                System.out.println("FOUR:");
//                System.out.println("===============================================");
//                for (Four four : func.fourlist) {
//                    System.out.println(four.toString());
//                }
//            }
//        } else {
//            System.out.println(new Integer(cc.error).toString());
//        }
//
//        //汇编输出
//        Generate gen = new Generate(cc.topconst, cc.topvar, cc.funclist);
//        gen.Generate();
//    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -