Advertisement
osipyonok

Ira sp1

Mar 7th, 2017
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.34 KB | None | 0 0
  1.     static public LinkedList<Node> language = new LinkedList();
  2.     static public LinkedList<TableNode> lexemaTable = new LinkedList();
  3.     static public int getLexemaCode(String lexema, int lexemaClass) {
  4.         for (TableNode tmp : lexemaTable) {
  5.             if (!tmp.getLexemaText().equals(lexema) || (tmp.getLexemaCode() & -16777216) != lexemaClass) continue; //16777216 = 2^24
  6.             return tmp.getLexemaCode();
  7.         }
  8.         return 0;
  9.     }
  10.  
  11.     static public String getLexemaText(int code) {
  12.         for (TableNode tmp : lexemaTable) {
  13.             if (tmp.getLexemaCode() != code) continue;
  14.             return tmp.getLexemaText();
  15.         }
  16.         return null;
  17.     }
  18.  
  19.     static public void task1(String path) {
  20.         BufferedReader s;
  21.         char[] lexema = new char[256];
  22.         int[] rule = new int[256];
  23.         try {
  24.             s = new BufferedReader(new FileReader(path));
  25.         }
  26.         catch (FileNotFoundException ee) {
  27.             System.out.print("Файл не найден");
  28.             return;
  29.         }
  30.  
  31.         Arrays.fill(rule , 0);
  32.         Arrays.fill(lexema , (char)0);
  33.  
  34.         int pos = 0;
  35.         int type = 0;
  36.         int rule_pos = 0;
  37.         String line = null;
  38.         int read_pos = 0;
  39.         int len = 0;
  40.         try {
  41.             Node node;
  42.             int newLexemaCode;
  43.             TableNode tnode;
  44.             while (s.ready()) {
  45.                 if (line == null || read_pos >= len) {
  46.                     line = s.readLine();
  47.                     if (line.length() > 0 && line.charAt(0) == 65279) {
  48.                         line = line.substring(1);
  49.                     }
  50.                     len = line.length();
  51.                 }
  52.                 start :
  53.                 for (read_pos = 0; read_pos < len; ++read_pos) {
  54.                     char cur = line.charAt(read_pos);
  55.                     String str;
  56.                     boolean flag;
  57.                     switch (type) {
  58.                         case 0: {
  59.                             String ck = " \t\n\n\b";
  60.                             if(ck.indexOf(cur) != -1)continue start;
  61.                             if (read_pos == 0 && rule_pos > 0 && (cur == '!' || cur == '#')) {
  62.                                 node = new Node(rule, rule_pos);
  63.                                 language.add(node);
  64.                                 if (cur == '!') {
  65.                                     rule_pos = 1;
  66.                                     continue start;
  67.                                 }
  68.                                 rule_pos = 0;
  69.                             }
  70.                             lexema[0] = cur;
  71.                             pos = 1;
  72.                             if (cur == '#') {
  73.                                 type = 1;
  74.                                 continue start;
  75.                             }
  76.                             if (cur == '\\') {
  77.                                 --pos;
  78.                                 type = 3;
  79.                                 continue start;
  80.                             }
  81.                             type = 2;
  82.                             continue start;
  83.                         }
  84.                         case 1: {
  85.                             lexema[pos] = cur;
  86.                             ++pos;
  87.                             if (cur != '#' && read_pos != 0) continue start;
  88.                             str = new String(lexema, 0, pos);
  89.                             tnode = new TableNode(str, 0x80000000);
  90.                             flag = true;
  91.                             for (TableNode tmp : lexemaTable) {
  92.                                 if (!tnode.equals(tmp)) continue;
  93.                                 flag = false;
  94.                                 break;
  95.                             }
  96.                             if (flag) {
  97.                                 lexemaTable.add(tnode);
  98.                             }
  99.                             newLexemaCode = getLexemaCode(str, 0x80000000);
  100.                             rule[rule_pos++] = newLexemaCode;
  101.                             pos = 0;
  102.                             type = 0;
  103.                             continue start;
  104.                         }
  105.                         case 2: {
  106.                             if (cur == '\\') {
  107.                                 --pos;
  108.                                 type = 3;
  109.                                 continue start;
  110.                             }
  111.                             if (cur == ' ' || read_pos == 0 || cur == '#' || cur == '\r' || cur == '\t') {
  112.                                 str = new String(lexema, 0, pos);
  113.                                 tnode = new TableNode(str,  0x10000000);
  114.                                 flag = true;
  115.                                 for (TableNode tmp : lexemaTable) {
  116.                                     if (!tnode.equals(tmp)) continue;
  117.                                     flag = false;
  118.                                     break;
  119.                                 }
  120.                                 if (flag) {
  121.                                     lexemaTable.add(tnode);
  122.                                 }
  123.                                 newLexemaCode = getLexemaCode(str,  0x10000000);
  124.                                 rule[rule_pos] = newLexemaCode;
  125.                                 ++rule_pos;
  126.                                 pos = 0;
  127.                                 type = 0;
  128.                                 --read_pos;
  129.                                 continue start;
  130.                             }
  131.                             lexema[pos++] = cur;
  132.                             continue start;
  133.                         }
  134.                         case 3: {
  135.                             lexema[pos++] = cur;
  136.                             type = 2;
  137.                         }
  138.                     }
  139.                 }
  140.             }
  141.             if (pos != 0) {
  142.                 int code = (type == 1 ? 0x80000000 : 0x10000000); // Маска коду не термінала 0x80000000,  маска коду термінала 0x10000000
  143.                 String str = new String(lexema, 0, pos);
  144.                 tnode = new TableNode(str, code);
  145.                 boolean flag = true;
  146.                 for (TableNode tmp : lexemaTable) {
  147.                     if (!tnode.equals(tmp)) continue;
  148.                     flag = false;
  149.                     break;
  150.                 }
  151.                 if (flag) {
  152.                     lexemaTable.add(tnode);
  153.                 }
  154.                 newLexemaCode = getLexemaCode(str, code);
  155.                 rule[rule_pos] = newLexemaCode;
  156.                 ++rule_pos;
  157.             }
  158.             if (rule_pos > 0) {
  159.                 node = new Node(rule, rule_pos);
  160.                 language.add(node);
  161.             }
  162.         }
  163.         catch (IOException e) {
  164.             System.out.println(e.toString());
  165.         }
  166.     }
  167.     public static void printlang(){
  168.         int count = 0;
  169.         for (Node tmp : language) {
  170.             int[] roole = tmp.getRoole();
  171.             System.out.print("" + ++count + " ");
  172.             for (int ii = 0; ii < roole.length; ++ii) {
  173.                 if (ii == 1) {
  174.                     System.out.print(" -> ");
  175.                 }
  176.                 System.out.print(getLexemaText(roole[ii]) + " ");
  177.                 if (roole.length != 1) continue;
  178.                 System.out.print(" -> ");
  179.             }
  180.             System.out.println("");
  181.         }
  182.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement