Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.65 KB | None | 0 0
  1. import java.io.FileReader;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class Parser
  7. {
  8.     static List<String> tokens = new ArrayList<>();
  9.     static List<String> stack = new ArrayList<>();
  10.     static int index = 0;
  11.     static int level = 0;
  12.  
  13.     public static void main(String[] args) throws IOException
  14.     {
  15.         readTokens();
  16.         start();
  17.     }
  18.  
  19.     public static void readTokens() throws IOException
  20.     {
  21.         FileReader inputStream;
  22.         inputStream = new FileReader("C:/Users/Arnas/Desktop/Parser/src/output.txt");
  23.         StringBuilder buffer = new StringBuilder("");
  24.         try
  25.         {
  26.             int c;
  27.             char chr;
  28.  
  29.             while ((c = inputStream.read()) != -1) {
  30.                 chr = (char) c;
  31.                 if(chr == '$')
  32.                 {
  33.                     while(!Character.isWhitespace(chr))
  34.                     {
  35.                         buffer.append(chr);
  36.                         c = inputStream.read();
  37.                         chr = (char) c;
  38.                     }
  39.                     if(buffer.toString().equals("$INT") || buffer.toString().equals("$ID") || buffer.toString().equals("$DBLQUOTE") || buffer.toString().equals("$FLOAT"))
  40.                     {
  41.                         do
  42.                         {
  43.                             buffer.append(chr);
  44.                             c = inputStream.read();
  45.                             chr = (char) c;
  46.                         } while(!Character.isWhitespace(chr));
  47.                         if(buffer.toString().equals("$FLOAT float"))
  48.                         {
  49.                             buffer.delete(0, buffer.length());
  50.                             buffer.append("$FLOAT");
  51.                         }
  52.                         else if(buffer.toString().equals("$INT int"))
  53.                         {
  54.                             buffer.delete(0, buffer.length());
  55.                             buffer.append("$INT");
  56.                         }
  57.                     }
  58.                     tokens.add(buffer.toString());
  59.                     buffer.delete(0, buffer.length());
  60.                 }
  61.             }
  62.         }
  63.         finally
  64.         {
  65.             if(inputStream != null)
  66.             {
  67.                 inputStream.close();
  68.             }
  69.         }
  70.     }
  71.  
  72.     public static void start()
  73.     {
  74.         stack.add(level + " <program>");
  75.         level++;
  76.         if(tokens.get(index).equals("$START"))
  77.         {
  78.             stack.add(level + " start");
  79.             mainBlock();
  80.         }
  81.         else
  82.         {
  83.             parserError("keyword start");
  84.         }
  85.     }
  86.  
  87.     public static void mainBlock()
  88.     {
  89.         stack.add(level + " <mainBlock>");
  90.         level++;
  91.         index++;
  92.         if(tokens.get(index).equals("$VOID"))
  93.         {
  94.             index++;
  95.             if(tokens.get(index).equals("$MAIN"))
  96.             {
  97.                 index++;
  98.                 if(tokens.get(index).equals("$LBRACKET1"))
  99.                 {
  100.                     index++;
  101.                     if(tokens.get(index).equals("$RBRACKET1"))
  102.                     {
  103.                         index++;
  104.                         level++;
  105.                         block();
  106.                     }
  107.                     else
  108.                         parserError(")");
  109.                 }
  110.                 else
  111.                     parserError("(");
  112.             }
  113.             else
  114.                 parserError("main");
  115.         }
  116.         else
  117.         {
  118.             level++;
  119.             globals();
  120.         }
  121.     }
  122.  
  123.     public static void globals()
  124.     {
  125.         stack.add(level + " <globals>");
  126.         level++;
  127.         statement_varDeclaration();
  128.     }
  129.  
  130.  
  131.     public static boolean type()
  132.     {
  133.         stack.add(level + " <type>");
  134.         if(tokens.get(index).equals("$INT"))
  135.         {
  136.             index++;
  137.             stack.add(level + " int");
  138.             return true;
  139.         }
  140.         if(tokens.get(index).equals("$FLOAT"))
  141.         {
  142.             index++;
  143.             stack.add(level + " float");
  144.             return true;
  145.         }
  146.         if(tokens.get(index).equals("$STRING"))
  147.         {
  148.             index++;
  149.             stack.add(level + " string");
  150.             return true;
  151.         }
  152.         if(tokens.get(index).equals("$BOOLEAN"))
  153.         {
  154.             index++;
  155.             stack.add(level + " bool");
  156.             return true;
  157.         }
  158.         parserError("variable type");
  159.         return false;
  160.     }
  161.  
  162.     public static boolean variable()
  163.     {
  164.         if(tokens.get(index).substring(0, 2).equals("$ID"))
  165.         {
  166.             stack.add(level + " <variable>");
  167.             level++;
  168.             letters();
  169.             stack.add(level + " " + tokens.get(index).substring(4));
  170.             index++;
  171.             return true;
  172.         }
  173.         else
  174.             return false;
  175.     }
  176.  
  177.     public static void letters()
  178.     {
  179.         stack.add(level + " <letters>");
  180.         level++;
  181.     }
  182.  
  183.     public static void digits()
  184.     {
  185.         stack.add(level + " <digits>");
  186.         level++;
  187.     }
  188.  
  189.     public static boolean expression()
  190.     {
  191.         and_expression();
  192.         level++;
  193.     }
  194.  
  195.     public static boolean and_expression()
  196.     {
  197.         comp_expression();
  198.         level++;
  199.     }
  200.  
  201.     public static boolean comp_expression()
  202.     {
  203.         add_expression();
  204.         level++;
  205.     }
  206.  
  207.     public static boolean add_expression()
  208.     {
  209.         term();
  210.         level++;
  211.     }
  212.  
  213.     public static boolean term()
  214.     {
  215.         factor();
  216.         level++;
  217.     }
  218.  
  219.     public static boolean factor()
  220.     {
  221.         if(tokens.get(index).equals("$LBRACKET1"))
  222.         {
  223.             index++;
  224.             if(expression())
  225.             {
  226.                 if(tokens.get(index).equals("$RBRACKET1"));
  227.                 index++;
  228.             }
  229.             else
  230.                 parserError("expression");
  231.         }
  232.         else if(variable())
  233.             return true;
  234.         else if(constant())
  235.     }
  236.  
  237.     public static boolean constant()
  238.     {
  239.         if(string())
  240.         {
  241.             stack.add(level + " <constant>");
  242.             level++;
  243.             return true;
  244.         }
  245.         else if(number())
  246.     }
  247.  
  248.     public static boolean number()
  249.     {
  250.         stack.add(level + " <number>");
  251.         if(integer())
  252.         {
  253.             return true;
  254.         }
  255.         else if(floate())
  256.         {
  257.             return true;
  258.         }
  259.         stack.remove(stack.size()-1);
  260.         return false;
  261.     }
  262.  
  263.     public static boolean minus()
  264.     {
  265.         if(tokens.get(index).charAt(7) == '-')
  266.         {
  267.             stack.add(level + " <negativeSign>");
  268.             return true;
  269.         }
  270.         else return false;
  271.     }
  272.  
  273.     public static boolean negativeSign()
  274.     {
  275.         if(tokens.get(index).equals("$NEGATIVESIGN"))
  276.         {
  277.             index++;
  278.             stack.add(level + " -");
  279.             return true;
  280.         }
  281.         return false;
  282.     }
  283.  
  284.     public static boolean plus()
  285.     {
  286.         if(tokens.get(index).equals("$PLUS"))
  287.         {
  288.             index++;
  289.             stack.add(level + " +");
  290.             return true;
  291.         }
  292.         return false;
  293.     }
  294.  
  295.     public static boolean floate()
  296.     {
  297.         if(tokens.get(index).substring(0, 5).equals("$FLOAT"))
  298.         {
  299.             level++;
  300.             stack.add(level + " <float>");
  301.             level++;
  302.  
  303.             String token = tokens.get(index);
  304.             String[] parts = token.split(".");
  305.             String part1 = parts[0];
  306.             String part2 = parts[1];
  307.             if(minus())
  308.             {
  309.                 stack.add(level + " <int>");
  310.                 stack.add(level + part1.substring(8));
  311.             }
  312.             else
  313.             {
  314.                 stack.add(level + " <int>");
  315.                 stack.add(level + part1.substring(7));
  316.             }
  317.             stack.add(level + " .");
  318.             digits();
  319.             level--;
  320.             stack.add(level + part2);
  321.  
  322.             level = level - 2;
  323.             index++;
  324.             return true;
  325.         }
  326.         else
  327.             return false;
  328.     }
  329.  
  330.     public static boolean integer()
  331.     {
  332.         if(tokens.get(index).substring(0, 3).equals("$INT"))
  333.         {
  334.             level++;
  335.             if(minus())
  336.             {
  337.                 stack.add(level + " <int>");
  338.                 level++;
  339.                 digits();
  340.                 stack.add(level + " " + tokens.get(index).substring(6));
  341.             }
  342.             else
  343.             {
  344.                 stack.add(level + " <int>");
  345.                 level++;
  346.                 digits();
  347.                 stack.add(level + " " + tokens.get(index).substring(5));
  348.             }
  349.             index++;
  350.             return true;
  351.         }
  352.         else
  353.             return false;
  354.     }
  355.  
  356.     public static boolean string()
  357.     {
  358.         if(tokens.get(index).substring(0, 10).equals("$DBLQUOTE"))
  359.         {
  360.             stack.add(level + " <string>");
  361.             index++;
  362.             level++;
  363.             symbols();
  364.             stack.add(level + tokens.get(index).substring(10));
  365.             return true;
  366.         }
  367.         return false;
  368.     }
  369.  
  370.     public static boolean symbols()
  371.     {
  372.         stack.add(level + " <symbols>");
  373.         return true;
  374.     }
  375.  
  376.     public static boolean not()
  377.     {
  378.         stack.add(level + " <not>");
  379.         if(tokens.get(index).equals("$NOT"))
  380.         {
  381.             index++;
  382.             stack.add(level + " !");
  383.             return true;
  384.         }
  385.         return false;
  386.     }
  387.     public static boolean and()
  388.     {
  389.         stack.add(level + " <and>");
  390.         if(tokens.get(index).equals("$AND"))
  391.         {
  392.             index++;
  393.             stack.add(level + " &&");
  394.             return true;
  395.         }
  396.         return false;
  397.     }
  398.     public static boolean or()
  399.     {
  400.         stack.add(level + " <or>");
  401.         if(tokens.get(index).equals("$OR"))
  402.         {
  403.             index++;
  404.             stack.add(level + " ||");
  405.             return true;
  406.         }
  407.         return false;
  408.     }
  409.     public static boolean comparision() {
  410.         stack.add(level + " <comparison>");
  411.         if (tokens.get(index).equals("$BELOW")) {
  412.             index++;
  413.             stack.add(level + " <");
  414.             return true;
  415.         } else if (tokens.get(index).equals("$BELOWEQUAL")) {
  416.             index++;
  417.             stack.add(level + " <=");
  418.             return true;
  419.         } else if (tokens.get(index).equals("$ABOVE")) {
  420.             index++;
  421.             stack.add(level + " >");
  422.             return true;
  423.         } else if (tokens.get(index).equals("$ABOVEEQUAL")) {
  424.             index++;
  425.             stack.add(level + " >=");
  426.             return true;
  427.         } else if (tokens.get(index).equals("$NOTEQUAL")) {
  428.             index++;
  429.             stack.add(level + " !=");
  430.             return true;
  431.         } else if (tokens.get(index).equals("$EQUAL")) {
  432.             index++;
  433.             stack.add(level + " ==");
  434.             return true;
  435.         } else if (tokens.get(index).equals("$NOTEQUAL")) {
  436.             index++;
  437.             stack.add(level + " !=");
  438.             return true;
  439.         }
  440.     }
  441.     public static boolean addition(){
  442.         stack.add(level + " <addition>");
  443.         if (plus())
  444.             return true;
  445.         else if(negativeSign())
  446.             return true;
  447.         return false;
  448.     }
  449.     public static boolean assigment(){
  450.         stack.add(level + " <assignment>");
  451.         if (tokens.get(index).equals("$ASSIGNMENT")) {
  452.             index++;
  453.             stack.add(level + " =");
  454.             return true;
  455.         } else if (tokens.get(index).equals("$SUBASSIGNMENT")) {
  456.             index++;
  457.             stack.add(level + " -=");
  458.             return true;
  459.         } else if (tokens.get(index).equals("$ADDASSIGNMENT")) {
  460.             index++;
  461.             stack.add(level + " +=");
  462.             return true;
  463.         } else if (tokens.get(index).equals("$MULTASSIGNMENT")) {
  464.             index++;
  465.             stack.add(level + " *=");
  466.             return true;
  467.         }
  468.         return false;
  469.     }
  470.     public static boolean booleanE()
  471.     {
  472.         stack.add(level + " <boolean>");
  473.         if(tokens.get(index).equals("$TRUE")){
  474.             index++;
  475.             stack.add(level + " true" );
  476.             return true;
  477.         }
  478.         else if(tokens.get(index).equals("$FALSE")){
  479.             index++;
  480.             stack.add(level + " false" );
  481.             return true;
  482.         }
  483.         return false;
  484.     }
  485.  
  486.     public static boolean multiplication()
  487.     {
  488.         stack.add(level + " <multiplication>");
  489.         if(tokens.get(index).equals("$MULT"))
  490.         {
  491.             index++;
  492.             stack.add(level + " *" );
  493.             return true;
  494.         }
  495.         else if(tokens.get(index).equals("$DIV"))
  496.         {
  497.             index++;
  498.             stack.add(level + " /" );
  499.             return true;
  500.         }
  501.         return false;
  502.     }
  503.  
  504.     public static void parserError(String errorType)
  505.     {
  506.         System.out.println("Error! " + "'" + errorType + "' expected, not found.");
  507.     }
  508.  
  509.       /* Statements */
  510.  
  511.     public static boolean statements(){
  512.         if( statement() && statements() )
  513.         {
  514.             return true;
  515.         }
  516.         if( statements())
  517.             return true;
  518.  
  519.         return false;
  520.     }
  521.     public static boolean statement(){
  522.         if( simpleStmt() )
  523.         {
  524.             if(tokens.get(index).equals("$SEMICOLON"))
  525.             {
  526.                 index++;
  527.                 stack.add(level + " ;");
  528.                 return true;
  529.             }
  530.         }
  531.         if( statement_return() ) {
  532.  
  533.             if (tokens.get(index).equals("$SEMICOLON")) {
  534.                 index++;
  535.                 stack.add(level + " ;");
  536.                 return true;
  537.             }
  538.         }
  539.         if( functionCall() )
  540.         {
  541.             if(tokens.get(index).equals("$SEMICOLON"))
  542.             {
  543.                 index++;
  544.                 stack.add(level + " ;");
  545.                 return true;
  546.             }
  547.         }
  548.         if( controlStmt())
  549.             return true;
  550.  
  551.  
  552.         return false;
  553.     }
  554.     public static boolean statement_return() {
  555.  
  556.         if(tokens.get(index).equals("$RETURN"))
  557.         {
  558.             index++;
  559.             stack.add(level + " ;");
  560.  
  561.             if( expression() ) {
  562.  
  563.                 return true;
  564.             }
  565.             // Reikia su return 0 kažką bbž padaryt
  566.         }
  567.         return false;
  568.  
  569.     }
  570.     public static boolean functionCall() {
  571.  
  572.         if( functionName() ) {
  573.             if(tokens.get(index).equals("$LBRACKET1")) {
  574.                 index++;
  575.                 stack.add(level + " '('");
  576.  
  577.                 if (tokens.get(index).equals("$RBRACKET1")) {
  578.                     index++;
  579.                     stack.add(level + " ')'");
  580.  
  581.                     return true;
  582.                 } else if (callParameters()) {
  583.                     if (tokens.get(index).equals("$RBRACKET1")) {
  584.                         index++;
  585.                         stack.add(level + " ')'");
  586.                         return true;
  587.                     }
  588.                 }
  589.             }
  590.         }
  591.         return false;
  592.     }
  593.     public static boolean callParameters(){
  594.         if( expression() ){
  595.             if(tokens.get(index).equals("$COMMA")  ){
  596.                 index++;
  597.                 stack.add(level + " ','");
  598.  
  599.                 if( callParameters())
  600.                     return true;
  601.             }
  602.             else
  603.                 return true;
  604.         }
  605.         return false;
  606.     }
  607.  
  608.     public static boolean simpleStmt()
  609.     {
  610.         if( statement_varDeclaration() )
  611.             return true;
  612.         if( statement_assignment( ))
  613.             return true;
  614.         if( io() )
  615.             return true;
  616.  
  617.         return false;
  618.     }
  619.  
  620.     public static boolean statement_varDeclaration()
  621.     {
  622.         if( type() )
  623.         {
  624.             if( variable() )
  625.             {
  626.                 if(tokens.get(index).equals("$SEMICOLON"))
  627.                 {
  628.                     index++;
  629.                     stack.add(level + " ;");
  630.                     return true;
  631.                 }
  632.                 else  if(tokens.get(index).equals("$ASSIGNMENT"))
  633.                 {
  634.                     index++;
  635.                     stack.add(level + " '='");
  636.  
  637.                     if( expression())
  638.                     {
  639.                         if(tokens.get(index).equals("$SEMICOLON"))
  640.                         {
  641.                             index++;
  642.                             stack.add(level + " ;");
  643.                             return true;
  644.                         }
  645.                     }
  646.                 }
  647.             }
  648.         }
  649.         return false;
  650.     }
  651.  
  652.     public static boolean statement_assignment()
  653.     {
  654.         if(variable())
  655.         {
  656.             if( assigment())
  657.             {
  658.                 if( expression())
  659.                     return true;
  660.             }
  661.         }
  662.         return false;
  663.  
  664.     }
  665.     public static boolean io()
  666.     {
  667.         if( input( ))
  668.         {
  669.             return true;
  670.         }
  671.         if( output( ))
  672.         {
  673.             return true;
  674.         }
  675.         return false;
  676.  
  677.     }
  678.     public static boolean input(){
  679.         if(tokens.get(index).equals("$IN"))
  680.         {
  681.             index++;
  682.             stack.add(level + " 'input'");
  683.  
  684.             if( variable() )
  685.                 return true;
  686.  
  687.         }
  688.         return false;
  689.     }
  690.     public static boolean output(){
  691.         if(tokens.get(index).equals("$OUT"))
  692.         {
  693.             index++;
  694.             stack.add(level + " 'echo'");
  695.  
  696.             if( expression( ))
  697.                 return true;
  698.         }
  699.         return false;
  700.     }
  701. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement