dark-s0ul

Parser

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