size!=cardinality with BitSet.
This commit is contained in:
parent
a3a734759e
commit
2ecfe2671a
|
@ -255,7 +255,7 @@ import java.util.Set;
|
||||||
|
|
||||||
*/
|
*/
|
||||||
public class ParserATNSimulator extends ATNSimulator {
|
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 debug_list_atn_decisions = false;
|
||||||
public static boolean dfa_debug = false;
|
public static boolean dfa_debug = false;
|
||||||
public static boolean retry_debug = false;
|
public static boolean retry_debug = false;
|
||||||
|
@ -672,6 +672,7 @@ public class ParserATNSimulator extends ATNSimulator {
|
||||||
|
|
||||||
if ( debug ) {
|
if ( debug ) {
|
||||||
System.out.println("SLL altSubSets="+altSubSets+
|
System.out.println("SLL altSubSets="+altSubSets+
|
||||||
|
", configs="+reach+
|
||||||
", predict="+predictedAlt+", allSubsetsConflict="+
|
", predict="+predictedAlt+", allSubsetsConflict="+
|
||||||
allSubsetsConflict(altSubSets)+", conflictingAlts="+
|
allSubsetsConflict(altSubSets)+", conflictingAlts="+
|
||||||
getConflictingAlts(reach));
|
getConflictingAlts(reach));
|
||||||
|
@ -753,7 +754,7 @@ public class ParserATNSimulator extends ATNSimulator {
|
||||||
BitSet alts = evalSemanticContext(D.predicates, outerContext, true);
|
BitSet alts = evalSemanticContext(D.predicates, outerContext, true);
|
||||||
D.prediction = ATN.INVALID_ALT_NUMBER; // indicate we have preds
|
D.prediction = ATN.INVALID_ALT_NUMBER; // indicate we have preds
|
||||||
addDFAEdge(dfa, previousD, t, D);
|
addDFAEdge(dfa, previousD, t, D);
|
||||||
switch (alts.size()) {
|
switch (alts.cardinality()) {
|
||||||
case 0:
|
case 0:
|
||||||
throw noViableAlt(input, outerContext, D.configs, startIndex);
|
throw noViableAlt(input, outerContext, D.configs, startIndex);
|
||||||
|
|
||||||
|
@ -828,15 +829,12 @@ public class ParserATNSimulator extends ATNSimulator {
|
||||||
System.out.println("LL altSubSets="+altSubSets+
|
System.out.println("LL altSubSets="+altSubSets+
|
||||||
", predict="+getUniqueAlt(altSubSets)+
|
", predict="+getUniqueAlt(altSubSets)+
|
||||||
", resolvesToJustOneViableAlt="+
|
", resolvesToJustOneViableAlt="+
|
||||||
resolvesToJustOneViableAlt(altSubSets)+
|
resolvesToJustOneViableAlt(altSubSets));
|
||||||
", conflictingAlts="+
|
|
||||||
getConflictingAlts(reach));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// System.out.println("altSubSets: "+altSubSets);
|
// System.out.println("altSubSets: "+altSubSets);
|
||||||
reach.uniqueAlt = getUniqueAlt(altSubSets);
|
reach.uniqueAlt = getUniqueAlt(altSubSets);
|
||||||
if ( reach.uniqueAlt!=ATN.INVALID_ALT_NUMBER ) break;
|
if ( reach.uniqueAlt!=ATN.INVALID_ALT_NUMBER ) break;
|
||||||
reach.conflictingAlts = getConflictingAlts(reach);
|
|
||||||
if ( resolvesToJustOneViableAlt(altSubSets) ) break;
|
if ( resolvesToJustOneViableAlt(altSubSets) ) break;
|
||||||
previous = reach;
|
previous = reach;
|
||||||
input.consume();
|
input.consume();
|
||||||
|
@ -878,7 +876,7 @@ public class ParserATNSimulator extends ATNSimulator {
|
||||||
reportAmbiguity(dfa, D, startIndex, input.index(), reach.conflictingAlts, reach);
|
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,
|
protected ATNConfigSet computeReachSet(ATNConfigSet closure, int t,
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
grammar T;
|
grammar T;
|
||||||
s : expr expr
|
s : expr[0] ;
|
||||||
| expr
|
|
||||||
|
expr[int _p]
|
||||||
|
: ID
|
||||||
|
( {5 >= $_p}? '*' expr[6]
|
||||||
|
| {4 >= $_p}? '+' expr[5]
|
||||||
|
)*
|
||||||
;
|
;
|
||||||
expr: '@'
|
|
||||||
| ID '@'
|
ID : [a-zA-Z]+ ; // match identifiers
|
||||||
| ID
|
WS : [ \t\r\n]+ -> skip ; // toss out whitespace
|
||||||
;
|
|
||||||
ID : [a-z]+ ;
|
|
||||||
WS : [ \r\n\t]+ -> skip ;
|
|
||||||
|
|
|
@ -247,8 +247,33 @@ public class TestFullContextParsing extends BaseTest {
|
||||||
assertEquals("pass: a(i)<-x\n", found);
|
assertEquals("pass: a(i)<-x\n", found);
|
||||||
|
|
||||||
String expecting =
|
String expecting =
|
||||||
"line 1:3 reportAttemptingFullContext d=3, input='a(i)'\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";
|
"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);
|
assertEquals(expecting, this.stderrDuringParse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -331,7 +331,7 @@ public class TestLeftRecursion extends BaseTest {
|
||||||
assertEquals("line 1:1 reportAttemptingFullContext d=3, input='+'\n" +
|
assertEquals("line 1:1 reportAttemptingFullContext d=3, input='+'\n" +
|
||||||
"line 1:1 reportContextSensitivity d=3, input='+'\n" +
|
"line 1:1 reportContextSensitivity d=3, input='+'\n" +
|
||||||
"line 1:3 reportAttemptingFullContext 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);
|
stderrDuringParse);
|
||||||
|
|
||||||
result = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "prog", "(1+2)*3\n", true);
|
result = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "prog", "(1+2)*3\n", true);
|
||||||
|
|
|
@ -35,6 +35,24 @@ public class TestSemPredEvalParser extends BaseTest {
|
||||||
// TEST VALIDATING PREDS
|
// TEST VALIDATING PREDS
|
||||||
|
|
||||||
@Test public void testSimpleValidate() throws Exception {
|
@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 =
|
String grammar =
|
||||||
"grammar T;\n" +
|
"grammar T;\n" +
|
||||||
"s : a a a;\n" +
|
"s : a a a;\n" +
|
||||||
|
@ -154,9 +172,9 @@ public class TestSemPredEvalParser extends BaseTest {
|
||||||
"alt 1\n";
|
"alt 1\n";
|
||||||
assertEquals(expecting, found);
|
assertEquals(expecting, found);
|
||||||
assertEquals("line 1:0 reportAttemptingFullContext d=0, input='x'\n" +
|
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 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);
|
this.stderrDuringParse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,9 +206,9 @@ public class TestSemPredEvalParser extends BaseTest {
|
||||||
"alt 2\n";
|
"alt 2\n";
|
||||||
assertEquals(expecting, found);
|
assertEquals(expecting, found);
|
||||||
assertEquals("line 1:4 reportAttemptingFullContext d=0, input='x'\n" +
|
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 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);
|
this.stderrDuringParse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue