didn't join a$ + bx as [a,b]$ for local ctx. new unit test to chk

This commit is contained in:
Terence Parr 2012-07-22 12:57:30 -07:00
parent 8695210903
commit 829ad9191c
2 changed files with 56 additions and 10 deletions

View File

@ -249,21 +249,30 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
return new SingletonPredictionContext(parent, a.invokingState); return new SingletonPredictionContext(parent, a.invokingState);
} }
else { // a != b payloads differ else { // a != b payloads differ
if ( a.parent.equals(b.parent) ) { // see if we can collapse parents due to $+x parents if local ctx
// parents are equal, pick left one as parent to reuse PredictionContext singleParent = null;
PredictionContext parent = a.parent; if ( rootIsWildcard ) {
if ( a.parent == EMPTY ) singleParent = EMPTY; // $ + b = $
if ( b.parent == EMPTY ) singleParent = EMPTY; // a + $ = $
}
if ( a.parent.equals(b.parent) ) { // ax + bx = [a,b]x
singleParent = a.parent;
}
if ( singleParent!=null ) { // parents are same
// sort payloads and use same parent // sort payloads and use same parent
int[] payloads = {a.invokingState, b.invokingState}; int[] payloads = {a.invokingState, b.invokingState};
if ( a.invokingState > b.invokingState ) { if ( a.invokingState > b.invokingState ) {
payloads = new int[] {b.invokingState, a.invokingState}; payloads[0] = b.invokingState;
payloads[1] = a.invokingState;
} }
PredictionContext[] parents = {parent, parent}; PredictionContext[] parents = {singleParent, singleParent};
ArrayPredictionContext joined = ArrayPredictionContext joined =
new ArrayPredictionContext(parents, payloads); new ArrayPredictionContext(parents, payloads);
return joined; return joined;
} }
// parents differ, just pack together into array; can't merge. // parents differ and can't merge them. Just pack together
// sort though by payload // into array; can't merge. sort, though, by payload
// ax + by = [ax,by]
int[] payloads = {a.invokingState, b.invokingState}; int[] payloads = {a.invokingState, b.invokingState};
PredictionContext[] parents = {a.parent, b.parent}; PredictionContext[] parents = {a.parent, b.parent};
if ( a.invokingState > b.invokingState ) { if ( a.invokingState > b.invokingState ) {
@ -284,8 +293,8 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
boolean rootIsWildcard) boolean rootIsWildcard)
{ {
if ( rootIsWildcard ) { if ( rootIsWildcard ) {
if ( a == EMPTY ) return a; // * + b = * if ( a == EMPTY ) return EMPTY; // * + b = *
if ( b == EMPTY ) return b; // a + * = * if ( b == EMPTY ) return EMPTY; // a + * = *
} }
else { else {
if ( a == EMPTY && b == EMPTY ) return EMPTY; // $ + $ = $ if ( a == EMPTY && b == EMPTY ) return EMPTY; // $ + $ = $

View File

@ -149,7 +149,7 @@ public class TestGraphNodes extends TestCase {
assertEquals(expecting, PredictionContext.toDotString(r)); assertEquals(expecting, PredictionContext.toDotString(r));
} }
@Test public void test_ax_bx() { @Test public void test_ax_bx_same_x() {
PredictionContext x = createSingleton(PredictionContext.EMPTY, 9); PredictionContext x = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext a = createSingleton(x, 1); PredictionContext a = createSingleton(x, 1);
PredictionContext b = createSingleton(x, 2); PredictionContext b = createSingleton(x, 2);
@ -168,6 +168,43 @@ public class TestGraphNodes extends TestCase {
assertEquals(expecting, PredictionContext.toDotString(r)); assertEquals(expecting, PredictionContext.toDotString(r));
} }
@Test public void test_ax_bx() {
PredictionContext x1 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext a = createSingleton(x1, 1);
PredictionContext b = createSingleton(x2, 2);
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s5 [shape=box, label=\"[1, 2]\"];\n" +
" s1 [label=\"9\"];\n" +
" s0 [label=\"$\"];\n" +
" s5->s1 [label=\"parent[0]\"];\n" +
" s5->s1 [label=\"parent[1]\"];\n" +
" s1->s0;\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_a$_bx() {
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext a = createSingleton(PredictionContext.EMPTY, 1);
PredictionContext b = createSingleton(x2, 2);
PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard());
System.out.println(PredictionContext.toDotString(r));
String expecting =
"digraph G {\n" +
"rankdir=LR;\n" +
" s4 [shape=box, label=\"[1, 2]\"];\n" +
" s0 [label=\"$\"];\n" +
" s4->s0 [label=\"parent[0]\"];\n" +
" s4->s0 [label=\"parent[1]\"];\n" +
"}\n";
assertEquals(expecting, PredictionContext.toDotString(r));
}
@Test public void test_aex_bfx() { @Test public void test_aex_bfx() {
PredictionContext x1 = createSingleton(PredictionContext.EMPTY, 9); PredictionContext x1 = createSingleton(PredictionContext.EMPTY, 9);
PredictionContext x2 = createSingleton(PredictionContext.EMPTY, 9); PredictionContext x2 = createSingleton(PredictionContext.EMPTY, 9);