Ladies_Man

#COMPLR Lab5 (finite automata) COMPLETE

Apr 19th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.14 KB | None | 0 0
  1. import java.io.File;
  2. import java.util.*;
  3.  
  4. /*
  5. digraph {
  6.         rankdir = LR
  7.         node [shape = doublecircle]; "5_KEY", "6_ID", "7_NUM", "8_OP", "9_WS";
  8.         node [shape = circle]; "1", "2", "3", "4";
  9.         0 [shape = circle]
  10.  
  11.         0 -> "1"                 [label = "d"]
  12.         "1" -> "2"               [label = "e"]
  13.         "2" -> "5_KEY"           [label = "f"]
  14.         0 -> "3"                 [label = "v"]
  15.         "3" -> "4"               [label = "a"]
  16.         "4" -> "5_KEY"           [label = "r"]
  17.         "4" -> "5_KEY"           [label = "l"]
  18.  
  19.         0 -> "6_ID"              [label = "a-z"]
  20.         "6_ID" -> "6_ID"         [label = "a-z0-9"]
  21.  
  22.         0 -> "7_NUM"             [label = "0-9"]
  23.         "7_NUM" -> "7_NUM"       [label = "0-9"]
  24.  
  25.         0 -> "8_OP"              [label = "["]
  26.         0 -> "8_OP"              [label = "]"]
  27.  
  28.         0 -> "9_WS"              [label = "\\n\\r\\t' '"]
  29.         "9_WS" -> "9_WS"         [label = "\\n\\r\\t' '"]
  30. }
  31.  
  32. digraph {
  33.         rankdir = LR
  34.         node [shape = doublecircle]; "1_ID" "2_ID" "3_KEY" "4_ID" "5_ID"
  35.                                      "6_ID" "7_NUM" "8_OP" "9_WS";
  36.         0 [shape = circle]
  37.  
  38.         0 -> "1_ID"            [label = "d"]
  39.         "1_ID" -> "2_ID"       [label = "e"]
  40.         "2_ID" -> "3_KEY"      [label = "f"]
  41.         0 -> "4_ID"            [label = "v"]
  42.         "4_ID" -> "5_ID"       [label = "a"]
  43.         "5_ID" -> "3_KEY"      [label = "r"]
  44.         "5_ID" -> "3_KEY"      [label = "l"]
  45.         0 -> "6_ID"            [label = "a-z\\{d,v}"]
  46.  
  47.         "1_ID" -> "6_ID"       [label = "a-z0-9\\{e}"]
  48.         "2_ID" -> "6_ID"       [label = "a-z0-9\\{f}"]
  49.         "4_ID" -> "6_ID"       [label = "a-z0-9\\{a}"]
  50.         "5_ID" -> "6_ID"       [label = "a-z0-9\\{r,l}"]
  51.         "3_KEY" -> "6_ID"      [label = "a-z0-9"]
  52.  
  53.         "6_ID" -> "6_ID"       [label = "a-z0-9"]
  54.  
  55.         0 -> "7_NUM"           [label = "0-9"]
  56.         "7_NUM" -> "7_NUM"     [label = "0-9"]
  57.  
  58.         0 -> "8_OP"            [label = "["]
  59.         0 -> "8_OP"            [label = "]"]
  60.  
  61.         0 -> "9_WS"            [label = "\\n\\r\\t' '"]
  62.         "9_WS" -> "9_WS"        [label = "\\n\\r\\t' '"]
  63. }
  64. */
  65.  
  66. public class complab5 {
  67.  
  68.     private class Automata {
  69.         private SortedMap<Position, String> messages;
  70.         public String program;
  71.         private Position pos;
  72.         private int state;
  73.  
  74.         public Automata(String program) {
  75.             this.program = program;
  76.             this.pos = new Position(program);
  77.             this.state = 0;
  78.             this.messages = new TreeMap<>();
  79.         }
  80.  
  81.         private int get_code(char c) {
  82.             if (c >= '0' && c <= '9')
  83.                 return 7;
  84.             if (']' == c || '[' == c)
  85.                 return 8;
  86.             if (' ' == c || '\n' == c || '\r' == c || '\t' == c)
  87.                 return 9;
  88.  
  89.             switch (c) {
  90.                 case 'd':
  91.                     return 0;
  92.                 case 'e':
  93.                     return 1;
  94.                 case 'f':
  95.                     return 2;
  96.                 case 'v':
  97.                     return 3;
  98.                 case 'a':
  99.                     return 4;
  100.                 case 'r':
  101.                     return 5;
  102.                 case 'l':
  103.                     return 6;
  104.             }
  105.  
  106.             if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
  107.                 return 10;
  108.  
  109.             return -1;
  110.         }
  111.  
  112.         private String get_state_name(int state) {
  113.             switch (state) {
  114.                 case 1:
  115.                 case 2:
  116.                     return "IDENT";
  117.                 case 3:
  118.                     return "KEYWORD";
  119.                 case 4:
  120.                 case 5:
  121.                 case 6:
  122.                     return "IDENT";
  123.                 case 7:
  124.                     return "NUMBER";
  125.                 case 8:
  126.                     return "OPERATION";
  127.                 case 9:
  128.                     return "WHITESPACE";
  129.                 default:
  130.                     return "ERROR";
  131.             }
  132.         }
  133.  
  134.         boolean err = false;
  135.  
  136.         public void run() {
  137.             System.out.println("\nTokens:");
  138.             while (-1 != pos.getCp()) {
  139.  
  140.                 String word = "";
  141.                 state = 0;
  142.                 boolean final_state = false;
  143.                 Position start = pos.copy();
  144.  
  145.                 while (-1 != pos.getCp()) {
  146.  
  147.                     char curr_char = program.charAt(pos.getIndex());
  148.                     int jump_code = get_code(curr_char);
  149.  
  150.                     if (-1 == jump_code) {
  151.                         if (!err) {
  152.                             messages.put(pos.copy(), "Unexpected characters");
  153.                             err = true;
  154.                         }
  155.                         break;
  156.                     }
  157.                     err = false;
  158.  
  159.                     System.out.print("(" + state + ")->");
  160.                     System.out.print("[" + curr_char + "]->");
  161.  
  162.                     int next_state = table[state][jump_code];
  163.  
  164.                     if (-1 == next_state) {
  165.                         final_state = true;
  166.                         System.out.print("(-1)\n");
  167.                         break;
  168.                     }
  169.  
  170.                     word += curr_char;
  171.                     state = next_state;
  172.                     pos.nxt();
  173.                 }
  174.                 if (final_state) {
  175.  
  176.                     Fragment frag = new Fragment(start, pos);
  177.  
  178.                     System.out.println(get_state_name(state) + " " +
  179.                             frag.toString() + ": " + word.replaceAll("\n"," "));
  180.  
  181.                     continue;
  182.                 }
  183.  
  184.                 pos.nxt();
  185.             }
  186.         }
  187.  
  188.         public void output_messages() {
  189.             System.out.println("\nMessages:");
  190.             for (Map.Entry<Position, String> entry : messages.entrySet()) {
  191.                 System.out.print("ERROR ");
  192.                 System.out.print("(" + entry.getKey().getLine() + ", " +
  193.                         entry.getKey().getPos() + "): ");
  194.                 System.out.println(entry.getValue());
  195.             }
  196.         }
  197.  
  198.     }
  199.  
  200.  
  201.  
  202.     final static int[][] table = {
  203.                 /*  d   e   f   v   a   r   l  num  par  ws  oth*/
  204.     /*  START   */{ 1,  6,  6,  4,  6,  6,  6,  7,   8,   9,  6},
  205.     /*  ID_1    */{ 6,  2,  6,  6,  6,  6,  6,  6,  -1,  -1,  6},
  206.     /*  ID_2    */{ 6,  6,  3,  6,  6,  6,  6,  6,  -1,  -1,  6},
  207.     /*  KEY_3   */{ 6,  6,  6,  6,  6,  6,  6,  6,  -1,  -1,  6},
  208.     /*  ID_4    */{ 6,  6,  6,  6,  5,  6,  6,  6,  -1,  -1,  6},
  209.     /*  ID_5    */{ 6,  6,  6,  6,  6,  3,  3,  6,  -1,  -1,  6},
  210.     /*  ID_6    */{ 6,  6,  6,  6,  6,  6,  6,  6,  -1,  -1,  6},
  211.     /*  NUM_7   */{-1, -1, -1, -1, -1, -1, -1,  7,  -1,  -1, -1},
  212.     /*  OP_8    */{-1, -1, -1, -1, -1, -1, -1, -1,   8,  -1, -1},
  213.     /*  WS_9    */{-1, -1, -1, -1, -1, -1, -1, -1,  -1,   9, -1}
  214.     };
  215.  
  216.     //Var 10
  217.     //Лексические домены:
  218.     // Пробелы - \n, \r, \t, ' '
  219.     // Идентификаторы - непустые последовательности латинских букв и десятичных цифр, начинающиеся с буквы
  220.     // Целочисленные литералы - непустые последовательности десятичных цифр
  221.     // Ключевые слова: def, val, var
  222.     // Знаки операций: [, ]
  223.     //P.S. чтобы не усложнять лексический анализатор,
  224.     // разрешим идентификаторам примыкать справа к целочисленным литералам
  225.  
  226.     public static void main(String []args) {
  227.  
  228.         String text = "";
  229.         Scanner scanner;
  230.  
  231.         try {
  232.             scanner = new Scanner(new File("in.txt"));
  233.         } catch (java.io.FileNotFoundException e) {
  234.             System.out.println(e.toString());
  235.             return;
  236.         }
  237.  
  238.         while (scanner.hasNextLine())
  239.             text += scanner.nextLine() + "\n";
  240.  
  241.         Automata auto = new complab5().new Automata(text);
  242.  
  243.         auto.run();
  244.         auto.output_messages();
  245.  
  246.     }
  247. }
Add Comment
Please, Sign In to add comment