From 09fd792589935d6d36a4e1bd90d6c5e3d360064a Mon Sep 17 00:00:00 2001 From: AES268 Date: Wed, 8 May 2024 03:43:13 +0300 Subject: [PATCH 01/21] Changes 1 ASD --- Input.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Input.txt b/Input.txt index 2f67c54..a1b131c 100644 --- a/Input.txt +++ b/Input.txt @@ -1 +1 @@ -Print GO if GO \ No newline at end of file +Integer X equals 5GO \ No newline at end of file From 9c5e38c3bc1eda9a92f442cab2ac4a7c88a8c6a7 Mon Sep 17 00:00:00 2001 From: ahmad1999a28 Date: Wed, 8 May 2024 03:53:23 +0300 Subject: [PATCH 02/21] change --- Input.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Input.txt b/Input.txt index a1b131c..52118ec 100644 --- a/Input.txt +++ b/Input.txt @@ -1 +1 @@ -Integer X equals 5GO \ No newline at end of file +Integer X equals 10GO \ No newline at end of file From 782a100df5e27ec47f5d916976986c5f8b34c01b Mon Sep 17 00:00:00 2001 From: ahmad1999a28 Date: Wed, 8 May 2024 03:59:36 +0300 Subject: [PATCH 03/21] changed --- Input.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Input.txt b/Input.txt index 52118ec..665ef59 100644 --- a/Input.txt +++ b/Input.txt @@ -1 +1 @@ -Integer X equals 10GO \ No newline at end of file +Integer X equals 10 \ No newline at end of file From 1c1f00724563ca6c004f684c19f96e5400c9aa8e Mon Sep 17 00:00:00 2001 From: AES268 Date: Wed, 8 May 2024 04:02:25 +0300 Subject: [PATCH 04/21] Change 2 Y --- .vscode/launch.json | 14 ++++++++++++++ .vscode/settings.json | 3 +++ Input.txt | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..7c85317 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + // 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": "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..0db5873 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "cmake.configureOnOpen": true +} \ No newline at end of file diff --git a/Input.txt b/Input.txt index 52118ec..7071441 100644 --- a/Input.txt +++ b/Input.txt @@ -1 +1 @@ -Integer X equals 10GO \ No newline at end of file +String Y equals ZeftGO \ No newline at end of file From 3dde83bd269a56bccd19fe54f40b13c7a42ad6f1 Mon Sep 17 00:00:00 2001 From: ahmad1999a28 Date: Wed, 8 May 2024 04:09:11 +0300 Subject: [PATCH 05/21] change --- Input.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/Input.txt b/Input.txt index f1c4541..d1ed36e 100644 --- a/Input.txt +++ b/Input.txt @@ -1 +1,2 @@ String Y equals ZeftGO +integer x equals 7GO \ No newline at end of file From e39e2f4adf12c3aca86153b860849ecc15839932 Mon Sep 17 00:00:00 2001 From: ahmad1999a28 Date: Wed, 8 May 2024 04:11:35 +0300 Subject: [PATCH 06/21] change --- Input.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Input.txt b/Input.txt index d1ed36e..775fcb7 100644 --- a/Input.txt +++ b/Input.txt @@ -1,2 +1,3 @@ String Y equals ZeftGO -integer x equals 7GO \ No newline at end of file +integer x equals 7GO +print GO \ No newline at end of file From c08f5eb3228f9ca719b6589bbd472fa0ff8669b5 Mon Sep 17 00:00:00 2001 From: AES268 Date: Wed, 8 May 2024 04:14:21 +0300 Subject: [PATCH 07/21] Zeft Zeft Zeft --- Input.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Input.txt b/Input.txt index f1c4541..7071441 100644 --- a/Input.txt +++ b/Input.txt @@ -1 +1 @@ -String Y equals ZeftGO +String Y equals ZeftGO \ No newline at end of file From deb97a844055559f6d0e4257d456fad59aed45e4 Mon Sep 17 00:00:00 2001 From: AES268 Date: Wed, 8 May 2024 04:26:03 +0300 Subject: [PATCH 08/21] Zeft ZWEFT --- Input.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Input.txt b/Input.txt index 884b661..9bd788a 100644 --- a/Input.txt +++ b/Input.txt @@ -1,7 +1,3 @@ -<<<<<<< HEAD -String Y equals ZeftGO -======= String Y equals ZeftGO integer x equals 7GO print GO ->>>>>>> e39e2f4adf12c3aca86153b860849ecc15839932 From b72d81362a7aa96884ce6fde244b5b9268b13b7c Mon Sep 17 00:00:00 2001 From: AES268 Date: Wed, 8 May 2024 04:41:33 +0300 Subject: [PATCH 09/21] Ahmad Gameel --- .vscode/launch.json | 7 +++++++ Input.txt | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 7c85317..532f8df 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,13 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "type": "java", + "name": "Tokenizer", + "request": "launch", + "mainClass": "token.Tokenizer", + "projectName": "" + }, { "type": "java", "name": "Current File", diff --git a/Input.txt b/Input.txt index 9bd788a..022a9ba 100644 --- a/Input.txt +++ b/Input.txt @@ -1,3 +1,3 @@ String Y equals ZeftGO integer x equals 7GO -print GO +print GOCharacter prs equals YesGO \ No newline at end of file From 5f3c5ba20e3bec5c58c808cc3c2449125d87768d Mon Sep 17 00:00:00 2001 From: AES268 Date: Sat, 11 May 2024 10:06:35 +0300 Subject: [PATCH 10/21] =?UTF-8?q?=D8=A7=D8=B1=D8=AD=D9=85=D9=86=D9=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Input.txt | 4 +- Tokenizer.java | 132 +++++++++++++++++++++++-------------------------- 2 files changed, 62 insertions(+), 74 deletions(-) diff --git a/Input.txt b/Input.txt index 022a9ba..ba282bb 100644 --- a/Input.txt +++ b/Input.txt @@ -1,3 +1 @@ -String Y equals ZeftGO -integer x equals 7GO -print GOCharacter prs equals YesGO \ No newline at end of file +Double B equals 12941285GO \ No newline at end of file diff --git a/Tokenizer.java b/Tokenizer.java index 06bb796..fa032c3 100644 --- a/Tokenizer.java +++ b/Tokenizer.java @@ -17,13 +17,6 @@ public static void main(String[]args) }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); @@ -34,114 +27,111 @@ public static void main(String[]args) { 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; -} + } + boolean mathematicalexpression(String[] exp) + { + for(int i=3;i1)return true;return false; } + + public ArrayList tokenize(String input) throws IOException{ ArrayList result = new ArrayList(); - String[] tokens2=input.split("GO"); + String[] tokens2=input.trim().split("GO"); for(String token:tokens2){ System.out.println("\n"+token+"\n"); - String[] GO=token.split(" "); + 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; System.out.println("Condition statement"); } - else if (isDataType(nextTokenType(GO[0]))) + else if (isDataType(nextTokenType(GO[0]))&&GO.length>2) { k2=LineType.ASSIGNMENT; System.out.println("Assignment statement"); } - + else if (isDataType(nextTokenType(GO[0]))&&GO.length==2) + { + k2=LineType.DECLARATION; + System.out.println("Declaration statement"); + } else if (nextTokenType(GO[0])==TokenType.PRINT) { k2=LineType.PRINT; @@ -136,13 +151,24 @@ else if (nextTokenType(GO[0])==TokenType.PRINT) if(k2==LineType.ASSIGNMENT) { - if (nextTokenType(GO[0])==TokenType.CHAR&&nextTokenType(GO[3])!=TokenType.CHARACTER) + //if(GO.length>4){throw new Exception("ERROR! INVALID LINE DECLARATION, FORMAT:"+"\"TYPE,IDENTIFIER,OPERATOR,EXPRESSION\"");} + 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==GO[3])result.add(new Token(token2,TokenType.CHARACTER));else result.add(new Token(token2, nextTokenType(token2)));} 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 {result.add(new Token(token2, nextTokenType(token2)));} } + else if(k2==LineType.DECLARATION) + { + if(isDataType(nextTokenType(GO[0]))&&isValididentifier(GO[1]))result.add(new Token(token2, nextTokenType(token2))); + } else if(k2==LineType.CONDITION)if(!isvalidcond(GO)) {throw new Exception("ERROR! INVALID CONDITION STATEMENT");}else {result.add(new Token(token2, nextTokenType(token2)));} - else if(k2==LineType.PRINT)if(!isvalidprint(GO))throw new Exception("ERROR! INVALID PRINT STATEMENT");else result.add(new Token(token2, nextTokenType(token2))); + else if(k2==LineType.PRINT){if(!isvalidprint(GO))throw new Exception("ERROR! INVALID PRINT STATEMENT"); + else if(i!=0) result.add(new Token(token2, TokenType.PRINTED)); + else result.add(new Token(token2, nextTokenType(token2)));} else {throw new Exception("Wrong or Invalid line type.");} } From fe55c91248deec03e3fd73625c45f0824bc708ab Mon Sep 17 00:00:00 2001 From: AES268 Date: Wed, 15 May 2024 13:41:07 +0300 Subject: [PATCH 16/21] Conditions W 7agat --- Input.txt | 2 +- TokenType.java | 5 +++++ Tokenizer.java | 19 +++++++++++++------ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Input.txt b/Input.txt index 9a42b8f..74f1bd0 100644 --- a/Input.txt +++ b/Input.txt @@ -1 +1 @@ -If X and Y GO \ No newline at end of file +If X or Y GO \ No newline at end of file diff --git a/TokenType.java b/TokenType.java index b1e1eb1..0b8b1f6 100644 --- a/TokenType.java +++ b/TokenType.java @@ -8,6 +8,10 @@ public enum TokenType DATATYPE, NUMBER, OPERATOR,//+ - / * + ADD, + SUB, + TIMES, + BY, WHITESPACE, INTEGER, CHAR, @@ -24,4 +28,5 @@ public enum TokenType AND, OR, EQUALS, + GO, } diff --git a/Tokenizer.java b/Tokenizer.java index 53e3dde..5eff03e 100644 --- a/Tokenizer.java +++ b/Tokenizer.java @@ -75,8 +75,8 @@ boolean isLineType(TokenType input) } 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")); + 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) { @@ -86,6 +86,10 @@ boolean isConditionStatement(String input) { if(input.equals("If"))return true;return false; } + TokenType ConditionType(String tokenValue){ + if(tokenValue.equals("Or")||tokenValue.equals("or"))return TokenType.OR; + else if(tokenValue.equals("And")||tokenValue.equals("and"))return TokenType.AND; + else return TokenType.EQUALS;} boolean isPrintStatement(String input) { if(input.equals("Print")||input.equals("Print Line"))return true;return false; @@ -102,7 +106,7 @@ public TokenType nextTokenType(String input) 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("Or")||input.equals("And")||input.equals("and")||input.equals("or")||isequal(input))return TokenType.CONDITION; + else if(input.equals("Or")||input.equals("And")||input.equals("and")||input.equals("or")||input.equals("Is equal to"))return TokenType.CONDITION; else if(!Character.isDigit(input.charAt(0)))return TokenType.IDENTIFIER; else return TokenType.INVALID; } @@ -155,17 +159,20 @@ else if (nextTokenType(GO[0])==TokenType.PRINT) 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==GO[3])result.add(new Token(token2,TokenType.CHARACTER));else result.add(new Token(token2, nextTokenType(token2)));} + {if(token2==GO[3]){result.add(new Token(token2,TokenType.CHARACTER));result.add(new Token("GO", TokenType.GO));}else result.add(new Token(token2, nextTokenType(token2)));} 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 {result.add(new Token(token2, nextTokenType(token2)));} + else { + if(i==GO.length-1) {result.add(new Token(token2, nextTokenType(token2))); + result.add(new Token("GO", TokenType.GO));} + else result.add(new Token(token2, nextTokenType(token2)));} } else if(k2==LineType.DECLARATION) { if(isDataType(nextTokenType(GO[0]))&&isValididentifier(GO[1]))result.add(new Token(token2, nextTokenType(token2))); } - else if(k2==LineType.CONDITION)if(!isvalidcond(GO)) {throw new Exception("ERROR! INVALID CONDITION STATEMENT");}else {result.add(new Token(token2, nextTokenType(token2)));} + else if(k2==LineType.CONDITION)if(!isvalidcond(GO)) {throw new Exception("ERROR! INVALID CONDITION STATEMENT");}else {if(i==0)result.add(new Token(token2, nextTokenType(token2)));else result.add(new Token(token2, ConditionType(token2)));} else if(k2==LineType.PRINT){if(!isvalidprint(GO))throw new Exception("ERROR! INVALID PRINT STATEMENT"); else if(i!=0) result.add(new Token(token2, TokenType.PRINTED)); else result.add(new Token(token2, nextTokenType(token2)));} From 2c06ff21c09f9eda4a48b30b5efaaa38886730e2 Mon Sep 17 00:00:00 2001 From: AES268 Date: Sat, 18 May 2024 11:59:11 +0300 Subject: [PATCH 17/21] =?UTF-8?q?=D9=8A=D9=88=D9=85=20=D8=A7=D9=84=D8=B3?= =?UTF-8?q?=D8=A8=D8=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Checker.java | 19 +++ Input.txt | 2 +- Token.java | 13 +- TokenType.java | 4 + Tokenizer.java | 387 +++++++++++++++++++++++++++++-------------------- 5 files changed, 262 insertions(+), 163 deletions(-) create mode 100644 Checker.java diff --git a/Checker.java b/Checker.java new file mode 100644 index 0000000..65c33fd --- /dev/null +++ b/Checker.java @@ -0,0 +1,19 @@ +import java.util.ArrayList; + +public class Checker { + ArrayListTokens,Variables; + Checker(ArrayListlist1,ArrayListlist2) + { + Tokens=list1;Variables=list2; + } + + public void checkassignment() { + 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 list1: " + token1.line + ", Line in list2: " + token2.line); + } + } + } + } +} diff --git a/Input.txt b/Input.txt index 74f1bd0..3952bcf 100644 --- a/Input.txt +++ b/Input.txt @@ -1 +1 @@ -If X or Y GO \ No newline at end of file +If X and YGO Integer X Equals 5 Go \ No newline at end of file diff --git a/Token.java b/Token.java index 6d35171..e0f7bfd 100644 --- a/Token.java +++ b/Token.java @@ -4,13 +4,20 @@ public class Token { - public String tokenValue; - public TokenType tokenType; - public Token(String tokenValue,TokenType tokenType) + String tokenValue; + TokenType tokenType; + 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; + } public TokenType getType() { return tokenType; diff --git a/TokenType.java b/TokenType.java index 0b8b1f6..190dc97 100644 --- a/TokenType.java +++ b/TokenType.java @@ -28,5 +28,9 @@ public enum TokenType AND, OR, EQUALS, + EQUALSLESS, + EQUALSMORE, + LESS, + MORE, GO, } diff --git a/Tokenizer.java b/Tokenizer.java index 5eff03e..11a2d7d 100644 --- a/Tokenizer.java +++ b/Tokenizer.java @@ -1,191 +1,260 @@ package token; import java.io.IOException; -import java.util.ArrayList; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.ArrayList; + +public class Tokenizer { -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){ + 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(); } - 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) - { + Tokenizer tokenizer = new Tokenizer(); + ArrayList listoftoken,listofVariables; + try { + listoftoken = tokenizer.tokenize(testt); + listofVariables = tokenizer.tokenize2(testt); + for (Token token : listoftoken) { + System.out.println(token.getValue() + ":" + token.getType() + ":" + token.line + "\n"); + } + for (Token token : listofVariables) { + System.out.println(token.getValue() + ":" + token.getType() + ":" + token.line + "\n"); + } + } catch (IOException e) { System.out.println("ERROR"); } + //new Checker(listoftoken,listofVariables); + } + + public static boolean isNumeric(String str) { + for (char c : str.toCharArray()) { + if (!Character.isDigit(c)) return false; } - public static boolean isNumeric(String str) {for (char c : str.toCharArray()) if (!Character.isDigit(c)) return false;return true;} - boolean mathematicalexpression(String[] exp) - { - for(int i=3;i 2 && GO.length % 2 == 0) { + for (int i = 1; i < GO.length; i++) { + if ((i % 2 != 0 && (!isValididentifier(GO[i]) && !isNumeric(GO[i]))) || (i % 2 == 0 && (!isCondition(GO[i])))) return false; + } + return true; + } else return false; } - // TokenType conditions(String ar) - // { - // if(ar.equals("And"))return TokenType.AND;else if (ar.equals("Or"))return TokenType.OR; else if (ar.equals("Equals"))return TokenType.EQUALS; - // else return TokenType.INVALID; - // } - boolean isvalidInt(String[]str) - { - if(isValididentifier(str[1])&&isequal(str[2])&&mathematicalexpression(str)) - return true; - else return false; + + boolean isvalidInt(String[] str) { + if (isValididentifier(str[1]) && isequal(str[2]) && mathematicalexpression(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; + + 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;} - boolean isLineType(TokenType input) - { - if(input==TokenType.PRINT||input==TokenType.IF)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; } - 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 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; + + public boolean isValididentifier(String input) { + if (nextTokenType(input) == TokenType.IDENTIFIER) return true; + return false; } - boolean isConditionStatement(String input) - { - if(input.equals("If"))return true;return false; + + boolean isPrintStatement(String input) { + if (input.equals("Print") || input.equals("Print Line")) return true; + return false; } - TokenType ConditionType(String tokenValue){ - if(tokenValue.equals("Or")||tokenValue.equals("or"))return TokenType.OR; - else if(tokenValue.equals("And")||tokenValue.equals("and"))return TokenType.AND; - else return TokenType.EQUALS;} - 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")) + 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.length()==1)return TokenType.CHARACTER; - 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("Or")||input.equals("And")||input.equals("and")||input.equals("or")||input.equals("Is equal to"))return TokenType.CONDITION; - else if(!Character.isDigit(input.charAt(0)))return TokenType.IDENTIFIER; + + 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("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("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")) 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; } - boolean isvalidprint(String[] str) - { - if (str.length>1)return true;return false; - } - - - public ArrayList tokenize(String input) throws IOException{ - ArrayList result = new ArrayList(); - String[] tokens2=input.trim().split("GO"); - for(String token:tokens2){ - 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; - System.out.println("Condition statement"); - } - else if (isDataType(nextTokenType(GO[0]))&&GO.length>2) - { - k2=LineType.ASSIGNMENT; - System.out.println("Assignment statement"); - } - else if (isDataType(nextTokenType(GO[0]))&&GO.length==2) - { - k2=LineType.DECLARATION; - System.out.println("Declaration statement"); - } - else if (nextTokenType(GO[0])==TokenType.PRINT) - { - k2=LineType.PRINT; - System.out.println("Print statement"); + 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 k2 = LineType.INVALID; - else k2=LineType.INVALID; - - if(k2==LineType.ASSIGNMENT) - { - //if(GO.length>4){throw new Exception("ERROR! INVALID LINE DECLARATION, FORMAT:"+"\"TYPE,IDENTIFIER,OPERATOR,EXPRESSION\"");} - 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==GO[3]){result.add(new Token(token2,TokenType.CHARACTER));result.add(new Token("GO", TokenType.GO));}else result.add(new Token(token2, nextTokenType(token2)));} - 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(i==GO.length-1) {result.add(new Token(token2, nextTokenType(token2))); - result.add(new Token("GO", TokenType.GO));} - else result.add(new Token(token2, nextTokenType(token2)));} + 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 (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.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) { + if (!isvalidcond(GO)) { + throw new Exception("ERROR! INVALID CONDITION STATEMENT"); + } else { + result.add(new Token(token2, nextTokenType(token2), linenum)); + } + } 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); + } } - else if(k2==LineType.DECLARATION) - { - if(isDataType(nextTokenType(GO[0]))&&isValididentifier(GO[1]))result.add(new Token(token2, nextTokenType(token2))); - } - else if(k2==LineType.CONDITION)if(!isvalidcond(GO)) {throw new Exception("ERROR! INVALID CONDITION STATEMENT");}else {if(i==0)result.add(new Token(token2, nextTokenType(token2)));else result.add(new Token(token2, ConditionType(token2)));} - else if(k2==LineType.PRINT){if(!isvalidprint(GO))throw new Exception("ERROR! INVALID PRINT STATEMENT"); - else if(i!=0) result.add(new Token(token2, TokenType.PRINTED)); - else result.add(new Token(token2, nextTokenType(token2)));} - else {throw new Exception("Wrong or Invalid line type.");} - - } -} catch (Exception e) { - e.printStackTrace();System.exit(1); -} -} - return result; + return result; } + public ArrayList tokenize2(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 (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; + } } From 4c13a5a7f6d683f09248e1cdc8170d4a812752e5 Mon Sep 17 00:00:00 2001 From: AES268 Date: Sat, 18 May 2024 12:13:20 +0300 Subject: [PATCH 18/21] SSA --- Checker2.java | 19 +++++++++++++++++++ Token.java | 1 - Tokenizer.java | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 Checker2.java diff --git a/Checker2.java b/Checker2.java new file mode 100644 index 0000000..65c33fd --- /dev/null +++ b/Checker2.java @@ -0,0 +1,19 @@ +import java.util.ArrayList; + +public class Checker { + ArrayListTokens,Variables; + Checker(ArrayListlist1,ArrayListlist2) + { + Tokens=list1;Variables=list2; + } + + public void checkassignment() { + 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 list1: " + token1.line + ", Line in list2: " + token2.line); + } + } + } + } +} diff --git a/Token.java b/Token.java index e0f7bfd..58c6696 100644 --- a/Token.java +++ b/Token.java @@ -1,4 +1,3 @@ - package token; diff --git a/Tokenizer.java b/Tokenizer.java index 11a2d7d..defcf47 100644 --- a/Tokenizer.java +++ b/Tokenizer.java @@ -29,7 +29,7 @@ public static void main(String[] args) { } catch (IOException e) { System.out.println("ERROR"); } - //new Checker(listoftoken,listofVariables); + Checker Z=new Checker(listoftoken,listofVariables); } public static boolean isNumeric(String str) { From 3fc8ccf182fb145f61984bbd914cc320ee635696 Mon Sep 17 00:00:00 2001 From: AES268 Date: Mon, 20 May 2024 11:03:09 +0300 Subject: [PATCH 19/21] =?UTF-8?q?=D9=8A=D8=A7=D8=B1=D8=A8=20=D8=AA=D9=81?= =?UTF-8?q?=D8=B1=D8=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 7 + .vscode/token/Calculator.java | 33 ++++ .vscode/token/Checker.java | 60 +++++++ .vscode/token/DataType.java | 9 + .vscode/token/ExpressionTree.java | 91 ++++++++++ .vscode/token/Input.txt | 3 + .vscode/token/LineType.java | 18 ++ .vscode/token/Node.java | 14 ++ .vscode/token/Output.txt | 0 .vscode/token/Token.java | 27 +++ .vscode/token/TokenType.java | 37 ++++ .vscode/token/Tokenizer.java | 285 ++++++++++++++++++++++++++++++ .vscode/token/token | 147 +++++++++++++++ 13 files changed, 731 insertions(+) create mode 100644 .vscode/token/Calculator.java create mode 100644 .vscode/token/Checker.java create mode 100644 .vscode/token/DataType.java create mode 100644 .vscode/token/ExpressionTree.java create mode 100644 .vscode/token/Input.txt create mode 100644 .vscode/token/LineType.java create mode 100644 .vscode/token/Node.java create mode 100644 .vscode/token/Output.txt create mode 100644 .vscode/token/Token.java create mode 100644 .vscode/token/TokenType.java create mode 100644 .vscode/token/Tokenizer.java create mode 100644 .vscode/token/token diff --git a/.vscode/launch.json b/.vscode/launch.json index 532f8df..757e06b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,13 @@ // 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", diff --git a/.vscode/token/Calculator.java b/.vscode/token/Calculator.java new file mode 100644 index 0000000..aad7958 --- /dev/null +++ b/.vscode/token/Calculator.java @@ -0,0 +1,33 @@ +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/.vscode/token/Checker.java b/.vscode/token/Checker.java new file mode 100644 index 0000000..2507970 --- /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/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/ExpressionTree.java b/.vscode/token/ExpressionTree.java new file mode 100644 index 0000000..18a16e7 --- /dev/null +++ b/.vscode/token/ExpressionTree.java @@ -0,0 +1,91 @@ + + + +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..90a54cc --- /dev/null +++ b/.vscode/token/Input.txt @@ -0,0 +1,3 @@ +Integer X Equals 5 GO +Integer Y Equals 6 GO +If X then Integer Y is 8 GO diff --git a/.vscode/token/LineType.java b/.vscode/token/LineType.java new file mode 100644 index 0000000..3138d8b --- /dev/null +++ b/.vscode/token/LineType.java @@ -0,0 +1,18 @@ +/* + * 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 +{ + CONDITION, + ASSIGNMENT, + PRINT, + DECLARATION, + INVALID, +} diff --git a/.vscode/token/Node.java b/.vscode/token/Node.java new file mode 100644 index 0000000..3814338 --- /dev/null +++ b/.vscode/token/Node.java @@ -0,0 +1,14 @@ + + +public class Node +{ + String data; + Node left; + Node right; + Node(String data) + { + this.data=data; + this.left=null; + this.right=right; + } +} diff --git a/.vscode/token/Output.txt b/.vscode/token/Output.txt new file mode 100644 index 0000000..e69de29 diff --git a/.vscode/token/Token.java b/.vscode/token/Token.java new file mode 100644 index 0000000..7cf4ab6 --- /dev/null +++ b/.vscode/token/Token.java @@ -0,0 +1,27 @@ + + +public class Token +{ + String tokenValue; + TokenType tokenType; + 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; + } + 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..1603a4f --- /dev/null +++ b/.vscode/token/TokenType.java @@ -0,0 +1,37 @@ + + + +public enum TokenType +{ + IDENTIFIER,///variable name + DATATYPE, + NUMBER, + OPERATOR,//+ - / * + ADD, + SUB, + TIMES, + BY, + WHITESPACE, + INTEGER, + CHAR, + CHARACTER, + 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..a9d551b --- /dev/null +++ b/.vscode/token/Tokenizer.java @@ -0,0 +1,285 @@ +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; + +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; + try { + listoftoken = tokenizer.tokenize(testt); + listofVariables = tokenizer.assignedvariables(testt); + for (Token token : listoftoken) { + System.out.println(token.getValue() + ":" + token.getType() + ":" + token.line + "\n"); + } + 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); + } + + public static boolean isNumeric(String str) { + for (char c : str.toCharArray()) { + if (!Character.isDigit(c)) return false; + } + return true; + } + + boolean mathematicalexpression(String[] exp) { + 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 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])))) return false;} + return true; + } else return false; + } + else return false; + } + ArrayList isvalidthen(String[] GO) { + int then_location=0;StringBuilder result = new StringBuilder(); + while(!GO[then_location++].equals("then")); + for(int i=then_location;i IfResult=null; + try{IfResult=thenOutput.tokenize(input);} + catch(IOException e){System.out.println("ERROR");} + 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]) && mathematicalexpression(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; + } + + 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")) + 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("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")) 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 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 (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.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) { + if (!isvalidcond(GO)) {throw new Exception("ERROR! INVALID CONDITION STATEMENT");} + else if (i == GO.length - 1) { + ArrayListv=isvalidthen(GO); + 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.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 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 (isValididentifier(token2)&&nextTokenType(GO[0])!=TokenType.CHAR) { + result.add(new Token(token2, nextTokenType(token2), linenum)); + } + else if(nextTokenType(GO[0])==TokenType.CHAR&&!token2.equals(GO[3])&&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/.vscode/token/token b/.vscode/token/token new file mode 100644 index 0000000..63432dd --- /dev/null +++ b/.vscode/token/token @@ -0,0 +1,147 @@ +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="src/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; +// } + + +} From 49322381fc1e36ce438881c0b78a353c28e6f2f6 Mon Sep 17 00:00:00 2001 From: AES268 Date: Mon, 20 May 2024 17:50:01 +0300 Subject: [PATCH 20/21] kkl --- Calculator.java | 37 ------- Checker.java | 19 ---- Checker2.java | 19 ---- DataType.java | 13 --- ExpressionTree.java | 91 ---------------- Input.txt | 1 - LineType.java | 18 --- Node.java | 16 --- Output.txt | 0 Token.java | 28 ----- Tokenizer.java | 260 -------------------------------------------- token | 147 ------------------------- 12 files changed, 649 deletions(-) delete mode 100644 Calculator.java delete mode 100644 Checker.java delete mode 100644 Checker2.java delete mode 100644 DataType.java delete mode 100644 ExpressionTree.java delete mode 100644 Input.txt delete mode 100644 LineType.java delete mode 100644 Node.java delete mode 100644 Output.txt delete mode 100644 Token.java delete mode 100644 Tokenizer.java delete mode 100644 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/Checker.java b/Checker.java deleted file mode 100644 index 65c33fd..0000000 --- a/Checker.java +++ /dev/null @@ -1,19 +0,0 @@ -import java.util.ArrayList; - -public class Checker { - ArrayListTokens,Variables; - Checker(ArrayListlist1,ArrayListlist2) - { - Tokens=list1;Variables=list2; - } - - public void checkassignment() { - 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 list1: " + token1.line + ", Line in list2: " + token2.line); - } - } - } - } -} diff --git a/Checker2.java b/Checker2.java deleted file mode 100644 index 65c33fd..0000000 --- a/Checker2.java +++ /dev/null @@ -1,19 +0,0 @@ -import java.util.ArrayList; - -public class Checker { - ArrayListTokens,Variables; - Checker(ArrayListlist1,ArrayListlist2) - { - Tokens=list1;Variables=list2; - } - - public void checkassignment() { - 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 list1: " + token1.line + ", Line in list2: " + token2.line); - } - } - } - } -} 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/ExpressionTree.java b/ExpressionTree.java deleted file mode 100644 index be26584..0000000 --- a/ExpressionTree.java +++ /dev/null @@ -1,91 +0,0 @@ - -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"); - } -} - } \ No newline at end of file diff --git a/Input.txt b/Input.txt deleted file mode 100644 index 3952bcf..0000000 --- a/Input.txt +++ /dev/null @@ -1 +0,0 @@ -If X and YGO Integer X Equals 5 Go \ No newline at end of file diff --git a/LineType.java b/LineType.java deleted file mode 100644 index d71f75f..0000000 --- a/LineType.java +++ /dev/null @@ -1,18 +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; - -/** - * - * @author B268 - */ -public enum LineType -{ - CONDITION, - ASSIGNMENT, - PRINT, - DECLARATION, - INVALID, -} diff --git a/Node.java b/Node.java deleted file mode 100644 index be390ea..0000000 --- a/Node.java +++ /dev/null @@ -1,16 +0,0 @@ - -package token; - - -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/Output.txt deleted file mode 100644 index e69de29..0000000 diff --git a/Token.java b/Token.java deleted file mode 100644 index 58c6696..0000000 --- a/Token.java +++ /dev/null @@ -1,28 +0,0 @@ -package token; - - -public class Token -{ - String tokenValue; - TokenType tokenType; - 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; - } - public TokenType getType() - { - return tokenType; - } - public String getValue() - { - return tokenValue; - } -} diff --git a/Tokenizer.java b/Tokenizer.java deleted file mode 100644 index defcf47..0000000 --- a/Tokenizer.java +++ /dev/null @@ -1,260 +0,0 @@ -package token; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.ArrayList; - -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(); - } - Tokenizer tokenizer = new Tokenizer(); - ArrayList listoftoken,listofVariables; - try { - listoftoken = tokenizer.tokenize(testt); - listofVariables = tokenizer.tokenize2(testt); - for (Token token : listoftoken) { - System.out.println(token.getValue() + ":" + token.getType() + ":" + token.line + "\n"); - } - 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(listoftoken,listofVariables); - } - - public static boolean isNumeric(String str) { - for (char c : str.toCharArray()) { - if (!Character.isDigit(c)) return false; - } - return true; - } - - boolean mathematicalexpression(String[] exp) { - 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 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) { - if (GO.length == 2) { - if (isValididentifier(GO[1]) || isNumeric(GO[1])) return true; - return false; - } else if (GO.length > 2 && GO.length % 2 == 0) { - for (int i = 1; i < GO.length; i++) { - if ((i % 2 != 0 && (!isValididentifier(GO[i]) && !isNumeric(GO[i]))) || (i % 2 == 0 && (!isCondition(GO[i])))) return false; - } - return true; - } else return false; - } - - boolean isvalidInt(String[] str) { - if (isValididentifier(str[1]) && isequal(str[2]) && mathematicalexpression(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; - } - - 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")) - 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("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("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")) 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 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 (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.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) { - if (!isvalidcond(GO)) { - throw new Exception("ERROR! INVALID CONDITION STATEMENT"); - } else { - result.add(new Token(token2, nextTokenType(token2), linenum)); - } - } 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 tokenize2(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 (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/token deleted file mode 100644 index 63432dd..0000000 --- a/token +++ /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="src/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; -// } - - -} From db19044ad7b535ed2fa128f916cfceea3fdf5e48 Mon Sep 17 00:00:00 2001 From: AES268 Date: Wed, 22 May 2024 10:14:42 +0300 Subject: [PATCH 21/21] Final Commit --- .vscode/token/Calculator.java | 20 +- .vscode/token/Checker.java | 2 +- .vscode/token/Compiler.java | 75 ++++++ .vscode/token/Evaluator.java | 128 ++++++++++ .vscode/token/Input.txt | 4 +- .vscode/token/LineType.java | 1 + .vscode/token/Token.java | 21 +- .vscode/token/TokenType.java | 1 + .vscode/token/Tokenizer.java | 461 +++++++++++++++++++++++++++++++--- 9 files changed, 660 insertions(+), 53 deletions(-) create mode 100644 .vscode/token/Compiler.java create mode 100644 .vscode/token/Evaluator.java diff --git a/.vscode/token/Calculator.java b/.vscode/token/Calculator.java index aad7958..e2566fd 100644 --- a/.vscode/token/Calculator.java +++ b/.vscode/token/Calculator.java @@ -13,20 +13,12 @@ public double evaluate(Node node) 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); + 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 index 2507970..7d341e6 100644 --- a/.vscode/token/Checker.java +++ b/.vscode/token/Checker.java @@ -48,7 +48,7 @@ public boolean checkassignmentexists(ArrayList Tokens, ArrayList V 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 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());} 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/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/.vscode/token/Input.txt b/.vscode/token/Input.txt index 90a54cc..8b6b155 100644 --- a/.vscode/token/Input.txt +++ b/.vscode/token/Input.txt @@ -1,3 +1 @@ -Integer X Equals 5 GO -Integer Y Equals 6 GO -If X then Integer Y is 8 GO +Integer X is 18 Minus 8 GO diff --git a/.vscode/token/LineType.java b/.vscode/token/LineType.java index 3138d8b..e15102e 100644 --- a/.vscode/token/LineType.java +++ b/.vscode/token/LineType.java @@ -10,6 +10,7 @@ */ public enum LineType { + ASSIGNMENT_NO_DECLARATION, CONDITION, ASSIGNMENT, PRINT, diff --git a/.vscode/token/Token.java b/.vscode/token/Token.java index 7cf4ab6..c8ed141 100644 --- a/.vscode/token/Token.java +++ b/.vscode/token/Token.java @@ -3,7 +3,7 @@ public class Token { String tokenValue; - TokenType tokenType; + TokenType tokenType,stype1,stype2,stype3; int line; Token(String tokenValue,TokenType tokenType) { @@ -16,6 +16,25 @@ public class Token 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; diff --git a/.vscode/token/TokenType.java b/.vscode/token/TokenType.java index 1603a4f..eff695c 100644 --- a/.vscode/token/TokenType.java +++ b/.vscode/token/TokenType.java @@ -15,6 +15,7 @@ public enum TokenType INTEGER, CHAR, CHARACTER, + EQUALSCHECK, FLOAT, DOUBLE, LONG, diff --git a/.vscode/token/Tokenizer.java b/.vscode/token/Tokenizer.java index a9d551b..83da64c 100644 --- a/.vscode/token/Tokenizer.java +++ b/.vscode/token/Tokenizer.java @@ -2,10 +2,11 @@ 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 { @@ -14,21 +15,29 @@ public static void main(String[] args) throws Exception { e.printStackTrace(); } Tokenizer tokenizer = new Tokenizer(); - ArrayList listoftoken = null,listofVariables = null; + 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"); + 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); + Z.checkassignmentexists(listoftoken,listofVariables); + Z.checkassignmentorder(listoftoken,listofVariables); + Compiler Z2=new Compiler(listoftoken); + System.out.println(Z2.Complete()); + } public static boolean isNumeric(String str) { @@ -38,14 +47,60 @@ public static boolean isNumeric(String str) { return true; } - boolean mathematicalexpression(String[] exp) { + 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; - } + 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; @@ -59,23 +114,30 @@ boolean isvalidcond(String[] GO) { if(found==1){ if (s == 2) {if (isValididentifier(GO[1]) || isNumeric(GO[1])) return true;return false;} else if (GO.length-1 > 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])))) return false;} + 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; } - ArrayList isvalidthen(String[] GO) { - int then_location=0;StringBuilder result = new StringBuilder(); + 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")); - for(int i=then_location;i IfResult=null; - try{IfResult=thenOutput.tokenize(input);} + 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 isvalidthen(String[] GO) { } boolean isvalidInt(String[] str) { - if (isValididentifier(str[1]) && isequal(str[2]) && mathematicalexpression(str)) + if (isValididentifier(str[1]) && isequal(str[2]) && declared_mathematical_expression(str)) return true; else return false; } @@ -100,6 +162,11 @@ boolean isequal(String input) { 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("/") @@ -117,7 +184,7 @@ boolean isPrintStatement(String input) { } 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")) + 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; } @@ -132,11 +199,15 @@ public TokenType nextTokenType(String input) { 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 (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")) return TokenType.EQUALS; + 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; @@ -157,19 +228,19 @@ public ArrayList tokenize(String input) throws IOException { } for (String token : tokens2) { linenum++; - System.out.println("\n" + token + "\n"); + //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]); + //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"); @@ -184,15 +255,30 @@ public ArrayList tokenize(String input) throws IOException { throw new Exception("ERROR! Declared LONG assignment invalid"); } else if (nextTokenType(token2) == TokenType.INVALID) { throw new Exception("ERROR! INVALID TOKEN DETECTED"); - } else { + } 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 tokenize(String input) throws IOException { result.add(new Token(token2, nextTokenType(token2), linenum)); } } - } else if (k2 == LineType.CONDITION) { + } else if (k2 == LineType.CONDITION) {int then=thenlocation(GO); if (!isvalidcond(GO)) {throw new Exception("ERROR! INVALID CONDITION STATEMENT");} - else if (i == GO.length - 1) { - ArrayListv=isvalidthen(GO); - result.add(new Token(token2, nextTokenType(token2), linenum)); - result.add(new Token("GO", TokenType.GO, linenum)); + 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 {result.add(new Token(token2, nextTokenType(token2), linenum));} + else { + ArrayList 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)); @@ -224,6 +314,308 @@ else if (i == GO.length - 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<>(); @@ -237,13 +629,13 @@ public ArrayList assignedvariables(String input) throws IOException { } for (String token : tokens2) { linenum++; - System.out.println("\n" + token + "\n"); + //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]); + //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; @@ -251,10 +643,11 @@ public ArrayList assignedvariables(String input) throws IOException { else k2 = LineType.INVALID; if (k2 == LineType.ASSIGNMENT) { - if (isValididentifier(token2)&&nextTokenType(GO[0])!=TokenType.CHAR) { - result.add(new Token(token2, nextTokenType(token2), linenum)); - } - else if(nextTokenType(GO[0])==TokenType.CHAR&&!token2.equals(GO[3])&&isValididentifier(token2)) + 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) {