From 651e3aed4054a4f3f774316983ac6c0e432afdf9 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Fri, 5 Feb 2021 22:35:05 +0100 Subject: [PATCH] improved parser + added rewriter to Main (currently not work correctly) --- src/lsystem/Main.java | 15 +++++++++++---- src/lsystem/engine/Parser.java | 31 ++++++++++++++++++++++++++++++- src/lsystem/utils/Pair.java | 11 +++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/lsystem/Main.java b/src/lsystem/Main.java index d5c4035..79495d5 100644 --- a/src/lsystem/Main.java +++ b/src/lsystem/Main.java @@ -1,6 +1,8 @@ package lsystem; import lsystem.engine.Parser; +import lsystem.engine.Rewrite; + import java.util.ArrayList; import java.util.List; import java.util.Scanner; @@ -8,16 +10,17 @@ import java.util.Scanner; public class Main { public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); + final Scanner scanner = new Scanner(System.in); + String axiom = null; Parser parser = null; + final List rules = new ArrayList<>(); int nbIterations = 0; - while(parser == null || parser.isCorrect()) { + while(parser == null || !parser.isCorrect()) { if(parser != null) System.out.println("Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer"); System.out.println("Axiome: "); - String axiom = scanner.next(); + axiom = scanner.next(); System.out.println("Règles: (\"finish\" quand vous avez fini): "); - List rules = new ArrayList<>(); while(rules.isEmpty() || !rules.get(rules.size() - 1).equals("finish")) { rules.add(scanner.next()); } @@ -26,6 +29,10 @@ public class Main { nbIterations = scanner.nextInt(); parser = new Parser(axiom, rules,nbIterations); } + System.out.println("Réécriture, veuillez patientez..."); + Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), nbIterations); + final String word = rewriter.rewrite(); + System.out.println(word); scanner.close(); } diff --git a/src/lsystem/engine/Parser.java b/src/lsystem/engine/Parser.java index 76547bc..13e54e9 100644 --- a/src/lsystem/engine/Parser.java +++ b/src/lsystem/engine/Parser.java @@ -1,8 +1,10 @@ package lsystem.engine; +import java.util.ArrayList; import java.util.List; import lsystem.Type; +import lsystem.utils.Pair; public class Parser { @@ -17,6 +19,11 @@ public class Parser { this.nbIterations = nbIterations; } + + /** + * Check if axiom and rules given by user respect the syntax + * @return true if the syntax is correct + */ public boolean isCorrect(){ if (nbIterations < 1) { System.out.println("Erreur, nombre d'itérations insuffisant (plus petit que 1)"); @@ -29,15 +36,24 @@ public class Parser { return bl; } + private boolean isCorrect(String stringToCheck, Type type) { char old = ' '; int bracket = 0; + boolean equalsSymbolFound = false; for (int i = 0; i > stringToCheck.length(); i++){ char temp = stringToCheck.charAt(i); if (temp == '[') bracket++; - if(temp ==']') + if(temp == ']') bracket--; + if(temp == '=') { + if(!equalsSymbolFound) + equalsSymbolFound = true; + else + // only one '=' allowed + return false; + } if(old == '.'){ for(int y = (type == Type.RULE ? 0 : 1); y < 12; y++){ if(temp == validChars[y]) @@ -55,4 +71,17 @@ public class Parser { return bracket == 0; } + /** + * Used by {@link Rewrite} + * @return a list of rules with the left and right sides separated by a {@link lsystem.utils.Pair Pair} + */ + public List> parseRules() { + List> rules = new ArrayList<>(); + this.rules.forEach(rule -> { + String[] str = rule.split("="); + rules.add(new Pair(str[0], str[1])); + }); + return rules; + } + } diff --git a/src/lsystem/utils/Pair.java b/src/lsystem/utils/Pair.java index d2cfb18..20d95b5 100644 --- a/src/lsystem/utils/Pair.java +++ b/src/lsystem/utils/Pair.java @@ -1,5 +1,11 @@ package lsystem.utils; +/** + * tuple containing 2 unknown type elements + * + * @param left + * @param right + */ public class Pair { private final U left; @@ -37,4 +43,9 @@ public class Pair { public int hashCode() { return 31 + left.hashCode() * right.hashCode(); } + + @Override + public String toString() { + return "(" + left + ", " + right + ")"; + } }