Advertisement
dark-s0ul

Parser

May 21st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.86 KB | None | 0 0
  1. package Parser;
  2.  
  3. import Lexer.Lexem;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Stack;
  8.  
  9. /**
  10.  * Created by Vanyko on 09.05.18.
  11.  */
  12. public class Parser {
  13.     ArrayList<Lexem> lexems;
  14.  
  15.     public Parser(ArrayList<Lexem> lexems) {
  16.         this.lexems = lexems;
  17.         analyze();
  18.     }
  19.  
  20.     public class MatchIdent implements MatchCallback {
  21.         @Override
  22.         public boolean match(int code) {
  23.             return code > 1000;
  24.         }
  25.     }
  26.  
  27.     public class MatchUint implements MatchCallback {
  28.         @Override
  29.         public boolean match(int code) {
  30.             return (501 <= code) && (code <= 1000);
  31.         }
  32.     }
  33.  
  34.     private State[] states = {
  35.             new State(  0,        "-<signal-program>",              -1,  null,  0,  0 ),
  36.             new State(  1,               "-<program>",             401,  null,  0,  0 ),
  37.             new State(  2,                         "",             -59,  null, -1,  0 ),
  38.             new State(  3,                         "",               1,  null,  0,  0 ),
  39.             new State(  4,                         "",              -6,  null, -1,  0 ),
  40.             new State(  5,                         "",               4,  null,  1,  0 ),
  41.             new State(  6,                 "-<block>",              -7,  null,  0,  0 ),
  42.  
  43.             new State(  7,                         "",             -11,  null, -1, -1 ),
  44.  
  45.             new State(  8,                         "",             403,  null,  0,  0 ),
  46.             new State(  9,                         "",             -36,  null, -1,  0 ),
  47.             new State( 10,                         "",             404,  null,  0,  0 ),
  48.             new State( 11,  "<variable-declarations>",             402,  null,  0,  0 ),
  49.             new State( 12,                         "",             -13,  null,  0,  0 ),
  50.             new State( 13,     "-<declarations_list>",             -14,  null,  0,  0 ),
  51.             new State( 14,                         "",             -16,  null, -1,  0 ),
  52.             new State( 15,                         "",             -14,  null,  0,  0 ),
  53.             new State( 16,           "-<declaration>",             -17,  null,  0,  0 ),
  54.             new State( 17,                         "",             -58,  null, -1,  0 ),
  55.             new State( 18,                         "",               0,  null,  0,  0 ),
  56.             new State( 19,                         "",             -21,  null, -1, -1 ),
  57.  
  58.             new State( 20,                         "",               1,  null,  1,  0 ),
  59.             new State( 21,       "-<attributes_list>",             -22,  null,  0,  0 ),
  60.             new State( 22,                         "",             -24,  null,  1,  0 ),
  61.             new State( 23,                         "",             -22,  null,  0,  0 ),
  62.  
  63.             new State( 24,             "-<attribute>",             -25,  null,  0,  0 ),
  64.             new State( 25,                         "",             -30, null,   0, -1 ),
  65.             new State( 26,                         "",             -31, null,   0, -1 ),
  66.  
  67.             new State( 27,                         "",               2,  null,  0,  1 ),
  68.             new State( 28,                         "",             -32,  null, -1,  0 ),
  69.             new State( 29,                         "",               3,  null,  1,  0 ),
  70.  
  71.             new State( 30,                     "<int>",             407, null,   1,  1 ),
  72.             new State( 31,                   "<float>",             408, null,   1,  1 ),
  73.  
  74.  
  75.  
  76.             new State( 32,                 "-<range>",             -33,  null,  0,  0 ),
  77.             new State( 33,                         "",             -61,  null, -1,  0 ),
  78.             new State( 34,                         "",             301,  null,  0,  0 ),
  79.             new State( 35,                         "",             -61,  null,  0,  0 ),
  80.             new State( 36,       "-<statements_list>",             -37,  null,  0,  0 ),
  81.  
  82.             new State( 37,                         "",             -39,  null,  -1,  0 ),
  83.             new State( 38,                         "",             -37,  null,  0,  0 ),
  84.             new State( 39,             "-<statement>",             -40,  null,  0,  0 ),
  85.             new State( 40,                         "",             -45,  null,  0,  -1 ),
  86.             new State( 41,                         "",             -52,  null, -1,  -1 ),
  87.             new State( 42,                         "",             302,  null,  0,  0 ),
  88.             new State( 43,                         "",             -49,  null, -1,  0 ),
  89.             new State( 44,                         "",               1,  null,  1,  0 ),
  90.  
  91.             new State( 45,                         "",             405,  null,  0,  1 ),
  92.             new State( 46,                         "",             -36,  null,  0,  0 ),
  93.             new State( 47,                         "",             406,  null,  0,  0 ),
  94.             new State( 48,                         "",               1,  null,  1,  0 ),
  95.  
  96.             new State( 49,            "-<expression>",             -50,  null,  0,  0 ),
  97.             new State( 50,                         "",             -52,  null, -1, -1 ),
  98.             new State( 51,                         "",             -61,  null,  0,  0 ),
  99.             new State( 52,              "-<variable>",             -53,  null,  0,  0 ),
  100.             new State( 53,                         "",             -58,  null, -1,  0 ),
  101.             new State( 54,                         "",             -55,  null,  0,  0 ),
  102.             new State( 55,             "-<dimension>",               2,  null,  0,  1 ),
  103.             new State( 56,                         "",             -49,  null, -1,  0 ),
  104.             new State( 57,                         "",               3,  null,  1,  0 ),
  105.             new State( 58,   "-<variable_identifier>",             -60,  null,  0,  0 ),
  106.             new State( 59,  "-<procedure_identifier>",             -60,  null,  0,  0 ),
  107.             new State( 60,            "-<identifier>",               0,  new MatchIdent(),  1,  1 ),
  108.             new State( 61,      "-<unsigned_integer>",               0,  new MatchUint(),  1,  1 )
  109.     };
  110.  
  111.     public class Tree {
  112.         Tree root;
  113.         String name;
  114.         List<Tree> nodes;
  115.  
  116.         Tree(Tree root, String name) {
  117.             this.root = root;
  118.             this.name = name;
  119.             this.nodes = new ArrayList<>();
  120.         }
  121.     };
  122.  
  123.     void PrintTree(Tree root, int prefix) {
  124.         System.out.println(String.format("%" + prefix +"c%s", '\0', root.name));
  125.  
  126.         for (Tree node : root.nodes) {
  127.             PrintTree(node, prefix + 4);
  128.         }
  129.     }
  130.  
  131.     public void analyze() {
  132.         Stack<State> sp = new Stack<State>();
  133.         int prefix = 1, ip = 0, lexemPointer = 0;
  134.  
  135.         Tree root = new Tree(null, "<root>");
  136.  
  137.         do {
  138.             State stateToPush = getStateByIp(ip);
  139.             if (stateToPush == null) return;
  140.  
  141.             sp.push(stateToPush);
  142. //            System.out.println(sp.toString());
  143.  
  144.             if (!stateToPush.buffer.isEmpty()) {
  145.                 Tree node = new Tree(root, sp.peek().buffer);
  146.                 PrintTree(node, (prefix += 4) - 4);
  147.                 ;
  148.                 root.nodes.add(node);
  149.                 root = node;
  150.             }
  151.  
  152.             if (sp.peek().code < 0) {
  153.                 ip = -sp.peek().code;
  154.                 continue;
  155.             }
  156.  
  157.             boolean accept = sp.peek().accept != null ? sp.peek().accept.match(lexems.get(lexemPointer).getCode()) : (lexems.get(lexemPointer).getCode() == sp.peek().code);
  158.  
  159.             if (!accept && (sp.peek().af != 1)) {
  160.                 System.out.println(String.format("error: %d -> %s\n", sp.peek().ip, lexems.get(lexemPointer).getValue()));
  161.                 return;
  162.             }
  163.  
  164.             Tree node = new Tree(root, accept ? lexems.get(lexemPointer).getValue() : "empty");
  165.             root.nodes.add(node);
  166.             PrintTree(node, prefix);
  167.  
  168.             if (accept) {
  169.                 lexemPointer += 1;
  170.             }
  171.  
  172.             if ((accept ? sp.peek().at : sp.peek().af) == 1) {
  173.                 while (!sp.empty() && ((accept ? sp.peek().at : sp.peek().af) != -1)) {
  174.                     if (!sp.peek().buffer.isEmpty()) {
  175.                         root = root.root;
  176.                         prefix -= 4;
  177.                     }
  178.                     sp.pop();
  179.                 }
  180.             }
  181.  
  182.             if (!sp.empty()) {
  183.                 ip = sp.peek().ip + 1;
  184.                 if (sp.peek().buffer.isEmpty()) {
  185.                     sp.pop();
  186.                 }
  187.             }
  188.         } while (!sp.empty());
  189.  
  190.         PrintTree(root, 0);
  191.     }
  192.  
  193.     State getStateByIp(int ip) {
  194.         for (State state : states) {
  195.             if (state.ip == ip) return state;
  196.         }
  197.         return null;
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement