基于java结合mysql数据库对输入的任意c文件实现词法分析

直接上源代码

import com.mysql.jdbc.Connection;

import java.io.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 1   关键字
 * 2   标识符
 * 3   算数运算符
 * 4   逻辑运算符
 * 5   关系运算符
 * 6   比较运算符
 * 7   赋值运算符
 * 8   自增运算符
 * 9   自减运算符
 * 10  数据类型符号
 * 11  边界符
 * 12  转义字符
 * 13  常数
 * */

public class SecondExperiment {


    /**
     * IsLetter(char x)
     * 判断是否是字母函数
     */

    public static boolean IsLetter(char symbol) {
        if ((symbol >= 'a' && symbol < 'z') || (symbol > 'A' && symbol < 'Z') || symbol == '_') {
            return true;
        } else {
            return false;
        }
    }

    /**
     * IsDigit(char symbol)
     * 判断是否是数字函数
     */

    public static boolean IsDigit(char symbol) {
        if ((symbol >= '0' && symbol <= '9') || symbol == '.') {
            return true;
        } else {
            return false;
        }
    }

    /**
     * IsDataTypeSymbol(char symbol)
     * 判断是哪种数据类型符号
     */

    public static boolean IsDataTypeSymbol(char symbol) {
        if (symbol == '%' || symbol == 'd' || symbol == 'c' || symbol == 'l' || symbol == 'f' || symbol == 's') {
            return true;
        } else {
            return false;
        }
    }

    /**
     * IsLogicalSymbol
     * 判断参数字符是否为逻辑运算符
     **/

