Skip to content

Commit

Permalink
#301 fix tree generation
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyaDaleh committed Dec 12, 2023
1 parent af37313 commit 1be82f0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -546,21 +546,32 @@ private static void mergeTrees(BottomUpChartItem oldItem,
Map<Integer, List<Tree>> oldTreeMap = oldStackState.get(i).getSecond();
Map<Integer, List<Tree>> newTreeMap = newStackState.get(i).getSecond();

for (Map.Entry<Integer, List<Tree>> 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<Integer, List<Tree>> oldTreeMap,
Map<Integer, List<Tree>> newTreeMap) {
for (Map.Entry<Integer, List<Tree>> newTreeEntry
: newTreeMap.entrySet()) {
Integer key = newTreeEntry.getKey();
List<Tree> newTrees = newTreeEntry.getValue();
if (oldTreeMap.containsKey(key)) {
List<Tree> 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..
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public CfgBottomUpReduce(CfgProductionRule rule) {
}
BottomUpChartItem consequence = createConsequence(i, gamma);
List<Pair<String, Map<Integer, List<Tree>>>> derivedTrees =
new ArrayList<>(((BottomUpChartItem) antecedences.get(0)).getStackState());
duplicateStackState((
(BottomUpChartItem) antecedences.get(0)).getStackState());
try {
Tree derivedTreeBase = new Tree(rule);
if (derivedTrees.size() == 0) {
Expand Down Expand Up @@ -80,6 +81,19 @@ public CfgBottomUpReduce(CfgProductionRule rule) {
return consequences;
}

private List<Pair<String, Map<Integer, List<Tree>>>> duplicateStackState(
List<Pair<String, Map<Integer, List<Tree>>>> stackState) {
List<Pair<String, Map<Integer, List<Tree>>>> duplicateStackState =
new ArrayList<>();
for (Pair<String, Map<Integer, List<Tree>>> pair : stackState) {
Map<Integer, List<Tree>> mapCopy = pair.getSecond();
Pair<String, Map<Integer, List<Tree>>> dupPair =
new Pair<>(pair.getFirst(), mapCopy);
duplicateStackState.add(dupPair);
}
return duplicateStackState;
}

private void handleDerivedTreeBaseList(
List<Pair<String, Map<Integer, List<Tree>>>> derivedTrees,
Tree derivedTreeBase) {
Expand All @@ -97,7 +111,7 @@ private RhsTreesListAndRequiredLength getRhsTreesListAndRequiredLength(
List<Pair<String, Map<Integer, List<Tree>>>> rhsTreesLists = new ArrayList<>();
List<String> rhsSymbols = Arrays.asList(rule.getRhs());

int rhsIndex = rhsSymbols.size()-1;
int rhsIndex = rhsSymbols.size() - 1;
int terminalsBetween = 0;
int requiredLength = 0;
for (Pair<String, Map<Integer, List<Tree>>> treePair : derivedTrees) {
Expand Down

0 comments on commit 1be82f0

Please sign in to comment.