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

📄 cffc_1.java

📁 java写的PLO词法分析,刚交了作业 没什么大内容,仅供参考
💻 JAVA
字号:
import java.io.*;

/*********************
 * keyword--( k , _ )--1
 * digit----{ d , _ )--2
 * end------( e , _ )--3
 * op-------( o , _ )--4
 * var------( v , _ )--0
 *********************/

public class CFFC_1 {
  String st[]; //用于存放单个字符
  StringLinkedList list = new StringLinkedList(); //用于语法分析
  String word = ""; //临时存放单词
  StringLinkedList listword = new StringLinkedList(); //存放变量名

  private static String key[] = {
                                "procedure", "call", "begin", "end", "var",
                                "const",
                                "if", "then", "while", "do", "read", "write",
                                "odd",
                                "program", "type", "function", "array",
                                "integer",
                                "real", "char", "boobean", "case", "of",
                                "repeat",
                                "until", "to", "down"};
  private static String op[] = {
                               "+", "-", "*", "/", "=", "#", "<", ">", "<=",
                               ">=", ":=", "<>"};
  private static String end[] = {
                                "(", ")", ",", ";", ".", "[", "]", ":"};
  private String digit[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
  public boolean isDigit(String input) { //判断是否为数字
    if (input.equals("0") || input.equals("1") || input.equals("2") ||
        input.equals("3") || input.equals("4") || input.equals("5") ||
        input.equals("6") || input.equals("7") || input.equals("8") ||
        input.equals("9")) {
      return true;
    } else {
      return false;
    }
  }

  public boolean isLetter(String input) { //判断是否为字母
    char inp = '1'; //不是字母就行
    if (input.length() > 0) {
      inp = input.charAt(0);
    }
    if (((int) inp >= 65 && (int) inp <= 90)
        || ((int) inp >= 97 && (int) inp < 123)) {
      return true;
    } else {
      return false;
    }
  }

  public boolean isOp(String input) { //判断是否为算符
    if (input.equals("+") || input.equals("-") || input.equals("*") ||
        input.equals("/") || input.equals("=") || input.equals("<") ||
        input.equals(">") || input.equals("#") || input.equals(">=") ||
        input.equals("<=") || input.equals("<>") || input.equals(":=")) {
      return true;
    } else {
      return false;
    }
  }

  public boolean isEnd(String input) { //判断是否为界符
    if (input.equals(",") || input.equals(";") || input.equals("(") ||
        input.equals(")") || input.equals(".") || input.equals(":") ||
        input.equals("[") || input.equals("]")) {
      return true;
    } else {
      return false;
    }
  }

  int i_k = 0; //关键子的下标
  int i_d = 0; //数字的下标
  int i_e = 0; //界符的下标
  int i_o = 0; //算符的下标
  int i_v = 0; //变量名的下标

  public int search(String v) { //搜索 返回String v的 类别直
    String tempstr = "";
    if (v.length() > 0) {
      tempstr += v.charAt(0);
    }
    if (isLetter(tempstr)) { //如果首字符是字母
      for (i_k = 0; i_k < key.length; i_k++) { //是关键字
        if (v.equals(key[i_k])) {
          return 1;
        }
      }
      return 0; //是变量名
    } else if (isDigit(tempstr)) { //如果首字符是数字
      return 2;
    } else {
      for (i_e = 0; i_e < end.length; i_e++) { //是界符
        if (v.equals(end[i_e])) {
          return 3;
        }
      }
      for (i_o = 0; i_o < op.length; i_o++) { //是算符
        if (v.equals(op[i_o])) {
          return 4;
        }
      }
    }
    return -1; //是不能识别的符号
  }

  public void show(String v) { //显示二元组
    if (v.length() > 10) {
      System.out.println("too long: " + v);
    } else {
      if (search(v) == -1) { //不能识别的符号
        System.out.println("can't identify: " + v);
      } else if (search(v) == 1) { //关键字
        System.out.println("(k , " + i_k + ")  " + v);
        i_k = 0;
      } else if (search(v) == 2) { //数字
        for (int j = 0; j < v.length(); j++) {
          if (!isDigit("" + v.charAt(j))) { //拼写错误 数字后面有字母或不能识别的符号
            System.out.println(" error: " + v);
            break;
          }
          if (j == v.length() - 1) {
            System.out.println("(d , " + v + ")  " + v);
          }
        }
        i_d = 0;

      } else if (search(v) == 3) { //界符
        System.out.println("(e , " + i_e + ")  " + v);
        i_e = 0;
      } else if (search(v) == 4) { //算符
        System.out.println("(o , " + i_o + ")  " + v);
        i_o = 0;
      } else if (search(v) == 0) { //变量名
        for (int j = 0; j < v.length(); j++) {
          if (!(isDigit("" + v.charAt(j)) || isLetter("" + v.charAt(j)))) { //拼写错误 不能识别的符号
            System.out.println("can't identify: " + v);
            break;
          } else {
            if (j == v.length() - 1) {
              if (listword.find(v) > -1) { //变量名 在此之前出现了
                int mm = listword.length();
                int ll = listword.find(v);
                System.out.println("(v , " +
                                   (listword.length() - 1 - listword.find(v)) +
                                   ")  " + v);
              } else {
                listword.addNodeToStart(v); //该变量名第一次出现
                System.out.println("(v , " + i_v + ")  " + v);
                i_v++;
              }
            }
          }
        }
      }
    }
  }

  public CFFC_1() {

    try {
      System.out.println("Enter the name of the file:");
      String filename = SavitchIn.readLineWord();
      File f = new File(filename);
      RandomAccessFile inputStream = new RandomAccessFile(f, "r");
      long fileLength = f.length();
      String str = null;
      int no = 0; // 行号
      while (fileLength != inputStream.getFilePointer()) {
        str = nextline(inputStream);
        //System.out.println("the str's length is: "+str.length() );
        if (str.length() == 0) {
        //System.out.println("");
        } else { //非空行
          st = new String[str.length()];
          for (int i = 0; i < str.length(); i++) {
            st[i] = "";
            st[i] += str.charAt(i); //获取字符
          }
          int i = 0; //字符号
          place1:while (i < str.length()) {
            if (st[i].equals(" ") || st[i].equals("" + '\t')) {
              i++;
              continue place1;
            } else if (st[i].equals(",") || st[i].equals(";") ||
                       st[i].equals("(") ||
                       st[i].equals(")") || st[i].equals(".") ||
                       st[i].equals("[") || st[i].equals("]")) { //开头是界符,但由于 : 和:= 有歧异,因此单独处理:
              word += st[i];
              list.addNodeToStart(word);
              //show(word);
              //System.out.println(word);
              //word = ""; //清空word
              i++;
            } else if (st[i].equals("+") || st[i].equals("-") ||
                       st[i].equals("*") || st[i].equals("#") ||
                       st[i].equals("/") || st[i].equals("=")) { //开头是单算符  //不改变代码
              word += st[i];
              list.addNodeToStart(word);
              //show(word);
              //System.out.println(word);
              //word = ""; //清空word
              i++;
            } else if (st[i].equals("<")) { //开头是<
              word += st[i];
              i++;
              if (i < str.length()) { //防止下一行的 st[i] 超界
                if (st[i].equals("=") || st[i].equals(">")) { //开头是<= 或者<>
                  word += st[i];
                  list.addNodeToStart(word);
                  //show(word);
                  //System.out.println(word);
                  //word = ""; //清空word
                  i++;
                } else {
                  list.addNodeToStart(word);
                  //show(word);
                  //System.out.println(word);
                  //word = ""; //清空word
                }
              }
            } else if (st[i].equals(">")) { //开头是 >
              word += st[i];
              i++;
              if (i < str.length()) { //防止下一行的 st[i] 超界
                if (st[i].equals("=")) { //开头是 >=
                  word += st[i];
                  i++;
                }
              }
              list.addNodeToStart(word);
              //show(word);
              //System.out.println(word);
              //word = ""; //清空word

            } else if (st[i].equals(":")) { //开头是双算符 :
              word += st[i];
              i++;
              if (i < str.length()) { //防止下一行的 st[i] 超界
                if (st[i].equals("=")) { //开头是 :=
                  word += st[i];
                  i++;
                }
              }
              list.addNodeToStart(word);
              //show(word);
              //System.out.println(word);
              //word = ""; //清空word

            } else if (isDigit(st[i])) { //开头是数字
              word += st[i];
              i++;
              if (i < str.length()) { //防止下一行的 st[i] 超界
                while (!(isEnd(st[i]) || isOp(st[i]) ||
                         st[i].equals(" ") || st[i].equals("\t"))) {
                  word += st[i];
                  if (i < str.length() - 1) {
                    i++;
                  } else {
                    i++;
                    break; //防止while里的 st[i] 超界
                  }
                }

              }
              list.addNodeToStart(word);
              //word = ""; //清空word

            } else if (isLetter(st[i])) { //开头是字母
              word += st[i];
              i++;
              if (i < str.length()) { //防止下一行的 st[i] 超界
                while (!(isEnd(st[i]) || isOp(st[i]) ||
                         st[i].equals(" ") || st[i].equals("\t"))) {
                  word += st[i];
                  if (i < str.length() - 1) {
                    i++;
                  } else {
                    i++;
                    break; //防止while里的 st[i] 超界
                  }
                }

              }
              list.addNodeToStart(word);
              //System.out.println(word);
              //word = ""; //清空word
            } else {//不能识别的字符
              word += st[i];
              i++;
              if (i < str.length()) { //防止下一行的 st[i] 超界
                while (!(isEnd(st[i]) || isOp(st[i]) ||
                         st[i].equals(" ") || st[i].equals("\t"))) {
                  word += st[i];
                  if (i < str.length() - 1) {
                    i++;
                  } else {
                    i++;
                    break; //防止while里的 st[i] 超界
                  }
                }
              }
            }
            show(word);
            word = "";
          }
          word = "";
          i = 0; //清空i
        }
        no++;
      }
    } catch (FileNotFoundException e) {
      System.out.println("SYS:file not found or couldn't be opened");
    } catch (IOException e) {
      System.out.println("SYS:error");
    }
  }

  public String nextline(RandomAccessFile ips) { //获取下一行
    String line = null;
    try {
      line = ips.readLine();
    } catch (IOException e) {
      System.out.println("SYS:error");
    }
    return line;
  }

  public static void main(String[] args) { //main方法
    CFFC_1 cffc_1 = new CFFC_1();
  }

}

⌨️ 快捷键说明

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