Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- public class Parser
- {
- static List<String> tokens = new ArrayList<>();
- static List<String> stack = new ArrayList<>();
- static int index = 0;
- static int level = 0;
- public static void main(String[] args) throws IOException
- {
- readTokens();
- start();
- }
- public static void readTokens() throws IOException
- {
- FileReader inputStream;
- inputStream = new FileReader("C:/Users/Arnas/Desktop/Parser/src/output.txt");
- StringBuilder buffer = new StringBuilder("");
- try
- {
- int c;
- char chr;
- while ((c = inputStream.read()) != -1) {
- chr = (char) c;
- if(chr == '$')
- {
- while(!Character.isWhitespace(chr))
- {
- buffer.append(chr);
- c = inputStream.read();
- chr = (char) c;
- }
- if(buffer.toString().equals("$INT") || buffer.toString().equals("$ID") || buffer.toString().equals("$DBLQUOTE") || buffer.toString().equals("$FLOAT"))
- {
- do
- {
- buffer.append(chr);
- c = inputStream.read();
- chr = (char) c;
- } while(!Character.isWhitespace(chr));
- if(buffer.toString().equals("$FLOAT float"))
- {
- buffer.delete(0, buffer.length());
- buffer.append("$FLOAT");
- }
- else if(buffer.toString().equals("$INT int"))
- {
- buffer.delete(0, buffer.length());
- buffer.append("$INT");
- }
- }
- tokens.add(buffer.toString());
- buffer.delete(0, buffer.length());
- }
- }
- }
- finally
- {
- if(inputStream != null)
- {
- inputStream.close();
- }
- }
- }
- public static void start()
- {
- stack.add(level + " <program>");
- level++;
- if(tokens.get(index).equals("$START"))
- {
- stack.add(level + " start");
- mainBlock();
- }
- else
- {
- parserError("keyword start");
- }
- }
- public static void mainBlock()
- {
- stack.add(level + " <mainBlock>");
- level++;
- index++;
- if(tokens.get(index).equals("$VOID"))
- {
- index++;
- if(tokens.get(index).equals("$MAIN"))
- {
- index++;
- if(tokens.get(index).equals("$LBRACKET1"))
- {
- index++;
- if(tokens.get(index).equals("$RBRACKET1"))
- {
- index++;
- level++;
- block();
- }
- else
- parserError(")");
- }
- else
- parserError("(");
- }
- else
- parserError("main");
- }
- else
- {
- level++;
- globals();
- }
- }
- public static void globals()
- {
- stack.add(level + " <globals>");
- level++;
- statement_varDeclaration();
- }
- public static boolean type()
- {
- stack.add(level + " <type>");
- if(tokens.get(index).equals("$INT"))
- {
- index++;
- stack.add(level + " int");
- return true;
- }
- if(tokens.get(index).equals("$FLOAT"))
- {
- index++;
- stack.add(level + " float");
- return true;
- }
- if(tokens.get(index).equals("$STRING"))
- {
- index++;
- stack.add(level + " string");
- return true;
- }
- if(tokens.get(index).equals("$BOOLEAN"))
- {
- index++;
- stack.add(level + " bool");
- return true;
- }
- parserError("variable type");
- return false;
- }
- public static boolean variable()
- {
- if(tokens.get(index).substring(0, 2).equals("$ID"))
- {
- stack.add(level + " <variable>");
- level++;
- letters();
- stack.add(level + " " + tokens.get(index).substring(4));
- index++;
- return true;
- }
- else
- return false;
- }
- public static void letters()
- {
- stack.add(level + " <letters>");
- level++;
- }
- public static void digits()
- {
- stack.add(level + " <digits>");
- level++;
- }
- public static boolean expression()
- {
- and_expression();
- level++;
- }
- public static boolean and_expression()
- {
- comp_expression();
- level++;
- }
- public static boolean comp_expression()
- {
- add_expression();
- level++;
- }
- public static boolean add_expression()
- {
- term();
- level++;
- }
- public static boolean term()
- {
- factor();
- level++;
- }
- public static boolean factor()
- {
- if(tokens.get(index).equals("$LBRACKET1"))
- {
- index++;
- if(expression())
- {
- if(tokens.get(index).equals("$RBRACKET1"));
- index++;
- }
- else
- parserError("expression");
- }
- else if(variable())
- return true;
- else if(constant())
- }
- public static boolean constant()
- {
- if(string())
- {
- stack.add(level + " <constant>");
- level++;
- return true;
- }
- else if(number())
- }
- public static boolean number()
- {
- stack.add(level + " <number>");
- if(integer())
- {
- return true;
- }
- else if(floate())
- {
- return true;
- }
- stack.remove(stack.size()-1);
- return false;
- }
- public static boolean minus()
- {
- if(tokens.get(index).charAt(7) == '-')
- {
- stack.add(level + " <negativeSign>");
- return true;
- }
- else return false;
- }
- public static boolean negativeSign()
- {
- if(tokens.get(index).equals("$NEGATIVESIGN"))
- {
- index++;
- stack.add(level + " -");
- return true;
- }
- return false;
- }
- public static boolean plus()
- {
- if(tokens.get(index).equals("$PLUS"))
- {
- index++;
- stack.add(level + " +");
- return true;
- }
- return false;
- }
- public static boolean floate()
- {
- if(tokens.get(index).substring(0, 5).equals("$FLOAT"))
- {
- level++;
- stack.add(level + " <float>");
- level++;
- String token = tokens.get(index);
- String[] parts = token.split(".");
- String part1 = parts[0];
- String part2 = parts[1];
- if(minus())
- {
- stack.add(level + " <int>");
- stack.add(level + part1.substring(8));
- }
- else
- {
- stack.add(level + " <int>");
- stack.add(level + part1.substring(7));
- }
- stack.add(level + " .");
- digits();
- level--;
- stack.add(level + part2);
- level = level - 2;
- index++;
- return true;
- }
- else
- return false;
- }
- public static boolean integer()
- {
- if(tokens.get(index).substring(0, 3).equals("$INT"))
- {
- level++;
- if(minus())
- {
- stack.add(level + " <int>");
- level++;
- digits();
- stack.add(level + " " + tokens.get(index).substring(6));
- }
- else
- {
- stack.add(level + " <int>");
- level++;
- digits();
- stack.add(level + " " + tokens.get(index).substring(5));
- }
- index++;
- return true;
- }
- else
- return false;
- }
- public static boolean string()
- {
- if(tokens.get(index).substring(0, 10).equals("$DBLQUOTE"))
- {
- stack.add(level + " <string>");
- index++;
- level++;
- symbols();
- stack.add(level + tokens.get(index).substring(10));
- return true;
- }
- return false;
- }
- public static boolean symbols()
- {
- stack.add(level + " <symbols>");
- return true;
- }
- public static boolean not()
- {
- stack.add(level + " <not>");
- if(tokens.get(index).equals("$NOT"))
- {
- index++;
- stack.add(level + " !");
- return true;
- }
- return false;
- }
- public static boolean and()
- {
- stack.add(level + " <and>");
- if(tokens.get(index).equals("$AND"))
- {
- index++;
- stack.add(level + " &&");
- return true;
- }
- return false;
- }
- public static boolean or()
- {
- stack.add(level + " <or>");
- if(tokens.get(index).equals("$OR"))
- {
- index++;
- stack.add(level + " ||");
- return true;
- }
- return false;
- }
- public static boolean comparision() {
- stack.add(level + " <comparison>");
- if (tokens.get(index).equals("$BELOW")) {
- index++;
- stack.add(level + " <");
- return true;
- } else if (tokens.get(index).equals("$BELOWEQUAL")) {
- index++;
- stack.add(level + " <=");
- return true;
- } else if (tokens.get(index).equals("$ABOVE")) {
- index++;
- stack.add(level + " >");
- return true;
- } else if (tokens.get(index).equals("$ABOVEEQUAL")) {
- index++;
- stack.add(level + " >=");
- return true;
- } else if (tokens.get(index).equals("$NOTEQUAL")) {
- index++;
- stack.add(level + " !=");
- return true;
- } else if (tokens.get(index).equals("$EQUAL")) {
- index++;
- stack.add(level + " ==");
- return true;
- } else if (tokens.get(index).equals("$NOTEQUAL")) {
- index++;
- stack.add(level + " !=");
- return true;
- }
- }
- public static boolean addition(){
- stack.add(level + " <addition>");
- if (plus())
- return true;
- else if(negativeSign())
- return true;
- return false;
- }
- public static boolean assigment(){
- stack.add(level + " <assignment>");
- if (tokens.get(index).equals("$ASSIGNMENT")) {
- index++;
- stack.add(level + " =");
- return true;
- } else if (tokens.get(index).equals("$SUBASSIGNMENT")) {
- index++;
- stack.add(level + " -=");
- return true;
- } else if (tokens.get(index).equals("$ADDASSIGNMENT")) {
- index++;
- stack.add(level + " +=");
- return true;
- } else if (tokens.get(index).equals("$MULTASSIGNMENT")) {
- index++;
- stack.add(level + " *=");
- return true;
- }
- return false;
- }
- public static boolean booleanE()
- {
- stack.add(level + " <boolean>");
- if(tokens.get(index).equals("$TRUE")){
- index++;
- stack.add(level + " true" );
- return true;
- }
- else if(tokens.get(index).equals("$FALSE")){
- index++;
- stack.add(level + " false" );
- return true;
- }
- return false;
- }
- public static boolean multiplication()
- {
- stack.add(level + " <multiplication>");
- if(tokens.get(index).equals("$MULT"))
- {
- index++;
- stack.add(level + " *" );
- return true;
- }
- else if(tokens.get(index).equals("$DIV"))
- {
- index++;
- stack.add(level + " /" );
- return true;
- }
- return false;
- }
- public static void parserError(String errorType)
- {
- System.out.println("Error! " + "'" + errorType + "' expected, not found.");
- }
- /* Statements */
- public static boolean statements(){
- if( statement() && statements() )
- {
- return true;
- }
- if( statements())
- return true;
- return false;
- }
- public static boolean statement(){
- if( simpleStmt() )
- {
- if(tokens.get(index).equals("$SEMICOLON"))
- {
- index++;
- stack.add(level + " ;");
- return true;
- }
- }
- if( statement_return() ) {
- if (tokens.get(index).equals("$SEMICOLON")) {
- index++;
- stack.add(level + " ;");
- return true;
- }
- }
- if( functionCall() )
- {
- if(tokens.get(index).equals("$SEMICOLON"))
- {
- index++;
- stack.add(level + " ;");
- return true;
- }
- }
- if( controlStmt())
- return true;
- return false;
- }
- public static boolean statement_return() {
- if(tokens.get(index).equals("$RETURN"))
- {
- index++;
- stack.add(level + " ;");
- if( expression() ) {
- return true;
- }
- // Reikia su return 0 kažką bbž padaryt
- }
- return false;
- }
- public static boolean functionCall() {
- if( functionName() ) {
- if(tokens.get(index).equals("$LBRACKET1")) {
- index++;
- stack.add(level + " '('");
- if (tokens.get(index).equals("$RBRACKET1")) {
- index++;
- stack.add(level + " ')'");
- return true;
- } else if (callParameters()) {
- if (tokens.get(index).equals("$RBRACKET1")) {
- index++;
- stack.add(level + " ')'");
- return true;
- }
- }
- }
- }
- return false;
- }
- public static boolean callParameters(){
- if( expression() ){
- if(tokens.get(index).equals("$COMMA") ){
- index++;
- stack.add(level + " ','");
- if( callParameters())
- return true;
- }
- else
- return true;
- }
- return false;
- }
- public static boolean simpleStmt()
- {
- if( statement_varDeclaration() )
- return true;
- if( statement_assignment( ))
- return true;
- if( io() )
- return true;
- return false;
- }
- public static boolean statement_varDeclaration()
- {
- if( type() )
- {
- if( variable() )
- {
- if(tokens.get(index).equals("$SEMICOLON"))
- {
- index++;
- stack.add(level + " ;");
- return true;
- }
- else if(tokens.get(index).equals("$ASSIGNMENT"))
- {
- index++;
- stack.add(level + " '='");
- if( expression())
- {
- if(tokens.get(index).equals("$SEMICOLON"))
- {
- index++;
- stack.add(level + " ;");
- return true;
- }
- }
- }
- }
- }
- return false;
- }
- public static boolean statement_assignment()
- {
- if(variable())
- {
- if( assigment())
- {
- if( expression())
- return true;
- }
- }
- return false;
- }
- public static boolean io()
- {
- if( input( ))
- {
- return true;
- }
- if( output( ))
- {
- return true;
- }
- return false;
- }
- public static boolean input(){
- if(tokens.get(index).equals("$IN"))
- {
- index++;
- stack.add(level + " 'input'");
- if( variable() )
- return true;
- }
- return false;
- }
- public static boolean output(){
- if(tokens.get(index).equals("$OUT"))
- {
- index++;
- stack.add(level + " 'echo'");
- if( expression( ))
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement