From 1be82f02e2c6967ff90ec154d2f6434d0e96e7cb Mon Sep 17 00:00:00 2001 From: SamyaDaleh Date: Tue, 12 Dec 2023 13:00:50 +0100 Subject: [PATCH] #301 fix tree generation --- .../cltoolbox/chartparsing/Deduction.java | 31 +++++++++++++------ .../cfg/shiftreduce/CfgBottomUpReduce.java | 18 +++++++++-- 2 files changed, 37 insertions(+), 12 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 5a05ec1..3c52a67 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/Deduction.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/Deduction.java @@ -546,21 +546,32 @@ private static void mergeTrees(BottomUpChartItem oldItem, Map> oldTreeMap = oldStackState.get(i).getSecond(); Map> newTreeMap = newStackState.get(i).getSecond(); - for (Map.Entry> newTreeEntry : newTreeMap.entrySet()) { - oldTreeMap.merge(newTreeEntry.getKey(), newTreeEntry.getValue(), - (oldTrees, newTrees) -> { - for (Tree newTree : newTrees) { - if (!oldTrees.contains(newTree)) { - oldTrees.add(newTree); - } - } - return oldTrees; - }); + combineMaps(oldTreeMap, newTreeMap); + } + } + + private static void combineMaps( + Map> oldTreeMap, + Map> newTreeMap) { + for (Map.Entry> newTreeEntry + : newTreeMap.entrySet()) { + Integer key = newTreeEntry.getKey(); + List newTrees = newTreeEntry.getValue(); + if (oldTreeMap.containsKey(key)) { + List oldTrees = oldTreeMap.get(key); + for (Tree newTree : newTrees) { + if (!oldTrees.contains(newTree)) { + oldTrees.add(newTree); + } + } + } else { + oldTreeMap.put(key, newTrees); } } } + /** * Pretty-prints rows of the parsing process by filling up all columns up to a * specific length with spaces.. diff --git a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/shiftreduce/CfgBottomUpReduce.java b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/shiftreduce/CfgBottomUpReduce.java index ae66548..098f6cc 100644 --- a/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/shiftreduce/CfgBottomUpReduce.java +++ b/src/main/java/com/github/samyadaleh/cltoolbox/chartparsing/cfg/shiftreduce/CfgBottomUpReduce.java @@ -42,7 +42,8 @@ public CfgBottomUpReduce(CfgProductionRule rule) { } BottomUpChartItem consequence = createConsequence(i, gamma); List>>> derivedTrees = - new ArrayList<>(((BottomUpChartItem) antecedences.get(0)).getStackState()); + duplicateStackState(( + (BottomUpChartItem) antecedences.get(0)).getStackState()); try { Tree derivedTreeBase = new Tree(rule); if (derivedTrees.size() == 0) { @@ -80,6 +81,19 @@ public CfgBottomUpReduce(CfgProductionRule rule) { return consequences; } + private List>>> duplicateStackState( + List>>> stackState) { + List>>> duplicateStackState = + new ArrayList<>(); + for (Pair>> pair : stackState) { + Map> mapCopy = pair.getSecond(); + Pair>> dupPair = + new Pair<>(pair.getFirst(), mapCopy); + duplicateStackState.add(dupPair); + } + return duplicateStackState; + } + private void handleDerivedTreeBaseList( List>>> derivedTrees, Tree derivedTreeBase) { @@ -97,7 +111,7 @@ private RhsTreesListAndRequiredLength getRhsTreesListAndRequiredLength( List>>> rhsTreesLists = new ArrayList<>(); List rhsSymbols = Arrays.asList(rule.getRhs()); - int rhsIndex = rhsSymbols.size()-1; + int rhsIndex = rhsSymbols.size() - 1; int terminalsBetween = 0; int requiredLength = 0; for (Pair>> treePair : derivedTrees) {