Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.83 KB | None | 0 0
  1. package calculator;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.lang.StringBuilder;
  6.  
  7. public class CalculatorParser {
  8.     public Node tree;
  9.     public static class Node {
  10.         private String name;
  11.         private List<Node> children = new ArrayList<>();
  12.  
  13.         Node(String s) {
  14.             name = s;
  15.         }
  16.  
  17.         public void addChild(Node node) {
  18.             children.add(node);
  19.         }
  20.  
  21.         public String getName() {
  22.             return name;
  23.         }
  24.  
  25.         StringBuilder treeToString(ArrayList<Boolean> mask) {
  26.             StringBuilder sb = new StringBuilder();
  27.             if (!mask.isEmpty()) {
  28.                 sb.append("|__");
  29.             }
  30.             sb.append("'").append(name).append("'").append("\n");
  31.             for (int curChild = 0; curChild < children.size(); ++curChild) {
  32.                 for (boolean b : mask) {
  33.                     if (b) {
  34.                         sb.append("|  ");
  35.                     } else {
  36.                         sb.append("   ");
  37.                     }
  38.                 }
  39.                 mask.add(curChild != children.size() - 1);
  40.                 sb.append(children.get(curChild).treeToString(mask));
  41.                 mask.remove(mask.size() - 1);
  42.             }
  43.             return sb;
  44.         }
  45.     }
  46.  
  47.     public class Node_E extends Node {
  48.         Node_E() {
  49.             super("E");
  50.         }
  51.         public int v;
  52.         public int v;
  53.         public int v;
  54.         public int v;
  55.         public int v;
  56.         public int v;
  57.         public int v;
  58.         public int v;
  59.     }
  60.  
  61.     public class Node_G extends Node {
  62.         Node_G() {
  63.             super("G");
  64.         }
  65.         public int v;
  66.         public int v;
  67.         public int v;
  68.         public int v;
  69.         public int v;
  70.         public int v;
  71.         public int v;
  72.         public int v;
  73.     }
  74.  
  75.     public class Node_T extends Node {
  76.         Node_T() {
  77.             super("T");
  78.         }
  79.         public int v;
  80.         public int v;
  81.         public int v;
  82.         public int v;
  83.         public int v;
  84.         public int v;
  85.         public int v;
  86.         public int v;
  87.     }
  88.  
  89.     public class Node_H extends Node {
  90.         Node_H() {
  91.             super("H");
  92.         }
  93.         public int v;
  94.         public int v;
  95.         public int v;
  96.         public int v;
  97.         public int v;
  98.         public int v;
  99.         public int v;
  100.         public int v;
  101.     }
  102.  
  103.     public class Node_F extends Node {
  104.         Node_F() {
  105.             super("F");
  106.         }
  107.         public int v;
  108.         public int v;
  109.         public int v;
  110.         public int v;
  111.         public int v;
  112.         public int v;
  113.         public int v;
  114.         public int v;
  115.     }
  116.  
  117.     public class Node_PlusOp extends Node {
  118.         Node_PlusOp() {
  119.             super("PlusOp");
  120.         }
  121.         public int v;
  122.         public int v;
  123.         public int v;
  124.         public int v;
  125.         public int v;
  126.         public int v;
  127.         public int v;
  128.         public int v;
  129.     }
  130.  
  131.     public class Node_MinusOp extends Node {
  132.         Node_MinusOp() {
  133.             super("MinusOp");
  134.         }
  135.         public int v;
  136.         public int v;
  137.         public int v;
  138.         public int v;
  139.         public int v;
  140.         public int v;
  141.         public int v;
  142.         public int v;
  143.     }
  144.  
  145.     public class Node_MulOp extends Node {
  146.         Node_MulOp() {
  147.             super("MulOp");
  148.         }
  149.         public int v;
  150.         public int v;
  151.         public int v;
  152.         public int v;
  153.         public int v;
  154.         public int v;
  155.         public int v;
  156.         public int v;
  157.     }
  158.  
  159.     private CalculatorLexicalAnalyzer lexicalAnalyzer;
  160.  
  161.     public CalculatorParser(CalculatorLexicalAnalyzer lexicalAnalyzer) throws Exception {
  162.         this.lexicalAnalyzer = lexicalAnalyzer;
  163.         buildTree();
  164.     }
  165.  
  166.     private void buildTree() throws Exception {
  167.         tree = _E();
  168.         if (lexicalAnalyzer.getCurrentToken() != CalculatorToken._END) {
  169.             throw new Exception("Cur token is " + lexicalAnalyzer.getCurrentToken().toString() + " but expected END.");
  170.         }
  171.     }
  172.  
  173.     public void printTree() {
  174.         System.out.println(tree.treeToString(new ArrayList<>()));
  175.     }
  176.  
  177.     public int getv() {
  178.         return ((Node_E)tree).v;
  179.     }
  180.     public int getv() {
  181.         return ((Node_E)tree).v;
  182.     }
  183.     public int getv() {
  184.         return ((Node_E)tree).v;
  185.     }
  186.     public int getv() {
  187.         return ((Node_E)tree).v;
  188.     }
  189.     public int getv() {
  190.         return ((Node_E)tree).v;
  191.     }
  192.     public int getv() {
  193.         return ((Node_E)tree).v;
  194.     }
  195.     public int getv() {
  196.         return ((Node_E)tree).v;
  197.     }
  198.     public int getv() {
  199.         return ((Node_E)tree).v;
  200.     }
  201.  
  202.     private void consume(CalculatorToken token) throws Exception{
  203.         if (lexicalAnalyzer.getCurrentToken() != token) {
  204.             throw new Exception("Expected another token.");
  205.         }
  206.     }
  207.  
  208.     private Node_E _E(int a, int a, int a, int b, int a, int b, int a, int b) throws Exception {
  209.         Node_E res = new Node_E();
  210.         switch (lexicalAnalyzer.getCurrentToken()) {
  211.             default :
  212.                 throw new Exception("Unexpected token.");
  213.         }
  214.     }
  215.  
  216.     private Node_G _G(int a, int a, int a, int b, int a, int b, int a, int b) throws Exception {
  217.         Node_G res = new Node_G();
  218.         switch (lexicalAnalyzer.getCurrentToken()) {
  219.             default :
  220.                 throw new Exception("Unexpected token.");
  221.         }
  222.     }
  223.  
  224.     private Node_T _T(int a, int a, int a, int b, int a, int b, int a, int b) throws Exception {
  225.         Node_T res = new Node_T();
  226.         switch (lexicalAnalyzer.getCurrentToken()) {
  227.             default :
  228.                 throw new Exception("Unexpected token.");
  229.         }
  230.     }
  231.  
  232.     private Node_H _H(int a, int a, int a, int b, int a, int b, int a, int b) throws Exception {
  233.         Node_H res = new Node_H();
  234.         switch (lexicalAnalyzer.getCurrentToken()) {
  235.             default :
  236.                 throw new Exception("Unexpected token.");
  237.         }
  238.     }
  239.  
  240.     private Node_F _F(int a, int a, int a, int b, int a, int b, int a, int b) throws Exception {
  241.         Node_F res = new Node_F();
  242.         switch (lexicalAnalyzer.getCurrentToken()) {
  243.             default :
  244.                 throw new Exception("Unexpected token.");
  245.         }
  246.     }
  247.  
  248.     private Node_PlusOp _PlusOp(int a, int a, int a, int b, int a, int b, int a, int b) throws Exception {
  249.         Node_PlusOp res = new Node_PlusOp();
  250.         switch (lexicalAnalyzer.getCurrentToken()) {
  251.             default :
  252.                 throw new Exception("Unexpected token.");
  253.         }
  254.     }
  255.  
  256.     private Node_MinusOp _MinusOp(int a, int a, int a, int b, int a, int b, int a, int b) throws Exception {
  257.         Node_MinusOp res = new Node_MinusOp();
  258.         switch (lexicalAnalyzer.getCurrentToken()) {
  259.             default :
  260.                 throw new Exception("Unexpected token.");
  261.         }
  262.     }
  263.  
  264.     private Node_MulOp _MulOp(int a, int a, int a, int b, int a, int b, int a, int b) throws Exception {
  265.         Node_MulOp res = new Node_MulOp();
  266.         switch (lexicalAnalyzer.getCurrentToken()) {
  267.             default :
  268.                 throw new Exception("Unexpected token.");
  269.         }
  270.     }
  271.  
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement