size!=cardinality with BitSet.

This commit is contained in:
Terence Parr 2012-10-11 20:20:02 -07:00
parent a3a734759e
commit 2ecfe2671a
5 changed files with 65 additions and 22 deletions

View File

@ -255,7 +255,7 @@ import java.util.Set;
*/
public class ParserATNSimulator extends ATNSimulator {
public static boolean debug = false;
public static boolean debug = true;
public static boolean debug_list_atn_decisions = false;
public static boolean dfa_debug = false;
public static boolean retry_debug = false;
@ -672,6 +672,7 @@ public class ParserATNSimulator extends ATNSimulator {
if ( debug ) {
System.out.println("SLL altSubSets="+altSubSets+
", configs="+reach+
", predict="+predictedAlt+", allSubsetsConflict="+
allSubsetsConflict(altSubSets)+", conflictingAlts="+
getConflictingAlts(reach));
@ -753,7 +754,7 @@ public class ParserATNSimulator extends ATNSimulator {
BitSet alts = evalSemanticContext(D.predicates, outerContext, true);
D.prediction = ATN.INVALID_ALT_NUMBER; // indicate we have preds
addDFAEdge(dfa, previousD, t, D);
switch (alts.size()) {
switch (alts.cardinality()) {
case 0:
throw noViableAlt(input, outerContext, D.configs, startIndex);
@ -828,15 +829,12 @@ public class ParserATNSimulator extends ATNSimulator {
System.out.println("LL altSubSets="+altSubSets+
", predict="+getUniqueAlt(altSubSets)+
", resolvesToJustOneViableAlt="+
resolvesToJustOneViableAlt(altSubSets)+
", conflictingAlts="+
getConflictingAlts(reach));
resolvesToJustOneViableAlt(altSubSets));
}
// System.out.println("altSubSets: "+altSubSets);
reach.uniqueAlt = getUniqueAlt(altSubSets);
if ( reach.uniqueAlt!=ATN.INVALID_ALT_NUMBER ) break;
reach.conflictingAlts = getConflictingAlts(reach);
if ( resolvesToJustOneViableAlt(altSubSets) ) break;
previous = reach;
input.consume();
@ -878,7 +876,7 @@ public class ParserATNSimulator extends ATNSimulator {
reportAmbiguity(dfa, D, startIndex, input.index(), reach.conflictingAlts, reach);
}
return reach.conflictingAlts.nextSetBit(0);
return getConflictingAlts(reach).nextSetBit(0);
}
protected ATNConfigSet computeReachSet(ATNConfigSet closure, int t,

View File

@ -1,10 +1,12 @@
grammar T;
s : expr expr
| expr
s : expr[0] ;
expr[int _p]
: ID
( {5 >= $_p}? '*' expr[6]
| {4 >= $_p}? '+' expr[5]
)*
;
expr: '@'
| ID '@'
| ID
;
ID : [a-z]+ ;
WS : [ \r\n\t]+ -> skip ;
ID : [a-zA-Z]+ ; // match identifiers
WS : [ \t\r\n]+ -> skip ; // toss out whitespace

View File

@ -247,8 +247,33 @@ public class TestFullContextParsing extends BaseTest {
assertEquals("pass: a(i)<-x\n", found);
String expecting =
"line 1:3 reportAttemptingFullContext d=3, input='a(i)'\n" +
"line 1:7 reportAmbiguity d=3: ambigAlts={2..3}, input='a(i)<-x'\n";
"line 1:7 reportAttemptingFullContext d=3, input='a(i)<-x'\n" +
"line 1:7 reportAmbiguity d=3: ambigAlts={2, 3}, input='a(i)<-x'\n";
assertEquals(expecting, this.stderrDuringParse);
}
@Test
public void testTrueAmbiguityNoLoop() throws Exception {
// simpler version of testLoopsSimulateTailRecursion, no loops
String grammar =
"grammar T;\n" +
"prog: expr expr {System.out.println(\"alt 1\");}\n" +
" | expr\n" +
" ;\n" +
"expr: '@'\n" +
" | ID '@'\n" +
" | ID\n" +
" ;\n" +
"ID : [a-z]+ ;\n" +
"WS : [ \r\n\t]+ -> skip ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "prog", "a@", true);
assertEquals("alt 1\n", found);
String expecting =
"line 1:2 reportAmbiguity d=0: ambigAlts={1, 2}, input='a@'\n" +
"line 1:2 reportAttemptingFullContext d=1, input='a@'\n" +
"line 1:2 reportContextSensitivity d=1, input='a@'\n";
assertEquals(expecting, this.stderrDuringParse);
}

View File

@ -331,7 +331,7 @@ public class TestLeftRecursion extends BaseTest {
assertEquals("line 1:1 reportAttemptingFullContext d=3, input='+'\n" +
"line 1:1 reportContextSensitivity d=3, input='+'\n" +
"line 1:3 reportAttemptingFullContext d=3, input='*'\n" +
"line 1:3 reportAmbiguity d=3: ambigAlts={1..2}, input='*'\n",
"line 1:3 reportAmbiguity d=3: ambigAlts={1, 2}, input='*'\n",
stderrDuringParse);
result = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "prog", "(1+2)*3\n", true);

View File

@ -35,6 +35,24 @@ public class TestSemPredEvalParser extends BaseTest {
// TEST VALIDATING PREDS
@Test public void testSimpleValidate() throws Exception {
String grammar =
"grammar T;\n" +
"s : a ;\n" +
"a : {false}? ID {System.out.println(\"alt 1\");}\n" +
" | {true}? INT {System.out.println(\"alt 2\");}\n" +
" ;\n" +
"ID : 'a'..'z'+ ;\n" +
"INT : '0'..'9'+;\n" +
"WS : (' '|'\\n') {skip();} ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s",
"x", false);
String expecting = "line 1:0 no viable alternative at input 'x'\n";
assertEquals(expecting, stderrDuringParse);
}
@Test public void testSimpleValidate2() throws Exception {
String grammar =
"grammar T;\n" +
"s : a a a;\n" +
@ -154,9 +172,9 @@ public class TestSemPredEvalParser extends BaseTest {
"alt 1\n";
assertEquals(expecting, found);
assertEquals("line 1:0 reportAttemptingFullContext d=0, input='x'\n" +
"line 1:0 reportAmbiguity d=0: ambigAlts={1..2}, input='x'\n" +
"line 1:0 reportAmbiguity d=0: ambigAlts={1, 2}, input='x'\n" +
"line 1:3 reportAttemptingFullContext d=0, input='y'\n" +
"line 1:3 reportAmbiguity d=0: ambigAlts={1..2}, input='y'\n",
"line 1:3 reportAmbiguity d=0: ambigAlts={1, 2}, input='y'\n",
this.stderrDuringParse);
}
@ -188,9 +206,9 @@ public class TestSemPredEvalParser extends BaseTest {
"alt 2\n";
assertEquals(expecting, found);
assertEquals("line 1:4 reportAttemptingFullContext d=0, input='x'\n" +
"line 1:4 reportAmbiguity d=0: ambigAlts={2..3}, input='x'\n" +
"line 1:4 reportAmbiguity d=0: ambigAlts={2, 3}, input='x'\n" +
"line 1:7 reportAttemptingFullContext d=0, input='y'\n" +
"line 1:7 reportAmbiguity d=0: ambigAlts={2..3}, input='y'\n",
"line 1:7 reportAmbiguity d=0: ambigAlts={2, 3}, input='y'\n",
this.stderrDuringParse);
}