From 17bab7b3d41462c8fc6e9a648765d98a5d395d58 Mon Sep 17 00:00:00 2001 From: SamyaDaleh Date: Sun, 7 Jan 2024 15:55:53 +0100 Subject: [PATCH] replace epsilon and arrow right by constants --- .../cltoolbox/chartparsing/Deduction.java | 9 ++++-- .../converter/cfg/CfgToCykRulesConverter.java | 7 ++--- .../converter/lag/LagChartItem.java | 4 ++- .../lcfrs/LcfrsToCykRulesConverter.java | 3 +- .../chartparsing/tag/cyk/TagCykAdjoin.java | 3 +- .../tag/earley/TagEarleyAdjoin.java | 3 +- .../tag/earley/TagEarleySubstitute.java | 3 +- .../TagEarleyPrefixValidPredictAdjoined.java | 3 +- .../cltoolbox/common/ArrayUtils.java | 4 ++- .../cltoolbox/common/Constants.java | 4 +++ .../cltoolbox/common/TreeUtils.java | 11 +++++--- .../samyadaleh/cltoolbox/common/cfg/Cfg.java | 6 ++-- .../common/cfg/CfgProductionRule.java | 19 +++++++------ .../common/cfg/PcfgProductionRule.java | 11 +++++--- .../cltoolbox/common/cfg/util/Doubling.java | 9 ++++-- .../cltoolbox/common/cfg/util/Factoring.java | 6 ++-- .../common/cfg/util/LeftRecursion.java | 28 +++++++++++-------- .../cltoolbox/common/cfg/util/Loops.java | 6 ++-- .../cfg/util/ReverseProductionRules.java | 6 ++-- .../NondeterministicFiniteAutomaton.java | 8 ++++-- .../finiteautomata/util/UnifyFinalStates.java | 4 ++- .../cltoolbox/common/lag/LagRule.java | 4 ++- .../cltoolbox/common/lag/LagState.java | 4 ++- .../cltoolbox/common/lag/LagWord.java | 4 ++- .../cltoolbox/common/lcfrs/Clause.java | 13 +++++---- .../cltoolbox/common/lcfrs/Predicate.java | 8 ++++-- .../cltoolbox/common/lcfrs/Srcg.java | 6 ++-- .../common/lcfrs/util/EmptyProductions.java | 4 ++- .../cltoolbox/common/lcfrs/util/Order.java | 4 ++- .../parser/inner/InnerLagGrammarParser.java | 4 ++- .../samyadaleh/cltoolbox/common/tag/Tag.java | 4 ++- .../samyadaleh/cltoolbox/common/tag/Tree.java | 11 ++++---- .../cltoolbox/gui/AbstractDisplayTree.java | 6 ++-- 33 files changed, 148 insertions(+), 81 deletions(-) diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/Deduction.java b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/Deduction.java index b148c3bc..10b3b03f 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/Deduction.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/Deduction.java @@ -13,6 +13,9 @@ import java.util.*; import java.util.stream.Collectors; +import static com.github.samyadaleh.cltoolbox.common.Constants.ARROW_RIGHT; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + /** * A deduction system that derives consequences from antecendence items and * tries to generate a goal item. Based on the slides from Laura Kallmeyer about @@ -194,11 +197,11 @@ public void printTraceLatex(String[][] trace) { for (String[] line : trace) { String id = line[0]; String item = line[1].replace("$", "\\$").replace("•", "\\textbullet{}") - .replace("ε", "$\\epsilon$").replace("->", "$\\rightarrow$"); + .replace(EPSILON, "$\\epsilon$").replace(ARROW_RIGHT, "$\\rightarrow$"); String rules = - line[2].replace("ε", "$\\epsilon$").replace("->", "$\\rightarrow$"); + line[2].replace(EPSILON, "$\\epsilon$").replace(ARROW_RIGHT, "$\\rightarrow$"); String backPointers = line[3].replace("{", "\\{").replace("}", "\\}"); - String trees = line[4].replace("ε", "$\\epsilon$"); + String trees = line[4].replace(EPSILON, "$\\epsilon$"); log.info( id + " & " + item + " & " + rules + " & " + backPointers + " & " + trees + " \\\\"); } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/cfg/CfgToCykRulesConverter.java b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/cfg/CfgToCykRulesConverter.java index 2bcf53f5..f406f63d 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/cfg/CfgToCykRulesConverter.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/cfg/CfgToCykRulesConverter.java @@ -16,8 +16,7 @@ import java.util.ArrayList; import java.util.List; -import static com.github.samyadaleh.cltoolbox.common.Constants.DEDUCTION_RULE_CFG_CYK_AXIOM; -import static com.github.samyadaleh.cltoolbox.common.Constants.DEDUCTION_RULE_CFG_CYK_AXIOM_EPSILON; +import static com.github.samyadaleh.cltoolbox.common.Constants.*; public class CfgToCykRulesConverter { @@ -30,7 +29,7 @@ public static ParsingSchema cfgToCykRules(Cfg cfg, String w) throw new ParseException("Grammar has to be in Chomsky Normal Form.", 1); } String[] wSplit = w.split(" "); - int wLength = wSplit[0].equals("") || wSplit[0].equals("ε") + int wLength = wSplit[0].equals("") || wSplit[0].equals(EPSILON) ? 0 : wSplit.length; ParsingSchema schema = new ParsingSchema(); @@ -84,7 +83,7 @@ private static void addCykScanRules(String[] wSplit, ParsingSchema schema, for (int i = 0; i < wSplit.length; i++) { if (wSplit[i].equals(rule.getRhs()[0])) { StaticDeductionRule scan = new StaticDeductionRule(); - String itemLength = wSplit[i].equals("") || wSplit[i].equals("ε") + String itemLength = wSplit[i].equals("") || wSplit[i].equals(EPSILON) ? "0" : "1"; ChartItemInterface consequence = new DeductionChartItem(rule.getLhs(), String.valueOf(i), diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/lag/LagChartItem.java b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/lag/LagChartItem.java index 38587887..52e1fbb3 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/lag/LagChartItem.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/lag/LagChartItem.java @@ -3,6 +3,8 @@ import com.github.samyadaleh.cltoolbox.chartparsing.item.AbstractChartItem; import com.github.samyadaleh.cltoolbox.chartparsing.item.ChartItemInterface; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + /** * Chart Item for Left associative grammar that uses the usual itemForm for * display, but stores its own data structures for further use. @@ -31,7 +33,7 @@ public LagChartItem(String[][] categories, String[] rulePackage) { categorieRepr.append(cat); } if (categorieRepr.charAt(categorieRepr.length()-1) == '(') { - categorieRepr.append("ε"); + categorieRepr.append(EPSILON); } categorieRepr.append(")"); } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/lcfrs/LcfrsToCykRulesConverter.java b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/lcfrs/LcfrsToCykRulesConverter.java index bb3149f2..9a23e2e8 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/lcfrs/LcfrsToCykRulesConverter.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/converter/lcfrs/LcfrsToCykRulesConverter.java @@ -19,6 +19,7 @@ import java.util.List; import static com.github.samyadaleh.cltoolbox.common.Constants.DEDUCTION_RULE_LCFRS_CYK_AXIOM; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; public class LcfrsToCykRulesConverter { @@ -34,7 +35,7 @@ private static void addSrcgCykScanRules(String[] wsplit, ParsingSchema schema, String[] arg = clause.getLhs().getArgumentByIndex(i + 1); for (int j = 0; j < arg.length; j++) { if (arg[j].length() == 0) { - treeString.append("ε"); + treeString.append(EPSILON); } else { treeString.append(clause.getLhs().getSymAt(i + 1, j)); } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/cyk/TagCykAdjoin.java b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/cyk/TagCykAdjoin.java index 7e964c80..96450ded 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/cyk/TagCykAdjoin.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/cyk/TagCykAdjoin.java @@ -11,6 +11,7 @@ import com.github.samyadaleh.cltoolbox.common.tag.Tree; import static com.github.samyadaleh.cltoolbox.common.Constants.DEDUCTION_RULE_TAG_ADJOIN; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; /** * Adjoin an auxiliary tree into an appropriate node in any other tree. @@ -64,7 +65,7 @@ protected void calculateConsequences(String[] itemForm1, String[] itemForm2) { } } String node2Name = - (node2.length() > 1) ? node2.substring(0, node2.length() - 1) : "ε"; + (node2.length() > 1) ? node2.substring(0, node2.length() - 1) : EPSILON; this.name = DEDUCTION_RULE_TAG_ADJOIN + " " + treeName2 + "[" + node2Name + "," + treeName1 + "]"; diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/earley/TagEarleyAdjoin.java b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/earley/TagEarleyAdjoin.java index 723217bc..b18f13d5 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/earley/TagEarleyAdjoin.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/earley/TagEarleyAdjoin.java @@ -11,6 +11,7 @@ import com.github.samyadaleh.cltoolbox.common.tag.Tree; import static com.github.samyadaleh.cltoolbox.common.Constants.DEDUCTION_RULE_TAG_EARLEY_ADJOIN; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; /** * Combines an auxiliary tree with another tree to get a new item in which has @@ -67,7 +68,7 @@ protected void calculateConsequences(String[] itemForm1, String[] itemForm2) { } } consequence.setTrees(derivedTrees); - String node2name = node2.length() == 0 ? "ε" : node2; + String node2name = node2.length() == 0 ? EPSILON : node2; this.name = DEDUCTION_RULE_TAG_EARLEY_ADJOIN + " " + treeName2 + "[" + node2name + "," + treeName1 + "]"; diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/earley/TagEarleySubstitute.java b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/earley/TagEarleySubstitute.java index 135d0f84..69cd9cbb 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/earley/TagEarleySubstitute.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/earley/TagEarleySubstitute.java @@ -11,6 +11,7 @@ import com.github.samyadaleh.cltoolbox.common.tag.Tree; import static com.github.samyadaleh.cltoolbox.common.Constants.DEDUCTION_RULE_TAG_EARLEY_SUBSTITUTE; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; /** * If a potential initial tree is complete, substitute it if possible. @@ -63,7 +64,7 @@ public TagEarleySubstitute(String outTreeName, String outNode, Tag tag) { } // imagine a tree with 1 node where you would substitute into the root // ... - String outNodeName = outNode.length() == 0 ? "ε" : outNode; + String outNodeName = outNode.length() == 0 ? EPSILON : outNode; this.name = "substitute " + outTreeName + "[" + outNodeName + "," + treeName + "]"; diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/earleyprefixvalid/TagEarleyPrefixValidPredictAdjoined.java b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/earleyprefixvalid/TagEarleyPrefixValidPredictAdjoined.java index 7399ec49..6f4f6fa3 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/earleyprefixvalid/TagEarleyPrefixValidPredictAdjoined.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/tag/earleyprefixvalid/TagEarleyPrefixValidPredictAdjoined.java @@ -10,6 +10,7 @@ import java.util.List; import static com.github.samyadaleh.cltoolbox.common.Constants.DEDUCTION_RULE_TAG_EARLEY_PREFIXVALID_PREDICTADJOINED; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; public class TagEarleyPrefixValidPredictAdjoined extends AbstractDynamicDecutionRuleTwoAntecedences { @@ -42,7 +43,7 @@ protected void calculateConsequences(String[] itemForm1, String[] itemForm2) { new DeductionChartItem(treeName2, node2, "lb", iGamma2, m, "-", "-", m, "0"); List derivedTrees = generateDerivatedTrees(itemForm1); - String node2name = node2.length() == 0 ? "ε" : node2; + String node2name = node2.length() == 0 ? EPSILON : node2; this.name = DEDUCTION_RULE_TAG_EARLEY_PREFIXVALID_PREDICTADJOINED + " " + treeName2 + "[" + node2name + "," + treeName1 + "]"; diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/ArrayUtils.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/ArrayUtils.java index c779b4b8..0b7a4d9b 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/ArrayUtils.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/ArrayUtils.java @@ -6,6 +6,8 @@ import java.util.List; import java.util.stream.Stream; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + /** * Open collection of functions to work with arrays. */ @@ -85,7 +87,7 @@ public static String toString(String[] item) { representation.append(","); } if (element.equals("")) { - representation.append("ε"); + representation.append(EPSILON); } else { representation.append(element); } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/Constants.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/Constants.java index af3798b2..39d956fe 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/Constants.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/Constants.java @@ -4,6 +4,10 @@ * Class holding constant strings used all around the toolbox. */ public class Constants { + /* Symbols */ + public static final String EPSILON = "ε"; + public static final String ARROW_RIGHT = "->"; + /* Static rules*/ public final static String DEDUCTION_RULE_CCG_DEDUCTION_AXIOM = "axiom"; public final static String DEDUCTION_RULE_CFG_CYK_AXIOM = "scan"; diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/TreeUtils.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/TreeUtils.java index 2646c29e..2b2c0983 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/TreeUtils.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/TreeUtils.java @@ -10,11 +10,14 @@ import java.util.ArrayList; import java.util.List; +import static com.github.samyadaleh.cltoolbox.common.Constants.ARROW_RIGHT; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + public class TreeUtils { public static Tree getTreeOfSrcgClause(Clause clause, List vector) { StringBuilder extractedRule = new StringBuilder(); - extractedRule.append(clause.getLhs().getNonterminal()).append(" ->"); + extractedRule.append(clause.getLhs().getNonterminal()).append(" ").append(ARROW_RIGHT); int terminalsInLhs = 0; for (String symbol : clause.getLhs().getSymbolsAsPlainArray()) { if (!TreeUtils.symbolIsVariable(clause, symbol)) { @@ -55,7 +58,7 @@ public static Tree getTreeOfSrcgClause(Clause clause) { if (clause.getRhs().size() == 0) { try { return new Tree(new CfgProductionRule( - clause.getLhs().getNonterminal() + " -> " + ArrayUtils + clause.getLhs().getNonterminal() + " " + ARROW_RIGHT + " " + ArrayUtils .getSubSequenceAsString( clause.getLhs().getSymbolsAsPlainArray(), 0, clause.getLhs().getSymbolsAsPlainArray().length))); @@ -65,7 +68,7 @@ public static Tree getTreeOfSrcgClause(Clause clause) { } StringBuilder cfgRuleString = new StringBuilder(clause.getLhs().getNonterminal()); - cfgRuleString.append(" ->"); + cfgRuleString.append(" ").append(ARROW_RIGHT); for (Predicate rhsPred : clause.getRhs()) { cfgRuleString.append(" ").append(rhsPred.getNonterminal()); } @@ -222,7 +225,7 @@ private static List collectSubTrees(Tree tree1, Vertex node1, private static String collectSubtreeAsString(Tree tree2, Vertex node2) { StringBuilder newTree = new StringBuilder("("); if (node2.getLabel().equals("")) { - newTree.append("ε"); + newTree.append(EPSILON); } else { newTree.append(node2.getLabel()); } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/Cfg.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/Cfg.java index ecfd4e40..6c159bce 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/Cfg.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/Cfg.java @@ -10,6 +10,8 @@ import java.text.ParseException; import java.util.*; +import static com.github.samyadaleh.cltoolbox.common.Constants.ARROW_RIGHT; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; import static com.github.samyadaleh.cltoolbox.common.cfg.util.EmptyProductions.isEmptyProductionRule; import static com.github.samyadaleh.cltoolbox.common.cfg.util.EmptyProductions.nonterminalOccursInAnyRhs; @@ -52,7 +54,7 @@ public Cfg(NondeterministicFiniteAutomaton nfa) { for (String transitionState : entry.getValue()) { try { productionRules.add(new CfgProductionRule( - entry.getKey()[0] + " -> " + entry.getKey()[1] + " " + entry.getKey()[0] + " " + ARROW_RIGHT + " " + entry.getKey()[1] + " " + transitionState)); } catch (ParseException e) { throw new RuntimeException(e); @@ -61,7 +63,7 @@ public Cfg(NondeterministicFiniteAutomaton nfa) { } for (String finalState : nfa.getFinalStates()) { try { - productionRules.add(new CfgProductionRule(finalState + " -> ε")); + productionRules.add(new CfgProductionRule(finalState + " " + ARROW_RIGHT + " " + EPSILON)); } catch (ParseException e) { throw new RuntimeException(e); } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/CfgProductionRule.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/CfgProductionRule.java index 855f5f04..d81d719b 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/CfgProductionRule.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/CfgProductionRule.java @@ -2,6 +2,9 @@ import java.text.ParseException; +import static com.github.samyadaleh.cltoolbox.common.Constants.ARROW_RIGHT; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + /** * Representation of a CFG production rule where the lhs consists of one * nonterminal and the rhs can be any length. @@ -14,7 +17,7 @@ public class CfgProductionRule { public CfgProductionRule(String[] rule) { this.lhs = rule[0]; String[] ruleSplit = rule[1].split(" "); - if ((ruleSplit.length == 1 && ruleSplit[0].equals("ε"))) { + if ((ruleSplit.length == 1 && ruleSplit[0].equals(EPSILON))) { this.rhs = new String[] {""}; } else { this.rhs = ruleSplit; @@ -27,7 +30,7 @@ public CfgProductionRule(String[] rule) { */ public CfgProductionRule(String lhs, String[] rhs) { this.lhs = lhs; - if (rhs.length == 1 && rhs[0].equals("ε")) { + if (rhs.length == 1 && rhs[0].equals(EPSILON)) { this.rhs = new String[] {""}; } else { this.rhs = rhs; @@ -38,12 +41,12 @@ public CfgProductionRule(String lhs, String[] rhs) { * Creates a rule from a String representation like S -> A B */ public CfgProductionRule(String ruleString) throws ParseException { - if (!ruleString.contains("->")) { - throw new ParseException("Separator -> missing in rule " + ruleString, 0); + if (!ruleString.contains(ARROW_RIGHT)) { + throw new ParseException("Separator " + ARROW_RIGHT + " missing in rule " + ruleString, 0); } - String[] ruleSplit = ruleString.split("->", 2); + String[] ruleSplit = ruleString.split(ARROW_RIGHT, 2); this.lhs = ruleSplit[0].trim(); - if (ruleSplit[1].trim().equals("") || ruleSplit[1].trim().equals("ε")) { + if (ruleSplit[1].trim().equals("") || ruleSplit[1].trim().equals(EPSILON)) { this.rhs = new String[] {""}; } else { this.rhs = ruleSplit[1].trim().split(" "); @@ -60,9 +63,9 @@ public String[] getRhs() { @Override public String toString() { if (rhs.length == 0 || rhs[0].equals("")) { - return lhs + " -> ε"; + return lhs + " " + ARROW_RIGHT + " " + EPSILON; } else { - return lhs + " -> " + String.join(" ", rhs); + return lhs + " " + ARROW_RIGHT + " " + String.join(" ", rhs); } } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/PcfgProductionRule.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/PcfgProductionRule.java index 9c455457..2c8d35c7 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/PcfgProductionRule.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/PcfgProductionRule.java @@ -2,6 +2,9 @@ import java.text.ParseException; +import static com.github.samyadaleh.cltoolbox.common.Constants.ARROW_RIGHT; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + /** * Representation of a context-free rule where the lhs is only allowed to * contain one symbol and the rule has a probability. @@ -28,8 +31,8 @@ public class PcfgProductionRule extends CfgProductionRule { */ PcfgProductionRule(String ruleString) throws ParseException { super(ruleString.split(":", 2)[1]); - if (ruleString.indexOf("->") < ruleString.indexOf(':')) { - throw new ParseException(": has to be left of -> in rule " + ruleString, + if (ruleString.indexOf(ARROW_RIGHT) < ruleString.indexOf(':')) { + throw new ParseException(": has to be left of " + ARROW_RIGHT + " in rule " + ruleString, 0); } String[] ruleSplit = ruleString.split(":", 2); @@ -43,9 +46,9 @@ public Double getP() { @Override public String toString() { double roundedP = Math.round(p * 100.0) / 100.0; if (this.getRhs()[0].equals("")) { - return roundedP + " : " + this.getLhs() + " -> ε"; + return roundedP + " : " + this.getLhs() + " " + ARROW_RIGHT + " " + EPSILON; } else { - return roundedP + " : " + this.getLhs() + " -> " + return roundedP + " : " + this.getLhs() + " " + ARROW_RIGHT + " " + String.join(" ", this.getRhs()); } } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/Doubling.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/Doubling.java index 59bf79c3..374910f2 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/Doubling.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/Doubling.java @@ -9,6 +9,9 @@ import java.util.Arrays; import java.util.List; +import static com.github.samyadaleh.cltoolbox.common.Constants.ARROW_RIGHT; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + /** * Takes care that different nonterminals are used as left-corner and in other * places, so no symbol appears in both positions. @@ -32,7 +35,7 @@ public static Cfg doubleSymbols(Cfg cfgOld) throws ParseException { for (int i = 0; i < newRules.size(); i++) { CfgProductionRule rule = newRules.get(i); - if (rule.getRhs().length > 0 && !"ε".equals(rule.getRhs()[0])) { + if (rule.getRhs().length > 0 && !EPSILON.equals(rule.getRhs()[0])) { handleLcOccuringInOtherRules(newRules, newNonterminals, rule); } } @@ -52,7 +55,7 @@ private static void handleLcOccuringInOtherRules( // if N occurs in any other production rule not as lc: for (int j = 0; j < newRules.size(); j++) { CfgProductionRule rule2 = newRules.get(j); - if (rule2.getRhs().length > 0 && !"ε".equals(rule2.getRhs()[0])) { + if (rule2.getRhs().length > 0 && !EPSILON.equals(rule2.getRhs()[0])) { for (int k = 1; k < rule2.getRhs().length; k++) { if (lc.equals(rule2.getRhs()[k])) { if (newNt == null) { @@ -73,7 +76,7 @@ private static void duplicateRules(List newRules, CfgProductionRule rule3 = newRules.get(l); if (lc.equals(rule3.getLhs())) { newRules.add(new CfgProductionRule( - newNt + " -> " + rule3.toString().split("->")[1])); + newNt + " " + ARROW_RIGHT + " " + rule3.toString().split(ARROW_RIGHT)[1])); } } } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/Factoring.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/Factoring.java index 99f0dea8..e5c09a80 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/Factoring.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/Factoring.java @@ -7,6 +7,8 @@ import java.text.ParseException; import java.util.*; +import static com.github.samyadaleh.cltoolbox.common.Constants.ARROW_RIGHT; + /** * Util methods to handle factoring. */ @@ -74,11 +76,11 @@ private static void replacePrefix(String lhs, String rhsRest = ArrayUtils .getSubSequenceAsString(rule.getRhs(), prefLength, rule.getRhs().length); - productionRules.add(new CfgProductionRule(newNt + " -> " + rhsRest)); + productionRules.add(new CfgProductionRule(newNt + " " + ARROW_RIGHT + " " + rhsRest)); } } productionRules - .add(new CfgProductionRule(lhs + " -> " + longestPrefix + " " + newNt)); + .add(new CfgProductionRule(lhs + " " + ARROW_RIGHT + " " + longestPrefix + " " + newNt)); } private static String findLongestCommonPrefix(String lhs, diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/LeftRecursion.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/LeftRecursion.java index fe4ebbdb..ae0503dc 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/LeftRecursion.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/LeftRecursion.java @@ -9,8 +9,12 @@ import com.github.samyadaleh.cltoolbox.common.cfg.Cfg; import com.github.samyadaleh.cltoolbox.common.cfg.CfgProductionRule; +import static com.github.samyadaleh.cltoolbox.common.Constants.ARROW_RIGHT; + public class LeftRecursion { + public static final String DELIMITER = "-"; + /** * Returns true if CFG has one rule with direct left recursion, of the form A * -> A.... Remove epsilon productions to make sure no indirect left recursion @@ -149,13 +153,13 @@ public static Cfg removeLeftRecursionPaull(Cfg cfgOld) throws ParseException { * Removes any kind of left recursion including direct and indirect one, * but epsilon productions and loops have to be removed first. * Example: S -> A a, S -> B b, A -> S a, B -> b - * 1. If the LC is terminal or a non-recursive terminal add A -> X A_X - * For S -> B b add S -> B S_B. For B -> b add B -> b B_b. + * 1. If the LC is terminal or a non-recursive terminal add A -> X A-X + * For S -> B b add S -> B S-B. For B -> b add B -> b B-b. * 2. If LC is recursive and has more productions, add a new rule for each: - * For A add S_A -> a S_A. For S add A_S -> a A_S. + * For A add S-A -> a S-A. For S add A-S -> a A-S. * 3. For any rule I guess. - * For S -> A a add S_A -> a. For S -> B b add S_B -> b. - * For A -> S a add A_S -> a. For B -> b add B_b -> ε + * For S -> A a add S-A -> a. For S -> B b add S-B -> b. + * For A -> S a add A-S -> a. For B -> b add B-b -> ε * 4. For non-recursive nonterminals. Copy B -> b to new grammar. */ public static Cfg removeLeftRecursionMoore(Cfg cfgOld) throws ParseException { @@ -174,23 +178,23 @@ public static Cfg removeLeftRecursionMoore(Cfg cfgOld) throws ParseException { String lc2 = rule2.getRhs()[0]; String rhs2Rest = ArrayUtils.getSubSequenceAsString( rule2.getRhs(), 1, rule2.getRhs().length); - String newNt = lhs + "_" + lc2; - cfg.addProductionRule(newNt + " -> " + rhs2Rest); + String newNt = lhs + DELIMITER + lc2; + cfg.addProductionRule(newNt + " " + ARROW_RIGHT + " " + rhs2Rest); } } // Rule 3 if (nonterminalIsLhsOfLeftRecursion(cfgOld, lhs)) { - String newNt = lhs + "_" + lc; + String newNt = lhs + DELIMITER + lc; newNts.add(newNt); String rhsRest = ArrayUtils.getSubSequenceAsString( rule.getRhs(), 1, rule.getRhs().length); - cfg.addProductionRule(newNt + " -> " + rhsRest); + cfg.addProductionRule(newNt + " " + ARROW_RIGHT + " " + rhsRest); } } else { // Rule 1 - String newNt = lhs + "_" + lc; + String newNt = lhs + DELIMITER + lc; newNts.add(newNt); - cfg.addProductionRule(lhs + " -> " + lc + " " + newNt); + cfg.addProductionRule(lhs + " " + ARROW_RIGHT + " " + lc + " " + newNt); // Rule 4 cfg.addProductionRule(rule.toString()); } @@ -207,7 +211,7 @@ private static void removeIndirectLeftRecursion(Cfg cfg, String nt, int k, List newRules = new ArrayList<>(); for (CfgProductionRule rule2 : cfg.getProductionRules()) { if (rule2.getLhs().equals(nt2)) { - newRules.add(nt + " -> " + String.join(" ", rule2.getRhs()) + newRules.add(nt + " "+ ARROW_RIGHT + " " + String.join(" ", rule2.getRhs()) + " " + String.join(" ", bi)); } } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/Loops.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/Loops.java index e9e5af9f..3dd0abf2 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/Loops.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/Loops.java @@ -9,6 +9,8 @@ import java.util.Collections; import java.util.List; +import static com.github.samyadaleh.cltoolbox.common.Constants.ARROW_RIGHT; + public class Loops { public static Cfg getCfgWithoutLoops(Cfg cfgOld) { @@ -36,7 +38,7 @@ public static Cfg getCfgWithoutLoops(Cfg cfgOld) { } else { replaceRule.append(currentRule.getLhs()); } - replaceRule.append(" ->"); + replaceRule.append(" ").append(ARROW_RIGHT); for (String rhsSym : currentRule.getRhs()) { replaceRule.append(" "); if (loopNts.contains(rhsSym)) { @@ -46,7 +48,7 @@ public static Cfg getCfgWithoutLoops(Cfg cfgOld) { } } try { - if (!replaceRule.toString().equals(replaceNt + " -> " + replaceNt)) { + if (!replaceRule.toString().equals(replaceNt + " " + ARROW_RIGHT + " " + replaceNt)) { workingProductionRules.set(j, new CfgProductionRule(replaceRule.toString())); } else { workingProductionRules.remove(j); diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/ReverseProductionRules.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/ReverseProductionRules.java index d7d2cd00..1599184d 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/ReverseProductionRules.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/cfg/util/ReverseProductionRules.java @@ -5,6 +5,8 @@ import java.text.ParseException; +import static com.github.samyadaleh.cltoolbox.common.Constants.ARROW_RIGHT; + /** * Util class that reverses the production rules. */ @@ -20,7 +22,7 @@ public static Cfg getCfgWithReversedProductionRules(Cfg cfg) { cfgNew.setStartSymbol(cfg.getStartSymbol()); for (CfgProductionRule rule : cfg.getProductionRules()) { try { - cfgNew.addProductionRule(rule.getLhs() + " -> " + reverseRhs(rule)); + cfgNew.addProductionRule(rule.getLhs() + " " + ARROW_RIGHT + " " + reverseRhs(rule)); } catch (ParseException e) { throw new RuntimeException(e); } @@ -30,7 +32,7 @@ public static Cfg getCfgWithReversedProductionRules(Cfg cfg) { private static String reverseRhs(CfgProductionRule rule) { String ruleString = rule.toString(); - String[] ruleSplit = ruleString.split(" -> "); + String[] ruleSplit = ruleString.split(" " + ARROW_RIGHT + " "); String rhsString = ruleSplit[1]; StringBuilder reversedRhs = new StringBuilder(); String[] rhsSplit = rhsString.split(" "); diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/finiteautomata/NondeterministicFiniteAutomaton.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/finiteautomata/NondeterministicFiniteAutomaton.java index ea401d4c..c813c25b 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/finiteautomata/NondeterministicFiniteAutomaton.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/finiteautomata/NondeterministicFiniteAutomaton.java @@ -8,6 +8,8 @@ import java.text.ParseException; import java.util.*; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + /** * Representation of a nondeterministic finite automaton as defined by Hopcroft * 2011 p. 85. @@ -48,8 +50,8 @@ public NondeterministicFiniteAutomaton(Cfg cfg) throws ParseException { for (CfgProductionRule rule : cfg.getProductionRules()) { String[] rhs = rule.getRhs(); String lastState = rule.getLhs(); - if (rhs.length == 0 || rhs.length == 1 && "ε".equals(rhs[0])) { - String[] key = new String[] {lastState, "ε"}; + if (rhs.length == 0 || rhs.length == 1 && EPSILON.equals(rhs[0])) { + String[] key = new String[] {lastState, EPSILON}; if (!newTransitionFunction.containsKey(key)) { newTransitionFunction.put(key, new ArrayList<>()); } @@ -59,7 +61,7 @@ public NondeterministicFiniteAutomaton(Cfg cfg) throws ParseException { for (int j = 0; j < rhs.length; j++) { String rhsSym = rhs[j]; if (cfg.nonterminalsContain(rhsSym)) { - String[] key = new String[] {lastState, "ε"}; + String[] key = new String[] {lastState, EPSILON}; if (!newTransitionFunction.containsKey(key)) { newTransitionFunction.put(key, new ArrayList<>()); } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/finiteautomata/util/UnifyFinalStates.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/finiteautomata/util/UnifyFinalStates.java index 060cb62e..25ad867d 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/finiteautomata/util/UnifyFinalStates.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/finiteautomata/util/UnifyFinalStates.java @@ -5,6 +5,8 @@ import java.util.*; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + /** * Replaces the final states of an automaton with a single one. */ @@ -39,7 +41,7 @@ public static NondeterministicFiniteAutomaton getNfaWithSingleFinalState( .put(entry.getKey(), Arrays.asList(entry.getValue())); } for (String formerFinalState : nfa.getFinalStates()) { - String[] key = new String[] {formerFinalState, "ε"}; + String[] key = new String[] {formerFinalState, EPSILON}; if (!newTransitionFunction.containsKey(key)) { newTransitionFunction.put(key, new ArrayList<>()); } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/lag/LagRule.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/lag/LagRule.java index d49593b5..f7cf044c 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/lag/LagRule.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/lag/LagRule.java @@ -1,5 +1,7 @@ package com.github.samyadaleh.cltoolbox.common.lag; +import static com.github.samyadaleh.cltoolbox.common.Constants.ARROW_RIGHT; + public class LagRule { private String[] cat1; private String[] cat2; @@ -37,7 +39,7 @@ public void setState(LagState state) { @Override public String toString() { String repr = "[(" + String.join(" ", cat1) + ") (" + String.join(" ", cat2) - + ")] -> " + state; + + ")] " + ARROW_RIGHT + " " + state; return repr; } } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/lag/LagState.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/lag/LagState.java index 76787ed2..94bd64f4 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/lag/LagState.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/lag/LagState.java @@ -2,6 +2,8 @@ import java.util.List; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + public class LagState { private String[] rulePackage; @@ -32,7 +34,7 @@ public void setCategory(String[] category) { StringBuilder repr = new StringBuilder(); repr.append("[{").append(String.join(", ", rulePackage)).append("} "); if (category.length == 0) { - repr.append("ε"); + repr.append(EPSILON); } else { repr.append("(").append(String.join(" ", category)).append(")"); } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/lag/LagWord.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/lag/LagWord.java index 5c87ecdd..3374c8b6 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/lag/LagWord.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/lag/LagWord.java @@ -1,5 +1,7 @@ package com.github.samyadaleh.cltoolbox.common.lag; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + public class LagWord { private String wordSurface; private String[] category; @@ -29,7 +31,7 @@ public void setCategory(String[] category) { StringBuilder repr = new StringBuilder(); repr.append("[").append(wordSurface).append(" "); if (category.length == 0) { - repr.append("ε"); + repr.append(EPSILON); } else { repr.append("(").append(String.join(" ", category)).append(")"); } diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/Clause.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/Clause.java index 5c3a9b58..f6f4a829 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/Clause.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/Clause.java @@ -4,6 +4,9 @@ import java.util.ArrayList; import java.util.List; +import static com.github.samyadaleh.cltoolbox.common.Constants.ARROW_RIGHT; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + /** Representation of a clause of the form A(ɑ1,...,ɑ_dim(A)) -> * A1(X1,...,X_dim(A1)) ... */ public class Clause { @@ -28,7 +31,7 @@ public Clause(String lhs, String rhs) throws ParseException { * Does the split at "->" for you. */ public Clause(String clause) throws ParseException { - String[] clauseSplit = clause.split("->"); + String[] clauseSplit = clause.split(ARROW_RIGHT); this.lhs = new Predicate(clauseSplit[0]); String rhs = clauseSplit[1]; int start = 0; @@ -43,9 +46,9 @@ public Clause(String clause) throws ParseException { @Override public String toString() { StringBuilder repr = new StringBuilder(); repr.append(lhs.toString()); - repr.append(" -> "); + repr.append(" ").append(ARROW_RIGHT).append(" "); if (rhs.isEmpty()) { - repr.append("ε"); + repr.append(EPSILON); } else { for (int i = 0; i < rhs.size(); i++) { if (i > 0) @@ -61,9 +64,9 @@ public Clause(String clause) throws ParseException { public String setDotAt(int i, int j) { StringBuilder repr = new StringBuilder(); repr.append(lhs.setDotAt(i,j)); - repr.append(" -> "); + repr.append(" ").append(ARROW_RIGHT).append(" "); if (rhs.isEmpty()) { - repr.append("ε"); + repr.append(EPSILON); } else { for (int k = 0; k < rhs.size(); k++) { if (k > 0) diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/Predicate.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/Predicate.java index 8318d576..9d484bd0 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/Predicate.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/Predicate.java @@ -6,6 +6,8 @@ import com.github.samyadaleh.cltoolbox.common.ArrayUtils; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + /** Representation of a predicate of the form A(ɑ1,...,ɑ_dim(A)). */ public class Predicate { private final String nonterminal; @@ -33,13 +35,13 @@ public Predicate(String predicate) throws ParseException { } this.nonterminal = predicate.substring(0, lBrack).trim(); String rightOver = predicate.substring(lBrack + 1, rBrack).trim(); - if (rightOver.length() == 0 || rightOver.equals("ε")) { + if (rightOver.length() == 0 || rightOver.equals(EPSILON)) { symbols = new String[][] {new String[] {""}}; } else { String[] subgroups = rightOver.split(","); ArrayList subgroupCol = new ArrayList<>(); for (String subgroup : subgroups) { - if (subgroup.trim().equals("ε")) { + if (subgroup.trim().equals(EPSILON)) { subgroupCol.add(new String[] {""}); } else { subgroupCol.add(subgroup.trim().split(" ")); @@ -57,7 +59,7 @@ public Predicate(String predicate) throws ParseException { if (i > 0) repr.append(","); if (symbols[i].length == 1 && symbols[i][0].equals("")) { - repr.append("ε"); + repr.append(EPSILON); } else { repr.append( ArrayUtils.getSubSequenceAsString(symbols[i], 0, symbols[i].length)); diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/Srcg.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/Srcg.java index f30d128d..c8721996 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/Srcg.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/Srcg.java @@ -17,6 +17,8 @@ import java.util.ArrayList; import java.util.List; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + /** * Representation of a sRCG - simple Range Concatenation Grammar. */ @@ -55,8 +57,8 @@ public Srcg(Cfg cfg) throws ParseException { rhs.append(rhsSym).append("(").append(newVar).append(")"); } } else { - rhs.append("ε"); - lhs.append("ε"); + rhs.append(EPSILON); + lhs.append(EPSILON); } } lhs.append(")"); diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/util/EmptyProductions.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/util/EmptyProductions.java index 94ffe01b..68d42396 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/util/EmptyProductions.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/util/EmptyProductions.java @@ -11,6 +11,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + public class EmptyProductions { private static final Logger log = LogManager.getLogger(); @@ -109,7 +111,7 @@ private static void addNewStartSymbolAndHandleEmptyWord(Srcg oldSrcg, newNts.add(newS.toString()); } if (candidate[1].equals("0")) { - newSrcg.addClause(newS + "(ε) -> ε"); + newSrcg.addClause(newS + "(" + EPSILON + ") -> " + EPSILON); } else { createVariableIfNoneExists(newSrcg); newSrcg.addClause( diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/util/Order.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/util/Order.java index 628fcc27..38f80c58 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/util/Order.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/lcfrs/util/Order.java @@ -8,6 +8,8 @@ import com.github.samyadaleh.cltoolbox.common.lcfrs.Predicate; import com.github.samyadaleh.cltoolbox.common.lcfrs.Srcg; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + public class Order { public static final char ORDER_MARKING_LEFT = '['; @@ -194,7 +196,7 @@ private static String getClauseWithPositionVectors(Clause clause, newClause.append(predRep.substring(ibrack)); newClause.append(" ->"); if (clause.getRhs().isEmpty()) { - newClause.append('ε'); + newClause.append(EPSILON); } for (Predicate rhsPred : clause.getRhs()) { newNt = new StringBuilder(); diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/parser/inner/InnerLagGrammarParser.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/parser/inner/InnerLagGrammarParser.java index 0df84d54..0355107a 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/parser/inner/InnerLagGrammarParser.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/parser/inner/InnerLagGrammarParser.java @@ -13,6 +13,8 @@ import java.util.List; import java.util.Set; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + public class InnerLagGrammarParser extends InnerGrammarParser { private Lag lag; private List lexicon = new ArrayList<>(); @@ -88,7 +90,7 @@ public InnerLagGrammarParser(Lag lag, BufferedReader in) { "Something seems to be wrong with the brackets.", token.getLineNumber()); } - } else if (category.size() == 4 && "ε".equals(tokenString)){ + } else if (category.size() == 4 && EPSILON.equals(tokenString)){ states.add(new LagState(rulePackage.toArray(new String[0]), wordCategory.toArray(new String[0]))); rulePackage = new ArrayList<>(); diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/tag/Tag.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/tag/Tag.java index 7e4473f6..ada24426 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/tag/Tag.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/tag/Tag.java @@ -10,6 +10,8 @@ import java.text.ParseException; import java.util.*; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + /** * Tree adjoining grammar that consists of terminals, nonterminals, a start * symbol, some initial trees and auxiliary trees. @@ -33,7 +35,7 @@ public Tag(Cfg cfg) throws ParseException { int i = 1; for (CfgProductionRule rule : cfg.getProductionRules()) { if (rule.getRhs().length == 0 || "".equals(rule.getRhs()[0])) { - String treeString = "(" + rule.getLhs() + " ε)"; + String treeString = "(" + rule.getLhs() + " " + EPSILON + ")"; this.addInitialTree("α" + i, treeString); } else { String treeString = diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/common/tag/Tree.java b/src/main/java/com/github/samyadaleh/cltoolbox/common/tag/Tree.java index 06128ae3..7bea4a0e 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/common/tag/Tree.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/common/tag/Tree.java @@ -10,6 +10,7 @@ import java.util.Map; import static com.github.samyadaleh.cltoolbox.common.ArrayUtils.tokenize; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; /** * A non-recursive representation of a context-free tree consisting of @@ -98,7 +99,7 @@ private int handleSubscript(String[] tokens, int i) throws ParseException { private void handleChildNode(String[] tokens, List vertexPath, List children, int i) { Vertex vertex; - if (tokens[i].equals("ε")) { + if (tokens[i].equals(EPSILON)) { vertex = new Vertex(""); } else { vertex = new Vertex(tokens[i]); @@ -123,7 +124,7 @@ private int openSubTree(String[] tokens, List vertexPath, + " instead.", 0); } Vertex vertex; - if ("ε".equals(tokens[i])) { + if (EPSILON.equals(tokens[i])) { vertex = new Vertex(""); } else { vertex = new Vertex(tokens[i]); @@ -150,7 +151,7 @@ private int openSubTree(String[] tokens, List vertexPath, */ public Tree(CfgProductionRule rule) throws ParseException { this("(" + rule.getLhs() + " " + String.join(" ", - ("".equals(rule.getRhs()[0]) ? new String[] {"ε"} : rule.getRhs())) + ("".equals(rule.getRhs()[0]) ? new String[] {EPSILON} : rule.getRhs())) + ")"); } @@ -175,7 +176,7 @@ public Vertex getFoot() { return stringRepresentation; } stringRepresentation = "(" - + (this.root.getLabel().equals("") ? "ε" : this.root.getLabel()) + + (this.root.getLabel().equals("") ? EPSILON : this.root.getLabel()) + (this.root.equals(foot) ? "*" : "") + ( isInOA(this.root.getGornAddress()) ? "_OA" : "") + ( isInNA(this.root.getGornAddress()) ? "_NA" : "") + " " @@ -193,7 +194,7 @@ private String toStringAllChildren(Vertex node) { List children = getChildren(node); for (Vertex child : children) { representation.append("(") - .append(child.getLabel().equals("") ? "ε" : child.getLabel()) + .append(child.getLabel().equals("") ? EPSILON : child.getLabel()) .append(child.equals(foot) ? "*" : "") .append(isInOA(child.getGornAddress()) ? "_OA" : "") .append(isInNA(child.getGornAddress()) ? "_NA" : ""); diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/gui/AbstractDisplayTree.java b/src/main/java/com/github/samyadaleh/cltoolbox/gui/AbstractDisplayTree.java index 78c5d013..a21093ae 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/gui/AbstractDisplayTree.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/gui/AbstractDisplayTree.java @@ -7,6 +7,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import static com.github.samyadaleh.cltoolbox.common.Constants.EPSILON; + abstract class AbstractDisplayTree { private static final Logger log = LogManager.getLogger(); @@ -139,7 +141,7 @@ private static void mayDrawTagEarleyDot(DisplayTreeInterface dti, Vertex p, int height, int nodeX, String label) { if (p.getGornAddress().equals(dti.getItemForm()[1]) || (p.getGornAddress().equals("") - && dti.getItemForm()[1].equals("ε"))) { + && dti.getItemForm()[1].equals(EPSILON))) { drawTagEarleyDot(dti, height, nodeX, label); } } @@ -149,7 +151,7 @@ private static void mayDrawTagCykDot(DisplayTreeInterface dti, Vertex p, String gorn = dti.getItemForm()[1].substring(0, dti.getItemForm()[1].length() - 1); if (p.getGornAddress().equals(gorn) - || (p.getGornAddress().equals("") && gorn.equals("ε"))) { + || (p.getGornAddress().equals("") && gorn.equals(EPSILON))) { drawTagCykDot(dti, height, nodeX); } }