    public static boolean IsLogicalSymbol(char symbol) {
        if (symbol == '&' || symbol == '|' | symbol == '!'){
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * IsCompareVoSymbol(char symbol)
     * 判断是哪种比较运算符
     */

    public static boolean IsCompareVoSymbol(char symbol) {
        if (symbol == '<' || symbol == '=' || symbol == '>') {
            return true;
        } else {
            return false;
        }
    }

    /**
     * AnalysisProcess(String PathName)
     * 分析过程函数
     */

    public static void AnalysisProcess(String PathName) {
        char ch;
        String line_string = "";
        File file = new File(PathName);
        if (file.exists()) {
            try (Scanner input = new Scanner(file)) {
                while (input.hasNextLine()) {
                    line_string = input.nextLine();
                    for (int p = 0; p < line_string.length(); p++) {
                        ch = line_string.charAt(p);
                        if (IsLetter(ch)) {
                            String strToken = "";
                            while (IsDigit(ch) || IsLetter(ch)) {
                                strToken = strToken + ch;
                                ch = line_string.charAt(++p);
                            }
                            --p;
                            if (IsReserve(strToken)) {
                                System.out.println("(1\t,\t" + strToken+"\t)");
                            } else {
                                System.out.println("(2\t,\t" + strToken+"\t)");
                            }
                        } else if (Character.isDigit(ch)) {
                            String strToken_Number = "";
                            while (IsDigit(ch)) {
                                strToken_Number = strToken_Number + ch;
                                p = p + 1;
                                ch = line_string.charAt(p);
                            }
                            --p;
                            System.out.println("(13\t,\t" + strToken_Number+"\t)");
                        } else if (IsCompareVoSymbol(ch)) {
                            String strTokenCompare = "";
                            while (IsCompareVoSymbol(ch)) {
                                strTokenCompare = strTokenCompare + ch;
                                p = p + 1;
                                ch = line_string.charAt(p);
                            }
                            --p;
                            switch (strTokenCompare) {
                                case "=":
                                    System.out.println("(7\t,\t" + strTokenCompare+"\t)");
                                    break;
                                case "<":
                                    System.out.println("(6\t,\t" + strTokenCompare+"\t)");
                                    break;
                                case ">":
                                    System.out.println("(6\t,\t" + strTokenCompare+"\t)");
                                    break;
                                case ">=":
                                    System.out.println("(6\t,\t" + strTokenCompare+"\t)");
                                    break;
                                case "<=":
                                    System.out.println("(6\t,\t" + strTokenCompare+"\t)");
                                    break;
                                default:
                                    System.out.println("(13\t,\t其他不能识别的字符或者字符串\t)");
                                    break;
                            }
                        } else if (ch == ';') {
                            System.out.println("(11\t,\t" + ch+"\t)");
                        } else if (ch == ',') {
                            System.out.println("(11\t,\t" + ch+"\t)");
                        } else if (ch == '(') {
                            System.out.println("(11\t,\t" + ch+"\t)");
                        } else if (ch == ')') {
                            System.out.println("(11\t,\t" + ch+"\t)");
                        } else if (ch == '{') {
                            System.out.println("(11\t,\t" + ch+"\t)");
                        } else if (ch == '}') {
                            System.out.println("(11\t,\t" + ch+"\t)");
                        } else if (ch == '"') {
                            System.out.println("(11\t,\t" + ch+"\t)");
                        } else if (ch == '*') {
                            System.out.println("(3\t,\t" + ch+"\t)");
                        } else if (ch == '\\') {
                            p = p + 1;
                            System.out.println("(12\t,\t" + line_string.charAt(p)+"\t)");
                        } else if(IsLogicalSymbol(ch)) {
                            String strTokenLogical = "";
                            while(IsLogicalSymbol(ch)){
                                strTokenLogical=strTokenLogical+ch;
                                p=p+1;
                                ch=line_string.charAt(p);
                            }
                            --p;
                            if("!".equals(strTokenLogical)){
                                System.out.println("(4\t,\t"+strTokenLogical+"\t)");
                            }
                            else if("||".equals(strTokenLogical)){
                                System.out.println("(4\t,\t"+strTokenLogical+"\t)");
                            }else if("&&".equals(strTokenLogical)){
                                System.out.println("(4\t,\t"+strTokenLogical+"\t)");
                            }
                            else {
                            }
                        }
                            else if (ch == '+') {
                            String strToken_AutoIncrease = "";
                            while (ch == '+') {
                                strToken_AutoIncrease = strToken_AutoIncrease + ch;
                                p = p + 1;
                                try {
                                    ch = line_string.charAt(p);
                                } catch (Exception exception) {
                                    break;
                                }
                            }
                            --p;
                            if (strToken_AutoIncrease.length() == 1) {
                                System.out.println("(3\t,\t" + strToken_AutoIncrease+"\t)");
                                break;
                            } else if (strToken_AutoIncrease.length() == 2) {
                                System.out.println("(8\t,\t" + strToken_AutoIncrease+"\t)");
                            } else {
                            }
                        } else if (ch == '-') {
                            String strTokenAutoReduce = "";
                            while (ch == '-') {
                                strTokenAutoReduce = strTokenAutoReduce + ch;
                                p = p + 1;
                                try {
                                    ch = line_string.charAt(p);
                                } catch (Exception exception) {
                                    break;
                                }
                            }
                            --p;
                            if ("-".equals(strTokenAutoReduce)) {
                                System.out.println("(3\t,\t" + strTokenAutoReduce+"\t)");
                            } else if ("--".equals(strTokenAutoReduce)) {
                                System.out.println("(9\t,\t" + strTokenAutoReduce+"\t");
                            } else {
                            }
                        } else if (IsDataTypeSymbol(ch)) {
                            String strTokenDataSymblo = "";
                            while (IsDataTypeSymbol(ch)) {
                                strTokenDataSymblo = strTokenDataSymblo + ch;
                                p = p + 1;
                                ch = line_string.charAt(p);
                            }
                            p--;
                            switch (strTokenDataSymblo) {
                                case "%d":
                                    System.out.println("(10\t,\t" + strTokenDataSymblo+"\t)");
                                    break;
                                case "%lf":
                                    System.out.println("(10\t,\t" + strTokenDataSymblo+"\t");
                                    break;
                                case "%f":
                                    System.out.println("(10\t,\t" + strTokenDataSymblo+"\t)");
                                    break;
                                case "%c":
                                    System.out.println("10\t,\t" + strTokenDataSymblo+"\t)");
                                    break;
                                case "%s":
                                    System.out.println("10\t,\t" + strTokenDataSymblo+"\t)");
                                    break;
                                default:
                                    break;
                            }
                        } else if (ch == '/') {
                            String strTokenExplain = "";
                            while (ch == '/') {
                                strTokenExplain = ch + strTokenExplain;
                                p = p + 1;
                                ch = line_string.charAt(p);
                            }
                            p = line_string.length();
                        } else if (Character.isWhitespace(ch)) {
                        } else {
                        }
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("输入的文件路径错误或者不存在,请重新输入!!!!!!");
        }
    }

    /**
     * C:\Users\ml\Desktop\data.txt
     * main(String[] args)
     * 主程序入口
     */

    public static void main(String[] args) {

        while (true) {
            String flag = "yes";
            Scanner input = new Scanner(System.in);
            System.out.println("请输入分析文件的绝对路径(退出请输入'yes')!");
            String pathname = input.nextLine();
            if (pathname.equals(flag)) {
                System.out.println("即将退出程序,欢迎下次使用!!!!!!");
                break;
            }
            SecondExperiment.AnalysisProcess(pathname);
        }

    }
    /**
     * IsReserve(String Reserve)
     * 从数据库中查询关键字并判断是所传参数是否为关键字
     */

    public static boolean IsReserve(String Reserve) {
        Connection con = null;
        ResultSet resultSet = null;
        Statement s = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/java", "root" +
                    "", "123456");
            String sql = "select * from reserve";
            s = con.createStatement();
            resultSet = s.executeQuery(sql);
            while (resultSet.next()) {
                String name = resultSet.getNString("reserveName");
                if (name.equals(Reserve)) {
                    return true;
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                s.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

}

你可能感兴趣的:(java,java)