Skip to content

Commit

Permalink
#296 fix new SR tree generation
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyaDaleh committed Jun 16, 2023
1 parent c0e5975 commit a82d0d9
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ private static void mergeTrees(BottomUpChartItem oldItem,
List<Pair<String, Map<Integer, List<Tree>>>> newStackState =
newItem.getStackState();

for (int i = 0; i < oldStackState.size(); i++) {
for (int i = 0; i < oldStackState.size() && i < newStackState.size(); i++) {
Map<Integer, List<Tree>> oldTreeMap = oldStackState.get(i).getSecond();
Map<Integer, List<Tree>> newTreeMap = newStackState.get(i).getSecond();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,35 @@ private void handleDerivedTreeBaseList(
addTreesForLengthToDerivedTrees(derivedTrees, derivedTreeBaseList, length);
}

/**
* requiredLength sums up the lengths of the subtrees that will be combined in the rhs
*/
private RhsTreesListAndRequiredLength getRhsTreesListAndRequiredLength(
List<Pair<String, Map<Integer, List<Tree>>>> derivedTrees) {
List<Pair<String, Map<Integer, List<Tree>>>> rhsTreesLists = new ArrayList<>();
List<String> rhsSymbols = Arrays.asList(rule.getRhs());

int rhsIndex = 0;
int rhsIndex = rhsSymbols.size()-1;
int terminalsBetween = 0;
int requiredLength = 0;
for (Pair<String, Map<Integer, List<Tree>>> treePair : derivedTrees) {
while (rhsIndex < rhsSymbols.size()
while (rhsIndex >= 0
&& !treePair.getFirst().equals(rhsSymbols.get(rhsIndex))) {
rhsIndex++; // Skip over non-matching symbols
rhsIndex--; // Skip over non-matching symbols
terminalsBetween++;
}
if (rhsIndex < rhsSymbols.size()
if (rhsIndex >= 0
&& treePair.getFirst().equals(rhsSymbols.get(rhsIndex))) {
rhsTreesLists.add(treePair);
requiredLength
+= treePair.getSecond().entrySet().iterator().next().getKey();
rhsIndex++; // Move to the next symbol
rhsIndex--; // Move to the next symbol
}
}
while (rhsIndex >= 0) {
rhsIndex--; // Skip over non-matching symbols
terminalsBetween++;
}
return new RhsTreesListAndRequiredLength(
rhsTreesLists, terminalsBetween, requiredLength);
}
Expand Down Expand Up @@ -162,7 +169,7 @@ private BottomUpChartItem createConsequence(String i, String gamma) {
return consequence;
}

public Map<Integer, List<List<Tree>>> generateCombinations(
private Map<Integer, List<List<Tree>>> generateCombinations(
List<Pair<String, Map<Integer, List<Tree>>>> rhsTreesLists, int requiredLength) {
Map<Integer, List<List<Tree>>> result = new HashMap<>();

Expand Down Expand Up @@ -214,7 +221,7 @@ public Map<Integer, List<List<Tree>>> generateCombinations(

@Override public String toString() {
return "[Γ " + ArrayUtils.toString(rule.getRhs()) + ",i]" + "\n______"
+ rule.toString() + "\n" + "[Γ " + rule.getLhs() + ",i]";
+ rule + "\n" + "[Γ " + rule.getLhs() + ",i]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class BottomUpChartItem extends DeductionChartItem {
/** List of entries for every stack symbol. The key of Pair is
* the stack symbol which is the same as the tree root. The map key is the
* length of the trees in terminal symbols. Both values stored for convenience.
* The top of the stack is at 0, and for shift-reduce the stack in the item has its top to the right.
* */
private List<Pair<String, Map<Integer, List<Tree>>>> stackState;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ public class DeductionTest {
assertEquals(2, data.length);
}

@Test public void testCfgShiftreduceIndexOOB()
throws ParseException, FileNotFoundException {
Cfg cfg = GrammarLoader.readCfg("sr-ioob.cfg");
String w = "t0 t0";
ParsingSchema schema = CfgToShiftReduceRulesConverter.cfgToShiftReduceRules(
cfg, w);
Deduction deduction = new Deduction();
assertTrue(deduction.doParse(schema, false));
String[][] data = deduction.getTraceTable();
deduction.printTrace(data);
assertEquals(16, deduction.getDerivedTrees().size());
}

@Test public void testCfgEarley() throws ParseException,
FileNotFoundException {
Cfg cfg = GrammarLoader.readCfg("anbn.cfg");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.github.samyadaleh.cltoolbox.chartparsing.cfg.shiftreduce;

import com.github.samyadaleh.cltoolbox.chartparsing.item.BottomUpChartItem;
import com.github.samyadaleh.cltoolbox.chartparsing.item.ChartItemInterface;
import com.github.samyadaleh.cltoolbox.chartparsing.item.Pair;
import com.github.samyadaleh.cltoolbox.common.cfg.CfgProductionRule;
import com.github.samyadaleh.cltoolbox.common.tag.Tree;
import org.junit.Test;

import java.text.ParseException;
import java.util.*;

import static org.junit.Assert.assertEquals;

public class CfgBottomUpReduceTest {

@Test
public void testTreeGeneration() throws ParseException {
CfgBottomUpReduce rule = new CfgBottomUpReduce(new CfgProductionRule("S -> S N2"));
BottomUpChartItem antecedence = new BottomUpChartItem("S1 S N2", "2");
List<Pair<String, Map<Integer, List<Tree>>>> stackState = new ArrayList<>();
// 0 = {Pair@3650} "N2 : {1=[(N2 (t0 ))]}"
Map<Integer, List<Tree>> map1 = new HashMap<>();
map1.put(1, Collections.singletonList(new Tree("(N2 (t0 ))")));
Pair<String, Map<Integer, List<Tree>>> stack1 = new Pair<>("N2", map1);
stackState.add(stack1);
// 1 = {Pair@3651} "S : {1=[(S (t0 )), (S (N2 (t0 ))), (S (N1 (S (t0 )))), (S (N1 (S (N2 (t0 )))))]}"
Map<Integer, List<Tree>> map2 = new HashMap<>();
map2.put(1, Arrays.asList(new Tree("(S (t0 ))"),
new Tree("(S (N2 (t0 )))"),
new Tree("(S (N1 (S (t0 ))))"),
new Tree("(S (N1 (S (N2 (t0 ))))")));
Pair<String, Map<Integer, List<Tree>>> stack2 = new Pair<>("S", map2);
stackState.add(stack2);
// 2 = {Pair@3652} "S1 : {0=[(S1 (ε ))], 1=[(S1 (S (t0 ))), (S1 (S (N2 (t0 )))), (S1 (S (N1 (S (t0 ))))), (S1 (S (N1 (S (N2 (t0 ))))))]}"
Map<Integer, List<Tree>> map3 = new HashMap<>();
map3.put(0, Collections.singletonList(new Tree("(S1 (ε ))")));
map3.put(1, Arrays.asList(new Tree("(S1 (S (t0 )))"),
new Tree("(S1 (S (N2 (t0 )))"),
new Tree("(S1 (S (N1 (S (t0 ))))"),
new Tree("(S1 (S (N1 (S (N2 (t0 )))))")));
Pair<String, Map<Integer, List<Tree>>> stack3 = new Pair<>("S1", map3);
stackState.add(stack3);
antecedence.setStackState(stackState);
rule.clearItems();
rule.setAntecedences(Collections.singletonList(antecedence));
ChartItemInterface consequence = rule.getConsequences().get(0);
assertEquals(9, consequence.getTrees().size());
}

}
5 changes: 5 additions & 0 deletions src/test/resources/grammars/cfg/sr-ioob.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
G = <N, T, S, P>
N = {S1, S, N2, N1}
T = {t0}
S = S1
P = {N1 -> S, S -> N1, N2 -> t0 S, S -> t0, S -> S N2, N2 -> t0, S -> N2, S1 -> S, S1 -> ε}

0 comments on commit a82d0d9

Please sign in to comment.