diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..757e06b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Tokenizer", + "request": "launch", + "mainClass": "Tokenizer", + "projectName": "JavaCompiler_2ce0abb5" + }, + { + "type": "java", + "name": "Tokenizer", + "request": "launch", + "mainClass": "token.Tokenizer", + "projectName": "" + }, + { + "type": "java", + "name": "Current File", + "request": "launch", + "mainClass": "${file}" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..25749dd --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "cmake.configureOnOpen": true, + "java.debug.settings.onBuildFailureProceed": true +} \ No newline at end of file diff --git a/.vscode/token/Calculator.java b/.vscode/token/Calculator.java new file mode 100644 index 0000000..e2566fd --- /dev/null +++ b/.vscode/token/Calculator.java @@ -0,0 +1,25 @@ +public class Calculator { + public double evaluate(Node node) + { + if(node==null) + { + return 0; + } + if(node.left==null&&node.right==null) + { + return Double.parseDouble(node.data); + } + double leftValue=evaluate(node.left); + double rightValue=evaluate(node.right); + switch(node.data) + { + case "+": return leftValue+rightValue; + case "-": return leftValue-rightValue; + case "*": return leftValue*rightValue; + case "/":if(rightValue==0){throw new UnsupportedOperationException("not divide by zero");} + return leftValue/rightValue; + default:throw new IllegalArgumentException("unkown operator"+node.data); + } + } + +} diff --git a/.vscode/token/Checker.java b/.vscode/token/Checker.java new file mode 100644 index 0000000..7d341e6 --- /dev/null +++ b/.vscode/token/Checker.java @@ -0,0 +1,60 @@ +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class Checker { + public void checkassignmentorder(ArrayListTokens,ArrayListVariables) throws Exception { + for (Token token1 : Tokens) { + for (Token token2 : Variables) { + if ((token1.getValue().equals(token2.getValue()) && token1.line < token2.line)) { + //System.out.println("Token: " + token1.getValue() + " found on different lines. Line in Tokens list: " + token1.line + ", Line in Assigned Variables list: " + token2.line); + throw new Exception("Token: " + token1.getValue() + " used before its assignment. Used in line: " + token1.line + ", assigned in line: " + token2.line+"\nInvalid variable use, check for unassigned or undeclared variables."); + } + } + } + } + // boolean checkassignmentexists(ArrayListTokens,ArrayListVariables)throws Exception + // { + // // + // List identifierTokens = Tokens.stream().filter(token -> token.getType() == TokenType.IDENTIFIER).distinct().collect(Collectors.toList()); + // Set K = new HashSet<>(); + // List uniqueline=Tokens.stream().filter(token -> token.getType() == TokenType.IDENTIFIER).distinct().collect(Collectors.toList()).stream().filter(Token-> K.add(Token.getValue())).collect(Collectors.toList()); + // //List identifierTokens2 = Tokens.stream().filter(token -> token.getType() == TokenType.IDENTIFIER).map(token->token.tokenValue).distinct().collect(Collectors.toList()); + // for(Token yes:uniqueline)System.out.println(yes.getValue()+" "+yes.getType()+" "+yes.line); + + + // // for(Token tokens: uniqueline) + // // {int found=0; + // // for(Token vars: Variables) + // // { + // // if (vars.getValue().equals(tokens.getValue()))found=1; + // // } + // // if(found==0) + // // throw new Exception("Used variable unassigned");return false; + // // } + // for(int i =0;i Tokens, ArrayList Variables) throws Exception { + List identifierTokens = Tokens.stream().filter(token -> token.getType() == TokenType.IDENTIFIER).distinct().collect(Collectors.toList()); + Set uniqueIdentifiers = new HashSet<>(); + List uniqueLineTokens = identifierTokens.stream().filter(token -> uniqueIdentifiers.add(token.getValue())).collect(Collectors.toList()); + // for (Token yes : uniqueLineTokens) {System.out.print(yes.getValue() + " " + yes.getType() + " " + yes.line+" ");}System.out.println(""); + // for (Token no:Variables){System.out.print(no.getValue() + " " + no.getType() + " " + no.line+" ");} + for (Token token : uniqueLineTokens) { + boolean found = Variables.stream().anyMatch(var -> var.getValue().equals(token.getValue())); + if (!found) {throw new Exception("Used variable unassigned: " + token.getValue());} + } + + return true; + } +} + diff --git a/.vscode/token/Compiler.java b/.vscode/token/Compiler.java new file mode 100644 index 0000000..b5cb213 --- /dev/null +++ b/.vscode/token/Compiler.java @@ -0,0 +1,75 @@ +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +public class Compiler { + ArrayList Token; + Compiler(ArrayList Tokens) + { + Token=Tokens; + } + String Complete(ArrayList Token) + { + StringBuilder L=new StringBuilder(); + for(int i=0;i="; + case DONE: return "}"; + case LESS: return "<"; + case THEN: return "){"; + case MORE: return ">"; + case GO: return ";"; + default: return "INVALID"; + } + } +} diff --git a/.vscode/token/DataType.java b/.vscode/token/DataType.java new file mode 100644 index 0000000..3ee3066 --- /dev/null +++ b/.vscode/token/DataType.java @@ -0,0 +1,9 @@ + + +public enum DataType +{ + INTEGER, + CHAR, + FLOAT, + DOUBLE, +} diff --git a/.vscode/token/Evaluator.java b/.vscode/token/Evaluator.java new file mode 100644 index 0000000..5434db3 --- /dev/null +++ b/.vscode/token/Evaluator.java @@ -0,0 +1,128 @@ +import java.util.Stack; +import java.util.StringTokenizer; + +public class Evaluator { + + // Method to evaluate an infix expression + public int evaluate(String expression) { + String postfix = infixToPostfix(expression); + return evaluatePostfix(postfix); + } + + // Method to convert an infix expression to a postfix expression + private String infixToPostfix(String infix) { + StringBuilder postfix = new StringBuilder(); + Stack stack = new Stack<>(); + + StringTokenizer tokenizer = new StringTokenizer(infix, "+-*/() ", true); + + while (tokenizer.hasMoreTokens()) { + String token = tokenizer.nextToken().trim(); + if (token.isEmpty()) { + continue; + } + + if (isNumber(token)) { + postfix.append(token).append(' '); + } else if (token.equals("(")) { + stack.push(token); + } else if (token.equals(")")) { + while (!stack.isEmpty() && !stack.peek().equals("(")) { + postfix.append(stack.pop()).append(' '); + } + stack.pop(); // Pop '(' from the stack + } else if (isOperator(token)) { + while (!stack.isEmpty() && precedence(token) <= precedence(stack.peek())) { + postfix.append(stack.pop()).append(' '); + } + stack.push(token); + } + } + + while (!stack.isEmpty()) { + postfix.append(stack.pop()).append(' '); + } + + return postfix.toString().trim(); + } + + // Method to evaluate a postfix expression + private int evaluatePostfix(String postfix) { + Stack stack = new Stack<>(); + + StringTokenizer tokenizer = new StringTokenizer(postfix); + + while (tokenizer.hasMoreTokens()) { + String token = tokenizer.nextToken(); + + if (isOperator(token)) { + int b = stack.pop(); + int a = stack.pop(); + switch (token) { + case "+": + case "Plus": + case "plus": + stack.push(a + b); + break; + case "-":case "Minus":case "minus": + stack.push(a - b); + break; + case "*":case "Times":case "times": + stack.push(a * b); + break; + case "/":case "By":case "by": + stack.push(a / b); + break; + } + } else { + stack.push(Integer.parseInt(token)); + } + } + + return stack.pop(); + } + + // Method to check if a string is a number + private boolean isNumber(String token) { + try { + Integer.parseInt(token); + return true; + } catch (NumberFormatException e) { + return false; + } + } + + public boolean isOperator(String input) { + return (input.equals("Equals") || input.equals("-") || input.equals("+") || input.equals("*") || input.equals("/") + || input.equals("Plus") || input.equals("Minus") || input.equals("Times") || input.equals("=") || input.equals("By") || input.equals("Is") || input.equals("is")); + } + + // Method to get precedence of operators + private int precedence(String token) { + switch (token) { + case "+": + case "-": + case "Plus": + case "plus": + return 1; + case "*": + case "/": + return 2; + default: + return -1; + } + } + + // Main method to test the Evaluator + public static void main(String[] args) { + Evaluator evaluator = new Evaluator(); + + String expression1 = "18 + 4 * 5"; + String expression2 = "18 Plus 4 * 5"; + String expression3 = "18 plus 4 * 5"; + + System.out.println("Expression: " + expression1 + " = " + evaluator.evaluate(expression1)); + System.out.println("Expression: " + expression2 + " = " + evaluator.evaluate(expression2)); + System.out.println("Expression: " + expression3 + " = " + evaluator.evaluate(expression3)); + } +} diff --git a/ExpressionTree.java b/.vscode/token/ExpressionTree.java similarity index 95% rename from ExpressionTree.java rename to .vscode/token/ExpressionTree.java index be26584..18a16e7 100644 --- a/ExpressionTree.java +++ b/.vscode/token/ExpressionTree.java @@ -1,91 +1,91 @@ - -package token; - -import java.util.ArrayList; -import java.util.Stack; - - -public class ExpressionTree { - Node root; - - public ExpressionTree() - { - root=null; - } - public void addNode(String data) - { - root=addNodeRec(root,data); - } - private Node addNodeRec(Node current,String data) - { - if(current==null) - { - return new Node(data); - } - return current; - } - public void traverseInOrder(Node node) - { - if(node !=null) - { - traverseInOrder(node.left); - System.out.print(node.data+" "); - traverseInOrder(node.right); - } - } - private int getPriority(String operator) - { - switch(operator) - { - case "+": - case "-": - return 1; - case "*": - case"/": - return 2; - default: - return -1; - } - } - public void buildTree(ArrayList tokens) { - Stack nodes = new Stack<>(); - Stack operators = new Stack<>(); - - for (Token token : tokens) { - if (token.getType() == TokenType.NUMBER) { - nodes.push(new Node(token.getValue())); - } else if (token.getType() == TokenType.OPERATOR) { - while (!operators.isEmpty() && getPriority(operators.peek()) >= getPriority(token.getValue())) { - if (nodes.size() < 2) { - throw new IllegalArgumentException("number not correct"); - } - Node right = nodes.pop(); - Node left = nodes.pop(); - Node opNode = new Node(operators.pop()); - opNode.left = left; - opNode.right = right; - nodes.push(opNode); - } - operators.push(token.getValue()); - } - } - - while (!operators.isEmpty()) { - if (nodes.size() < 2) { - throw new IllegalArgumentException("number not correct "); - } - Node right = nodes.pop(); - Node left = nodes.pop(); - Node opNode = new Node(operators.pop()); - opNode.left = left; - opNode.right = right; - nodes.push(opNode); - } - - if (!nodes.isEmpty()) { - root = nodes.pop(); - } else { - throw new IllegalArgumentException("tree nor correct"); - } -} + + + +import java.util.ArrayList; +import java.util.Stack; + + +public class ExpressionTree { + Node root; + + public ExpressionTree() + { + root=null; + } + public void addNode(String data) + { + root=addNodeRec(root,data); + } + private Node addNodeRec(Node current,String data) + { + if(current==null) + { + return new Node(data); + } + return current; + } + public void traverseInOrder(Node node) + { + if(node !=null) + { + traverseInOrder(node.left); + System.out.print(node.data+" "); + traverseInOrder(node.right); + } + } + private int getPriority(String operator) + { + switch(operator) + { + case "+": + case "-": + return 1; + case "*": + case"/": + return 2; + default: + return -1; + } + } + public void buildTree(ArrayList tokens) { + Stack nodes = new Stack<>(); + Stack operators = new Stack<>(); + + for (Token token : tokens) { + if (token.getType() == TokenType.NUMBER) { + nodes.push(new Node(token.getValue())); + } else if (token.getType() == TokenType.OPERATOR) { + while (!operators.isEmpty() && getPriority(operators.peek()) >= getPriority(token.getValue())) { + if (nodes.size() < 2) { + throw new IllegalArgumentException("number not correct"); + } + Node right = nodes.pop(); + Node left = nodes.pop(); + Node opNode = new Node(operators.pop()); + opNode.left = left; + opNode.right = right; + nodes.push(opNode); + } + operators.push(token.getValue()); + } + } + + while (!operators.isEmpty()) { + if (nodes.size() < 2) { + throw new IllegalArgumentException("number not correct "); + } + Node right = nodes.pop(); + Node left = nodes.pop(); + Node opNode = new Node(operators.pop()); + opNode.left = left; + opNode.right = right; + nodes.push(opNode); + } + + if (!nodes.isEmpty()) { + root = nodes.pop(); + } else { + throw new IllegalArgumentException("tree nor correct"); + } +} } \ No newline at end of file diff --git a/.vscode/token/Input.txt b/.vscode/token/Input.txt new file mode 100644 index 0000000..8b6b155 --- /dev/null +++ b/.vscode/token/Input.txt @@ -0,0 +1 @@ +Integer X is 18 Minus 8 GO diff --git a/LineType.java b/.vscode/token/LineType.java similarity index 86% rename from LineType.java rename to .vscode/token/LineType.java index 1c5b9fc..e15102e 100644 --- a/LineType.java +++ b/.vscode/token/LineType.java @@ -1,17 +1,19 @@ -/* - * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license - * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template - */ -package token; - -/** - * - * @author B268 - */ -public enum LineType -{ - CONDITION, - ASSIGNMENT, - PRINT, - INVALID, -} +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ + + +/** + * + * @author B268 + */ +public enum LineType +{ + ASSIGNMENT_NO_DECLARATION, + CONDITION, + ASSIGNMENT, + PRINT, + DECLARATION, + INVALID, +} diff --git a/Node.java b/.vscode/token/Node.java similarity index 84% rename from Node.java rename to .vscode/token/Node.java index be390ea..3814338 100644 --- a/Node.java +++ b/.vscode/token/Node.java @@ -1,16 +1,14 @@ - -package token; - - -public class Node -{ - String data; - Node left; - Node right; - Node(String data) - { - this.data=data; - this.left=null; - this.right=right; - } -} + + +public class Node +{ + String data; + Node left; + Node right; + Node(String data) + { + this.data=data; + this.left=null; + this.right=right; + } +} diff --git a/Output.txt b/.vscode/token/Output.txt similarity index 100% rename from Output.txt rename to .vscode/token/Output.txt diff --git a/.vscode/token/Token.java b/.vscode/token/Token.java new file mode 100644 index 0000000..c8ed141 --- /dev/null +++ b/.vscode/token/Token.java @@ -0,0 +1,46 @@ + + +public class Token +{ + String tokenValue; + TokenType tokenType,stype1,stype2,stype3; + int line; + Token(String tokenValue,TokenType tokenType) + { + this.tokenValue=tokenValue; + this.tokenType=tokenType; + } + Token(String tokenValue,TokenType tokenType,int line) + { + this.tokenValue=tokenValue; + this.tokenType=tokenType; + this.line=line; + } + Token(String tokenValue,TokenType tokenType,int line,TokenType stype1) + { + this.tokenValue=tokenValue; + this.tokenType=tokenType; + this.stype1=stype1;this.line=line; + } + Token(String tokenValue,TokenType tokenType,int line,TokenType stype1,TokenType stype2) + { + this.tokenValue=tokenValue; + this.tokenType=tokenType;this.stype1=stype1;this.stype2=stype2;this.line=line; + } + Token(String tokenValue,TokenType tokenType,int line,TokenType stype1,TokenType stype2,TokenType stype3) + { + this.tokenValue=tokenValue; + this.tokenType=tokenType; + this.stype1=stype1; + this.stype1=stype2; + this.stype1=stype3;this.line=line; + } + public TokenType getType() + { + return tokenType; + } + public String getValue() + { + return tokenValue; + } +} diff --git a/.vscode/token/TokenType.java b/.vscode/token/TokenType.java new file mode 100644 index 0000000..eff695c --- /dev/null +++ b/.vscode/token/TokenType.java @@ -0,0 +1,38 @@ + + + +public enum TokenType +{ + IDENTIFIER,///variable name + DATATYPE, + NUMBER, + OPERATOR,//+ - / * + ADD, + SUB, + TIMES, + BY, + WHITESPACE, + INTEGER, + CHAR, + CHARACTER, + EQUALSCHECK, + FLOAT, + DOUBLE, + LONG, + STRING, + INVALID, + PRINT, + PRINTED, + IF, + CONDITION, + AND, + OR, + EQUALS, + EQUALSLESS, + EQUALSMORE, + DONE, + LESS, + THEN, + MORE, + GO, +} diff --git a/.vscode/token/Tokenizer.java b/.vscode/token/Tokenizer.java new file mode 100644 index 0000000..83da64c --- /dev/null +++ b/.vscode/token/Tokenizer.java @@ -0,0 +1,678 @@ +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Collections; + +public class Tokenizer { + public static void main(String[] args) throws Exception { + + String filePath = ".vscode\\token\\Input.txt"; + String testt = ""; + try { + testt = new String(Files.readAllBytes(Paths.get(filePath))); + } catch (IOException e) { + e.printStackTrace(); + } + Tokenizer tokenizer = new Tokenizer(); + ArrayList listoftoken = null,listofVariables = null,assigned=null; + try { + listoftoken = tokenizer.tokenize(testt); + assigned = tokenizer.getassignedvariables(testt); + listofVariables = tokenizer.assignedvariables(testt); + for (Token token : listoftoken) { + System.out.println(token.getValue() + ":" + token.getType() + ":" + token.line + "\n"); + } + for (Token token2 : assigned) { + //System.out.println(token2.getValue() + ":" + token2.getType() + ":" + token2.line + "\n"+"AAAAAAAAAAAAAAAAAAAAAAAAAAA"); + } + for (Token token : listofVariables) { + System.out.println(token.getValue() + ":" + token.getType() + ":" + token.line + "\n"); + } + } catch (IOException e) { + System.out.println("ERROR"); + } + Checker Z=new Checker(); + Z.checkassignmentexists(listoftoken,listofVariables); + Z.checkassignmentorder(listoftoken,listofVariables); + Compiler Z2=new Compiler(listoftoken); + System.out.println(Z2.Complete()); + + } + + public static boolean isNumeric(String str) { + for (char c : str.toCharArray()) { + if (!Character.isDigit(c)) return false; + } + return true; + } + + boolean declared_mathematical_expression(String[] exp) { + // StringBuilder Z=new StringBuilder(); + // for (String L: exp) + // { + // Z.append(L+" "); + // } + // System.err.println(Z); + for (int i = 3; i < exp.length; i++) { + if ((i % 2 == 0 && (isNumeric(exp[i]) || isValididentifier(exp[i]) || (i % 2 != 0 && isOperator(exp[i]))))) { + return false; + } + } + return true;} + + boolean assigned_mathematical_expression(String[] exp) { + StringBuilder Z=new StringBuilder(); + for (String L: exp) + { + Z.append(L+" "); + } + System.err.println(Z); + for (int i = 2; i < exp.length; i++) { + if ((i % 2 != 0 && (isNumeric(exp[i]) || isValididentifier(exp[i]) || (i % 2 == 0 && isOperator(exp[i]))))) { + return false; + } + } + return true;} + boolean assigned_mathematical_expressionNUMBERS(String[] exp) { + StringBuilder Z=new StringBuilder(); + for (String L: exp) + { + Z.append(L+" "); + } + System.err.println(Z); + for (int i = 2; i < exp.length; i++) { + if ((i % 2 != 0 && (isNumeric(exp[i])) || (i % 2 == 0 && isOperator(exp[i])))) { + return false; + } + } + return true;} + boolean declared_mathematical_expressionNUMBERS(String[] exp) { + // StringBuilder Z=new StringBuilder(); + // for (String L: exp) + // { + // Z.append(L+" "); + // } + // System.err.println(Z); + for (int i = 3; i < exp.length; i++) { + if ((i % 2 == 0 && (isNumeric(exp[i])) || (i % 2 != 0 && isOperator(exp[i])))){ + return false; + } + } + return true;} + + + boolean isvalidchar(String[] exp) { + if (exp.length == 2 && nextTokenType(exp[0]) == TokenType.CHAR && isValididentifier(exp[1])) return true; + else if (exp.length == 4 && nextTokenType(exp[0]) == TokenType.CHAR && isValididentifier(exp[1]) && isequal(exp[2]) && exp[3].length() == 1) return true; + return false; + } + + boolean isvalidcond(String[] GO) { + int s=0,found=0; + for(int i =0;i 2 && (s-1) % 2 == 0) {for (int i = 1; i < s; i++) { + if ((i % 2 != 0 && (!isValididentifier(GO[i]) && !isNumeric(GO[i]))) || (i % 2 == 0 && (!isCondition(GO[i])))||!GO[GO.length-1].equals("done")) return false;} + return true; + } else return false; + } + else return false; + } + int thenlocation(String[] GO) { + for(int i=0;i isvalidthen(String[] GO,int line) { + int then_location=0,done_location=0;StringBuilder result = new StringBuilder(); + while(!GO[then_location++].equals("then")); + while(!GO[++done_location].equals("done")); + for(int i=then_location;i<=done_location;i++) + {result.append(GO[i]+" ");} + //result.append("GO"); + //System.out.println(result); + String input=result.toString(); + //System.out.println(input+"AAAAAAAAAAAAAAAAAAA"); + Tokenizer thenOutput=new Tokenizer(); + ArrayList IfResult=null; + try{IfResult=thenOutput.tokenizeIf(input,line);} + catch(IOException e){System.out.println("ERROR");} + Collections.swap(IfResult, IfResult.size()-1, IfResult.size()-2); + return IfResult; + // int s=0,found=0; + // for(int i =0;i thenoutput=Tokenizer.tokenize(); + } + + boolean isvalidInt(String[] str) { + if (isValididentifier(str[1]) && isequal(str[2]) && declared_mathematical_expression(str)) + return true; + else return false; + } + + public boolean isDataType(TokenType input) { + if (input == TokenType.INTEGER || input == TokenType.DOUBLE || input == TokenType.LONG || input == TokenType.CHAR || input == TokenType.STRING) return true; + return false; + } + + boolean isequal(String input) { + if (input.equals("=") || input.equals("Equals") || input.equals("equals") || input.equals("Is") || input.equals("is")) + return true; + return false; + } + TokenType isequalcheck(String input) { + if (input.equals("=") || input.equals("Equals") || input.equals("equals") || input.equals("Is") || input.equals("is")) + return TokenType.EQUALSCHECK; + return TokenType.INVALID; + } + + public boolean isOperator(String input) { + return (input.equals("Equals") || input.equals("-") || input.equals("+") || input.equals("*") || input.equals("/") + || input.equals("Plus") || input.equals("Minus") || input.equals("Times") || input.equals("=") || input.equals("By") || input.equals("Is") || input.equals("is")); + } + + public boolean isValididentifier(String input) { + if (nextTokenType(input) == TokenType.IDENTIFIER) return true; + return false; + } + + boolean isPrintStatement(String input) { + if (input.equals("Print") || input.equals("Print Line")) return true; + return false; + } + + boolean isCondition(String input) { + if (input.equals("Or") || input.equals("And") || input.equals("and") || input.equals("or") || input.equals("Equals") || input.equals("equals") || input.equals("EQLess") || input.equals("EQMore") || input.equals("MoreThan") || input.equals("LessThan")||input.equals("is")||input.equals("Is")) + return true; + return false; + } + + public TokenType nextTokenType(String input) { + if (input.equals("Integer")) return TokenType.INTEGER; + else if (input.equals("String")) return TokenType.STRING; + else if (input.equals("Character")) return TokenType.CHAR; + else if (input.equals("Double")) return TokenType.DOUBLE; + else if (input.equals("Long")) return TokenType.LONG; + else if (isNumeric(input)) return TokenType.NUMBER; + else if(input.equals("then"))return TokenType.THEN; + else if (input.equals("If")) return TokenType.IF; + else if (input.equals("Print") || input.equals("Print Line")) return TokenType.PRINT; + //else if (isOperator(input)) return TokenType.OPERATOR; + else if(input.equals("Plus")||input.equals("plus")||input.equals("+"))return TokenType.ADD; + else if(input.equals("Minus")||input.equals("minus")||input.equals("-"))return TokenType.SUB; + else if(input.equals("Times")||input.equals("times")||input.equals("*"))return TokenType.TIMES; + else if(input.equals("By")||input.equals("by")||input.equals("/"))return TokenType.BY; + else if(input.equals("done"))return TokenType.DONE; + else if (input.equals("Or") || input.equals("or")) return TokenType.OR; + else if (input.equals("And") || input.equals("and")) return TokenType.AND; + else if (input.equals("Equals") || input.equals("equals")||input.equals("is") || input.equals("Is")||input.equals("=")) return TokenType.EQUALS; + else if (input.equals("EQLess")) return TokenType.EQUALSLESS; + else if (input.equals("LessThan")) return TokenType.LESS; + else if (input.equals("EQMore")) return TokenType.EQUALSMORE; + else if (input.equals("MoreThan")) return TokenType.MORE; + else if (!Character.isDigit(input.charAt(0))) return TokenType.IDENTIFIER; + else return TokenType.INVALID; + } + + public ArrayList tokenize(String input) throws IOException { + ArrayList result = new ArrayList<>(); + String[] tokens2 = input.trim().split("GO"); + int linenum = 0; + try { + if (!input.contains("GO")) throw new Exception("ERROR! Missing GO"); + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + for (String token : tokens2) { + linenum++; + //System.out.println("\n" + token + "\n"); + String[] GO = token.trim().split(" "); + try { + for (int i = 0; i < GO.length; i++) { + String token2 = GO[i]; + LineType k2; + //System.out.println(GO[i]); + if (nextTokenType(GO[0]) == TokenType.IF) k2 = LineType.CONDITION; + else if (isDataType(nextTokenType(GO[0])) && GO.length > 2) k2 = LineType.ASSIGNMENT; + else if (isDataType(nextTokenType(GO[0])) && GO.length == 2) k2 = LineType.DECLARATION; + else if (nextTokenType(GO[0]) == TokenType.PRINT) k2 = LineType.PRINT; + else if(nextTokenType(GO[0])==TokenType.IDENTIFIER)k2=LineType.ASSIGNMENT_NO_DECLARATION; + else k2 = LineType.INVALID; + if (k2 == LineType.ASSIGNMENT) { + if (nextTokenType(GO[0]) == TokenType.CHAR && !isvalidchar(GO)) { + throw new Exception("ERROR! Declared token type should be CHARACTER"); + } else if (nextTokenType(GO[0]) == TokenType.CHAR && isvalidchar(GO) && GO.length > 2) { + if (token2.equals(GO[3])) { + result.add(new Token(token2, TokenType.CHARACTER, linenum)); + result.add(new Token("GO", TokenType.GO, linenum)); + } else result.add(new Token(token2, nextTokenType(token2), linenum)); + } else if (nextTokenType(GO[0]) == TokenType.INTEGER && !isvalidInt(GO)) { + throw new Exception("ERROR! Declared Integer assignment invalid"); + } else if (nextTokenType(GO[0]) == TokenType.LONG && !isvalidInt(GO)) { + throw new Exception("ERROR! Declared LONG assignment invalid"); + } else if (nextTokenType(token2) == TokenType.INVALID) { + throw new Exception("ERROR! INVALID TOKEN DETECTED"); + } else if(nextTokenType(GO[0])==TokenType.STRING&&i==GO.length-1){result.add(new Token(token2, TokenType.PRINTED,linenum)); + result.add(new Token("GO", TokenType.GO,linenum));}else if(nextTokenType(GO[0])==TokenType.STRING&&i>2){result.add(new Token(token2, TokenType.PRINTED,linenum));} + else { + if (i == GO.length - 1) { + result.add(new Token(token2, nextTokenType(token2), linenum)); + result.add(new Token("GO", TokenType.GO, linenum)); + } else { + if(nextTokenType(GO[0])==TokenType.INTEGER&&declared_mathematical_expressionNUMBERS(GO)&&i>2) + {StringBuilder k=new StringBuilder(); for(int h=3;h restofIF = isvalidthen(GO,linenum); + for (int j = 0; j < restofIF.size(); j++) {result.add(restofIF.get(j));}//if(j==restofIF.size()-1){{result.add(new Token("done", TokenType.DONE,linenum));}} + break; + } + } else if (k2 == LineType.PRINT) { + if (GO.length < 2) throw new Exception("ERROR! INVALID PRINT STATEMENT"); + else if (i != 0) result.add(new Token(token2, TokenType.PRINTED, linenum)); + else result.add(new Token(token2, nextTokenType(token2), linenum)); + } else { + throw new Exception("Wrong or Invalid line type."); + } + } + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } + return result; + } + public ArrayList getassignedvariables(String input) throws IOException { + ArrayList result = new ArrayList<>(); + String[] tokens2 = input.trim().split("GO"); + int linenum = 0; + try { + if (!input.contains("GO")) throw new Exception("ERROR! Missing GO"); + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + for (String token : tokens2) { + linenum++; + //System.out.println("\n" + token + "\n"); + String[] GO = token.trim().split(" "); + try { + for (int i = 0; i < GO.length; i++) { + String token2 = GO[i]; + LineType k2; + //System.out.println(GO[i]); + if (nextTokenType(GO[0]) == TokenType.IF) k2 = LineType.CONDITION; + else if (isDataType(nextTokenType(GO[0])) && GO.length > 2) k2 = LineType.ASSIGNMENT; + else if (isDataType(nextTokenType(GO[0])) && GO.length == 2) k2 = LineType.DECLARATION; + else if (nextTokenType(GO[0]) == TokenType.PRINT) k2 = LineType.PRINT; + else if(nextTokenType(GO[0])==TokenType.IDENTIFIER)k2=LineType.ASSIGNMENT_NO_DECLARATION; + else k2 = LineType.INVALID; + if (k2 == LineType.ASSIGNMENT) { + // if (nextTokenType(GO[0]) == TokenType.CHAR && !isvalidchar(GO)) { + // throw new Exception("ERROR! Declared token type should be CHARACTER"); + // } else if (nextTokenType(GO[0]) == TokenType.CHAR && isvalidchar(GO) && GO.length > 2) { + // if (token2.equals(GO[3])) { + // result.add(new Token(token2, TokenType.CHARACTER, linenum)); + // result.add(new Token("GO", TokenType.GO, linenum)); + // } else result.add(new Token(token2, nextTokenType(token2), linenum)); + // } else if (nextTokenType(GO[0]) == TokenType.INTEGER && !isvalidInt(GO)) { + // throw new Exception("ERROR! Declared Integer assignment invalid"); + // } else if (nextTokenType(GO[0]) == TokenType.LONG && !isvalidInt(GO)) { + // throw new Exception("ERROR! Declared LONG assignment invalid"); + // } else if (nextTokenType(token2) == TokenType.INVALID) { + // throw new Exception("ERROR! INVALID TOKEN DETECTED"); + // } else if(nextTokenType(GO[0])==TokenType.STRING&&i>2){result.add(new Token(token2, TokenType.PRINTED,linenum));} + + // else { + // if (i == GO.length - 1) { + // result.add(new Token(token2, nextTokenType(token2), linenum)); + // result.add(new Token("GO", TokenType.GO, linenum)); + // } else { + // result.add(new Token(token2, nextTokenType(token2), linenum)); + // } + // } + } + else if(k2==LineType.ASSIGNMENT_NO_DECLARATION) + { + if(!isValididentifier(GO[0])||!isequal(GO[1])){throw new Exception("ERROR! INVALID ASSIGNMENT");} + else + { + if(token==GO[0]){ + if(GO.length==3) + { + if(GO[2].length()==1) + { + if(isNumeric(GO[2]))result.add(new Token(token2,nextTokenType(token2),linenum,TokenType.INTEGER,TokenType.STRING,TokenType.CHAR)); + else result.add(new Token(token2,nextTokenType(token2),linenum,TokenType.STRING,TokenType.CHAR)); + } + else + { + if(isNumeric(GO[2]))result.add(new Token(token2,nextTokenType(token2),linenum,TokenType.INTEGER,TokenType.STRING)); + else result.add(new Token(token2,nextTokenType(token2),linenum,TokenType.STRING)); + } + } + else if(GO.length>3) + { + if(assigned_mathematical_expression(GO))result.add(new Token(token2, nextTokenType(token2),linenum,TokenType.INTEGER,TokenType.STRING)); + else result.add(new Token(token2, nextTokenType(token2),linenum,TokenType.STRING)); + + } + } + } + } + else if (k2 == LineType.DECLARATION) { + // if (isDataType(nextTokenType(GO[0])) && isValididentifier(GO[1])) { + // if (i == GO.length - 1) { + // result.add(new Token(token2, nextTokenType(token2), linenum)); + // result.add(new Token("GO", TokenType.GO, linenum)); + // } else { + // result.add(new Token(token2, nextTokenType(token2), linenum)); + // } + // } + } else if (k2 == LineType.CONDITION) { + // int then=thenlocation(GO); + // if (!isvalidcond(GO)) {throw new Exception("ERROR! INVALID CONDITION STATEMENT");} + // else if (i <= then) { + // if(isequal(token2))result.add(new Token(token2,TokenType.EQUALSCHECK,linenum)); + // else result.add(new Token(token2, nextTokenType(token2), linenum)); + // //result.add(new Token("GO", TokenType.GO, linenum)); + // } + // else { + // ArrayList restofIF = isvalidthen(GO,linenum); + // for (int j = 0; j < restofIF.size(); j++) { + // //if(j==restofIF.size()-1){{result.add(new Token("done", TokenType.DONE,linenum));}} + // result.add(restofIF.get(j)); + // } + // } + } + else if (k2 == LineType.PRINT) { + // if (GO.length < 2) throw new Exception("ERROR! INVALID PRINT STATEMENT"); + // else if (i != 0) result.add(new Token(token2, TokenType.PRINTED, linenum)); + // else result.add(new Token(token2, nextTokenType(token2), linenum)); + } else { + //throw new Exception("Wrong or Invalid line type."); + } + } + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } + return result; + } + public ArrayList tokenizeIf(String input,int Line) throws IOException { + ArrayList result = new ArrayList<>(); + String[] tokens2 = input.trim().split("GO"); + int linenum = 0; + // try { + // if (!input.contains("GO")) throw new Exception("ERROR! Missing GO"); + // } catch (Exception e) { + // e.printStackTrace(); + // System.exit(1); + // } + for (String token : tokens2) { + linenum++; + //System.out.println("\n" + token + "\n"); + String[] GO = token.trim().split(" "); + try { + for (int i = 0; i < GO.length; i++) { + String token2 = GO[i]; + LineType k2; + //System.out.println(GO[i]); + if (nextTokenType(GO[0]) == TokenType.IF) k2 = LineType.CONDITION; + else if (isDataType(nextTokenType(GO[0])) && GO.length > 2) k2 = LineType.ASSIGNMENT; + else if (isDataType(nextTokenType(GO[0])) && GO.length == 2) k2 = LineType.DECLARATION; + else if (nextTokenType(GO[0]) == TokenType.PRINT) k2 = LineType.PRINT; + else if(nextTokenType(GO[0])==TokenType.IDENTIFIER)k2=LineType.ASSIGNMENT_NO_DECLARATION; + else k2 = LineType.INVALID; + + if (k2 == LineType.ASSIGNMENT) { + if (nextTokenType(GO[0]) == TokenType.CHAR && !isvalidchar(GO)) { + throw new Exception("ERROR! Declared token type should be CHARACTER"); + } + else if (nextTokenType(GO[0]) == TokenType.CHAR && isvalidchar(GO) && GO.length > 2) { + if (token2.equals(GO[3])) {result.add(new Token(token2, TokenType.CHARACTER, Line));result.add(new Token("GO", TokenType.GO, Line));} + else result.add(new Token(token2, nextTokenType(token2), Line));} + + else if (nextTokenType(GO[0]) == TokenType.INTEGER && !isvalidInt(GO)) {throw new Exception("ERROR! Declared Integer assignment invalid");} + else if (nextTokenType(GO[0]) == TokenType.LONG && !isvalidInt(GO)) {throw new Exception("ERROR! Declared LONG assignment invalid");} + else if (nextTokenType(token2) == TokenType.INVALID) {throw new Exception("ERROR! INVALID TOKEN DETECTED");} + else if(nextTokenType(GO[0])==TokenType.STRING&&i>2){result.add(new Token(token2, TokenType.PRINTED));} + else { + if (i == GO.length - 1) { + result.add(new Token(token2, nextTokenType(token2), Line)); + result.add(new Token("GO", TokenType.GO, Line)); + } else { + result.add(new Token(token2, nextTokenType(token2), Line)); + } + } + } else if (k2 == LineType.DECLARATION) { + if (isDataType(nextTokenType(GO[0])) && isValididentifier(GO[1])) { + if (i == GO.length - 1) { + result.add(new Token(token2, nextTokenType(token2), Line)); + result.add(new Token("GO", TokenType.GO, Line)); + } else { + result.add(new Token(token2, nextTokenType(token2), Line)); + } + } + } + else if (k2 == LineType.CONDITION) {int then=thenlocation(GO); + if (!isvalidcond(GO)) {throw new Exception("ERROR! INVALID CONDITION STATEMENT");} + else if (i <= then) { + if(isequal(token2))result.add(new Token(token2,TokenType.EQUALSCHECK,linenum)); + else result.add(new Token(token2, nextTokenType(token2), Line)); + //result.add(new Token("GO", TokenType.GO, Line)); + } + else { + ArrayList restofIF = isvalidthen(GO,Line); + for (int j = 0; j < restofIF.size(); j++) { + result.add(restofIF.get(j)); + } + } + } else if (k2 == LineType.PRINT) { + if (GO.length < 2) throw new Exception("ERROR! INVALID PRINT STATEMENT"); + else if (i != 0) result.add(new Token(token2, TokenType.PRINTED, Line)); + else result.add(new Token(token2, nextTokenType(token2), Line)); + } else { + throw new Exception("Wrong or Invalid line type."); + } + } + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } + //System.out.println("RESULT OF TOKENIZE IFFFFFF"); + for (Token token : result) { + //System.out.println(token.getValue() + ":" + token.getType() + ":" + token.line + "\n"); + } + return result; + } + public ArrayList tokenizeIf2(String input,int line) throws IOException { + ArrayList result = new ArrayList<>(); + String[] tokens2 = input.trim().split("GO"); + int linenum = 0; + try { + if (!input.contains("GO")) throw new Exception("ERROR! Missing GO"); + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + for (String token : tokens2) { + linenum++; + //System.out.println("\n" + token + "\n"); + String[] GO = token.trim().split(" "); + try { + for (int i = 0; i < GO.length; i++) { + String token2 = GO[i]; + LineType k2; + //System.out.println(GO[i]); + if (nextTokenType(GO[0]) == TokenType.IF) k2 = LineType.CONDITION; + else if (isDataType(nextTokenType(GO[0])) && GO.length > 2) k2 = LineType.ASSIGNMENT; + else if (isDataType(nextTokenType(GO[0])) && GO.length == 2) k2 = LineType.DECLARATION; + else if (nextTokenType(GO[0]) == TokenType.PRINT) k2 = LineType.PRINT; + else if(nextTokenType(GO[0])==TokenType.IDENTIFIER)k2=LineType.ASSIGNMENT_NO_DECLARATION; + else k2 = LineType.INVALID; + if (k2 == LineType.ASSIGNMENT) { + if (nextTokenType(GO[0]) == TokenType.CHAR && !isvalidchar(GO)) { + throw new Exception("ERROR! Declared token type should be CHARACTER"); + } else if (nextTokenType(GO[0]) == TokenType.CHAR && isvalidchar(GO) && GO.length > 2) { + if (token2.equals(GO[3])) { + result.add(new Token(token2, TokenType.CHARACTER, line)); + result.add(new Token("GO", TokenType.GO, line)); + } else result.add(new Token(token2, nextTokenType(token2), line)); + } else if (nextTokenType(GO[0]) == TokenType.INTEGER && !isvalidInt(GO)) { + throw new Exception("ERROR! Declared Integer assignment invalid"); + } else if (nextTokenType(GO[0]) == TokenType.LONG && !isvalidInt(GO)) { + throw new Exception("ERROR! Declared LONG assignment invalid"); + } else if (nextTokenType(token2) == TokenType.INVALID) { + throw new Exception("ERROR! INVALID TOKEN DETECTED"); + } else if(nextTokenType(GO[0])==TokenType.STRING&&i>2){result.add(new Token(token2, TokenType.PRINTED,linenum));} + + else { + if (i == GO.length - 1) { + result.add(new Token(token2, nextTokenType(token2), line)); + result.add(new Token("GO", TokenType.GO, line)); + } else { + result.add(new Token(token2, nextTokenType(token2), line)); + } + } + } + else if(k2==LineType.ASSIGNMENT_NO_DECLARATION) + { + if(!isValididentifier(GO[0])||!isequal(GO[1])){throw new Exception("ERROR! INVALID ASSIGNMENT");} + else + { + result.add(new Token(token2,nextTokenType(token2),linenum)); + } + } + else if (k2 == LineType.DECLARATION) { + if (isDataType(nextTokenType(GO[0])) && isValididentifier(GO[1])) { + if (i == GO.length - 1) { + result.add(new Token(token2, nextTokenType(token2), line)); + result.add(new Token("GO", TokenType.GO, line)); + } else { + result.add(new Token(token2, nextTokenType(token2), line)); + } + } + } else if (k2 == LineType.CONDITION) {int then=thenlocation(GO); + if (!isvalidcond(GO)) {throw new Exception("ERROR! INVALID CONDITION STATEMENT");} + else if (i <= then) { + if(isequal(token2))result.add(new Token(token2,TokenType.EQUALSCHECK,linenum)); + else result.add(new Token(token2, nextTokenType(token2), line)); + //result.add(new Token("GO", TokenType.GO, line)); + } + else { + ArrayList restofIF = isvalidthen(GO,linenum); + for (int j = 0; j < restofIF.size(); j++) { + //if(j==restofIF.size()-1){{result.add(new Token("done", TokenType.DONE,linenum));}} + result.add(restofIF.get(j)); + } + } + } else if (k2 == LineType.PRINT) { + if (GO.length < 2) throw new Exception("ERROR! INVALID PRINT STATEMENT"); + else if (i != 0) result.add(new Token(token2, TokenType.PRINTED, line)); + else result.add(new Token(token2, nextTokenType(token2), line)); + } else { + throw new Exception("Wrong or Invalid line type."); + } + } + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } + return result; + } + + public ArrayList assignedvariables(String input) throws IOException { + ArrayList result = new ArrayList<>(); + String[] tokens2 = input.trim().split("GO"); + int linenum = 0; + try { + if (!input.contains("GO")) throw new Exception("ERROR! Missing GO"); + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + for (String token : tokens2) { + linenum++; + //System.out.println("\n" + token + "\n"); + String[] GO = token.trim().split(" "); + try { + for (int i = 0; i < GO.length; i++) { + String token2 = GO[i]; + LineType k2; + //System.out.println(GO[i]); + if (nextTokenType(GO[0]) == TokenType.IF) k2 = LineType.CONDITION; + else if (isDataType(nextTokenType(GO[0])) && GO.length > 2) k2 = LineType.ASSIGNMENT; + else if (isDataType(nextTokenType(GO[0])) && GO.length == 2) k2 = LineType.DECLARATION; + else if (nextTokenType(GO[0]) == TokenType.PRINT) k2 = LineType.PRINT; + else k2 = LineType.INVALID; + + if (k2 == LineType.ASSIGNMENT) { + if(nextTokenType(GO[0])==TokenType.INTEGER&&token2==GO[1]&&isValididentifier(token2)) + {result.add(new Token(token2, nextTokenType(token2), linenum));} + else if(nextTokenType(GO[0])==TokenType.CHAR&&token2==(GO[1])&&isValididentifier(token2)) + {result.add(new Token(token2, nextTokenType(token2), linenum));} + else if(nextTokenType(GO[0])==TokenType.STRING&&token2==GO[1]&&isValididentifier(token2)) + {result.add(new Token(token2, nextTokenType(token2), linenum));} + } + // else if (k2 == LineType.DECLARATION) { + // if (isValididentifier(token2)) { + // result.add(new Token(token2, nextTokenType(token2), linenum)); + // } + // } else if (k2 == LineType.CONDITION) { + // if (isValididentifier(token2) || isNumeric(token2)) { + // result.add(new Token(token2, nextTokenType(token2), linenum)); + // } + // } else if (k2 == LineType.PRINT) { + // if (GO.length > 1) { + // if (i != 0) { + // result.add(new Token(token2, TokenType.PRINTED, linenum)); + // } else { + // result.add(new Token(token2, nextTokenType(token2), linenum)); + // } + // } + // } + } + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } + return result; + } +} diff --git a/token b/.vscode/token/token similarity index 100% rename from token rename to .vscode/token/token diff --git a/Calculator.java b/Calculator.java deleted file mode 100644 index 43bc7b3..0000000 --- a/Calculator.java +++ /dev/null @@ -1,37 +0,0 @@ - -package token; - - -public class Calculator { - public double evaluate(Node node) - { - if(node==null) - { - return 0; - } - if(node.left==null&&node.right==null) - { - return Double.parseDouble(node.data); - } - double leftValue=evaluate(node.left); - double rightValue=evaluate(node.right); - switch(node.data) - { - case "+": - return leftValue+rightValue; - case "-": - return leftValue-rightValue; - case "*": - return leftValue*rightValue; - case "/": - if(rightValue==0) - { - throw new UnsupportedOperationException("not divide by zero"); - } - return leftValue/rightValue; - default: - throw new IllegalArgumentException("unkown opreator"+node.data); - } - } - -} diff --git a/DataType.java b/DataType.java deleted file mode 100644 index fcf0d60..0000000 --- a/DataType.java +++ /dev/null @@ -1,13 +0,0 @@ -/* - * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license - * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template - */ -package token; - -public enum DataType -{ - INTEGER, - CHAR, - FLOAT, - DOUBLE, -} diff --git a/Input.txt b/Input.txt deleted file mode 100644 index d97993f..0000000 --- a/Input.txt +++ /dev/null @@ -1 +0,0 @@ -integer x equals 5 \ No newline at end of file diff --git a/Token.java b/Token.java deleted file mode 100644 index 6d35171..0000000 --- a/Token.java +++ /dev/null @@ -1,22 +0,0 @@ - -package token; - - -public class Token -{ - public String tokenValue; - public TokenType tokenType; - public Token(String tokenValue,TokenType tokenType) - { - this.tokenValue=tokenValue; - this.tokenType=tokenType; - } - public TokenType getType() - { - return tokenType; - } - public String getValue() - { - return tokenValue; - } -} diff --git a/TokenType.java b/TokenType.java index 7a9d1a3..190dc97 100644 --- a/TokenType.java +++ b/TokenType.java @@ -8,6 +8,10 @@ public enum TokenType DATATYPE, NUMBER, OPERATOR,//+ - / * + ADD, + SUB, + TIMES, + BY, WHITESPACE, INTEGER, CHAR, @@ -17,6 +21,16 @@ public enum TokenType LONG, STRING, INVALID, - IF, PRINT, + PRINTED, + IF, + CONDITION, + AND, + OR, + EQUALS, + EQUALSLESS, + EQUALSMORE, + LESS, + MORE, + GO, } diff --git a/Tokenizer.java b/Tokenizer.java deleted file mode 100644 index 06bb796..0000000 --- a/Tokenizer.java +++ /dev/null @@ -1,147 +0,0 @@ -package token; - -import java.io.IOException; -import java.util.ArrayList; -import java.nio.file.Files; -import java.nio.file.Paths; - -public class Tokenizer -{ - - public static void main(String[]args) - { - String filePath="Input.txt"; - String testt=""; - try{ - testt=new String(Files.readAllBytes(Paths.get(filePath))); - }catch(IOException e){ - e.printStackTrace(); - } - //Integer x equals 6 - //Character b equals 5 - //String[] tokens=testt.split(" "); - //for (String token : tokens) - //{System.out.print(token + ", ");} - //System.out.print("\n\n\n"); - - Tokenizer tokenizer=new Tokenizer(); - try{ - ArrayList listoftoken=tokenizer.tokenize(testt); - for(Token token:listoftoken) - {System.out.println(token.getValue()+":"+token.getType()+"\n");} - } - catch (IOException e) - { - System.out.println("ERROR"); - } - //ExpressionTree tree=tokenizer.buildExpressionTree(listoftoken); - //Calculator calculator=new Calculator(); - //double result=calculator.evaluate(tree.root); - - //tree.traverseInOrder(tree.root); - //System.out.println("\nresult="+result); - //Integer Integer x equals 5 - } - public static boolean isNumeric(String str) { - for (char c : str.toCharArray()) { - if (!Character.isDigit(c)) {return false;} - } - return true; -} - public boolean isDataType(TokenType input) - { - if (input!=TokenType.NUMBER&&input!=TokenType.OPERATOR&&input!=TokenType.CHARACTER)return true; - return false; - } - public boolean isOperator(String input) - { - return (input.equals("equals")||input.equals('-')||input.equals('+')||input.equals('*')||input.equals('/') - ||input.equals("plus")||input.equals("minus")||input.equals("times")||input.equals('=')||input.equals("by")); - } - public TokenType nextTokenType(String input) - { - //Character.isAlphabetic(input.charAt(0)) - if(input.equals("Integer"))return TokenType.INTEGER; - - else if(input.equals("String"))return TokenType.STRING; - - else if(input.equals("Character"))return TokenType.CHAR; - - else if(input.equals("Double"))return TokenType.DOUBLE; - - else if(input.equals("Long"))return TokenType.LONG; - - else if(isNumeric(input))return TokenType.NUMBER; - - else if(input.equals("if"))return TokenType.IF; - - else if(input.equals("Print")||input.equals("Print Line"))return TokenType.PRINT; - - else if(input.length()==1)return TokenType.CHARACTER; -// else if(Character.isWhitespace(nextChar)) -// { -// return TokenType.WHITESPACE; -// } - else if (isOperator(input)) - { - return TokenType.OPERATOR; - } - else - { - return TokenType.INVALID; - } - } - public ArrayList tokenize(String input) throws IOException{ - ArrayList result = new ArrayList(); - String[] tokens=input.split(" "); - String[] tokens2=input.split("GO"); - int stop=0; -// for(String token:tokens2){ -// System.out.println(token);} - - for(String token:tokens2){ - System.out.println(token+"\n"); - String[] GO=token.split(" "); -try { - for (int i = 0; i < GO.length; i++) { - String token2 = GO[i]; - TokenType k;LineType k2=LineType.ASSIGNMENT; - if (nextTokenType(GO[0])==TokenType.IF) - k2=LineType.CONDITION; - else if (isDataType(nextTokenType(GO[0]))) - k2=LineType.ASSIGNMENT; - else if (nextTokenType(GO[0])==TokenType.PRINT) - k2=LineType.PRINT; - else if(nextTokenType(GO[0])==TokenType.INVALID)k2=LineType.INVALID; - if(k2==LineType.ASSIGNMENT) - { - if (i == 0 && !isDataType(nextTokenType(token2))) { - throw new Exception("Invalid data type declaration."); - } - //Character k equals kasfgjasdg - else if (nextTokenType(GO[0])==TokenType.CHAR&&nextTokenType(GO[3])!=TokenType.CHARACTER) - {throw new Exception("ERROR! Declared token type should be CHARACTER");} - result.add(new Token(token2, nextTokenType(token2))); - } - } -} catch (Exception e) { - e.printStackTrace();System.exit(1); -} -} -// for(Token token4:result) -// { -// System.out.println(token4.tokenValue+" "+token4.tokenType); -// } - return result; - } - - -// public ExpressionTree buildExpressionTree(ArrayList tokens) -// { -// ExpressionTree tree = new ExpressionTree(); -// tree.buildTree(tokens); -// return tree; -// } - - -}