diff --git a/Aufgabe4/Parser.java b/Aufgabe4/Parser.java index c230d25d1e90bd397429597216d28cb9b98b0025..f97b6615140bef44e5018c2eb329b0122acd84bf 100644 --- a/Aufgabe4/Parser.java +++ b/Aufgabe4/Parser.java @@ -1,6 +1,8 @@ import java.sql.Array; import java.sql.SQLOutput; import java.util.ArrayList; +import java.util.stream.Collectors; +import java.util.stream.IntStream; public class Parser { @@ -19,17 +21,16 @@ public class Parser { // If tokenList contains one item: Expression is a value - boolean isValue = false; if(tokenList.size() == 1 ) { toReturn.astValue = parseValue(tokenList); - //return toReturn; - isValue = true; + return toReturn; } + // If tokenList contains a dot and not operators (+ - * /): Expression is a value for (Token t : tokenList) { @@ -45,17 +46,50 @@ public class Parser { } - if(isValue) { toReturn.astValue = parseValue(tokenList); return toReturn; } - // If tokenList only contains more than one item: Expression must be expression in brackets or binary operation + // If method reaches this point: Expression must be expression in brackets or binary operation + + // Check if first and last tokens have "(" and ")" strings + + if(tokenList.get(0).tokenString.equals("(") && tokenList.get(tokenList.size() - 1).tokenString.equals(")")) { + - // ( 3+4 ) * 5 + // Check if "false alarm", e.g. ( 1 + 2 ) + ( 1 + 2 ) <- starts and ends with ( and ) but is to be treated as binaryOp + boolean falseAlarm = false; + + for(int i = tokenList.size() - 2; i > 0; i--) { + + if(tokenList.get(i).tokenString.equals(")") || tokenList.get(i).tokenString.equals("(")) { + + if(tokenList.get(i).tokenString.equals(")")) { + falseAlarm = false; + + } else { + falseAlarm = true; + } + break; + } + } + + // Parse tokenList without first and last object and save it in toReturns astExpression + + if(!falseAlarm) { + + toReturn.astExpression = parseAstExpression( + (ArrayList<Token>) IntStream.range(1, tokenList.size() - 1) + .mapToObj(i -> tokenList.get(i)) + .collect(Collectors.toList()) // https://www.baeldung.com/java-stream-indices + ); + return toReturn; + } + + } toReturn.astBinaryOp = parseBinaryOp(tokenList); @@ -66,7 +100,6 @@ public class Parser { private AstBinaryOp parseBinaryOp (ArrayList<Token> tokenList) { - AstBinaryOp toReturn = new AstBinaryOp(); int positionOfFirstTopLevelOperator = 0; @@ -100,15 +133,15 @@ public class Parser { toReturn.astExpression2 = parseAstExpression(new ArrayList<>(tokenList.subList(positionOfFirstTopLevelOperator + 1, tokenList.size()))); - return toReturn ; + return toReturn; } private AstOperator parseOperator (Token operator) { - AstOperator astOperator = new AstOperator(); - astOperator.astOperator = operator.tokenString; + AstOperator toReturn = new AstOperator(); + toReturn.astOperator = operator.tokenString; - return astOperator; + return toReturn; } private AstValue parseValue (ArrayList<Token> valueTokens) { @@ -240,6 +273,7 @@ public class Parser { int tokenContentAsString; + try { tokenContentAsString = Integer.parseInt(digit.tokenString); } catch (NumberFormatException error) { diff --git a/Aufgabe4/main.java b/Aufgabe4/main.java index f46339291eb2aece0c9a9ad35e165a10c010f6bd..057cd27350f6b39ab112673ce4ecce97c9b891bb 100644 --- a/Aufgabe4/main.java +++ b/Aufgabe4/main.java @@ -1,5 +1,3 @@ -import java.util.Arrays; - public class main { public static void main(String[] args) { @@ -7,7 +5,7 @@ public class main { // values above integer space will produce errors - String ausdruck = "0032.00555 * 2.5 + 2.1"; + String ausdruck = "((5 + 1) + (3+4))"; Lexer lexer = new Lexer(); @@ -26,6 +24,9 @@ public class main { AstExpression result = p.parse(lexer.lex(ausdruck)); + printAst(result); + +/* do { try { @@ -62,5 +63,37 @@ public class main { } while (true); + */ + + } + + public static void printAst(AstExpression astExpression) { + + if(astExpression.astExpression != null) { + System.out.print("("); + printAst(astExpression.astExpression); + System.out.print( ")"); + return; + } + + if(astExpression.astValue != null) { + + if(astExpression.astValue.astNumber != null) { + + System.out.print(astExpression.astValue.astNumber.astDigitWoz.astDigitWozContent); + + } else { + System.out.println(astExpression.astValue.astDecimal.astDigitBeforeComma.astDigitWoz.astDigitWozContent + + "." + + astExpression.astValue.astDecimal.astDigitAfterComma.astDigitWoz.astDigitWozContent); + } + return; + } + + printAst(astExpression.astBinaryOp.astExpression1); + System.out.print(astExpression.astBinaryOp.astOperator.astOperator); + printAst(astExpression.astBinaryOp.astExpression2); + } + } diff --git a/Aufgabe4/test.java b/Aufgabe4/test.java index 91a6c9baaebc9562e69919247e4a9dc7e71da76d..cdf56ab4b0d79407d539437abdb973697b4dc17f 100644 --- a/Aufgabe4/test.java +++ b/Aufgabe4/test.java @@ -1,8 +1,11 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.IntStream; public class test { + /* public static void main(String[] args) { String ausdruck = "((3 * 5 * ( 4 + 3)) + ((5 + 4 ( 4*4 )) + 4))"; @@ -78,6 +81,43 @@ public class test { + } + + */ + + public static void main(String[] args) { + + String ausdruck = "((1+2) + (3+5))"; + String ausdruck2 = "(1+2) ()+ (3+5)"; + + Lexer lexer = new Lexer(); + + ArrayList<Token> test = lexer.lex(ausdruck); + ArrayList<Token> test2 = lexer.lex(ausdruck2); + + System.out.println(testmethode(test)); + System.out.println(testmethode(test2)); + + + + } + public static boolean testmethode(ArrayList<Token> arrList) { + + boolean bracketsAroundEverything = false; + + for(int i = arrList.size() - 2; i > 0; i--) { + if(arrList.get(i).tokenString.equals(")") || arrList.get(i).tokenString.equals("(")) { + if (arrList.get(i).tokenString.equals(")")) { + return true; + } + return false; + } + } + + System.out.println("geht hier rein"); + return false; + + } } diff --git a/out/production/inf3_git/main.class b/out/production/inf3_git/main.class index a8d74f180113e2cc6f580bdfdbf5bb8c5d6f4518..ef5c641e7481d83366e656c310a54d13a9807d7d 100644 Binary files a/out/production/inf3_git/main.class and b/out/production/inf3_git/main.class differ