Must check rule transition follow states before eliminating states in the ATN (fixes #224)

This commit is contained in:
Sam Harwell 2013-04-18 14:43:18 -05:00
parent bf4f198fbf
commit 7a7f4a7851
2 changed files with 26 additions and 1 deletions

View File

@ -431,7 +431,7 @@ public class ParserATNFactory implements ATNFactory {
boolean isRuleTrans = tr instanceof RuleTransition;
if ( el.left.getStateType() == ATNState.BASIC &&
el.right.getStateType()== ATNState.BASIC &&
tr!=null && (isRuleTrans || tr.target == el.right) )
tr!=null && (isRuleTrans && ((RuleTransition)tr).followState == el.right || tr.target == el.right) )
{
// we can avoid epsilon edge to next el
if ( isRuleTrans ) ((RuleTransition)tr).followState = els.get(i+1).left;

View File

@ -84,6 +84,31 @@ public class TestLexerExec extends BaseTest {
assertEquals(expecting, found);
}
/**
* This is a regression test for antlr/antlr4#224: "Parentheses without
* quantifier in lexer rules have unclear effect".
* https://github.com/antlr/antlr4/issues/224
*/
@Test public void testParentheses() {
String grammar =
"lexer grammar Demo;\n" +
"\n" +
"START_BLOCK: '-.-.-';\n" +
"\n" +
"ID : (LETTER SEPARATOR) (LETTER SEPARATOR)+;\n" +
"fragment LETTER: L_A|L_K;\n" +
"fragment L_A: '.-';\n" +
"fragment L_K: '-.-';\n" +
"\n" +
"SEPARATOR: '!';\n";
String found = execLexer("Demo.g4", grammar, "Demo", "-.-.-!");
String expecting =
"[@0,0:4='-.-.-',<1>,1:0]\n" +
"[@1,5:5='!',<3>,1:5]\n" +
"[@2,6:5='<EOF>',<-1>,1:6]\n";
assertEquals(expecting, found);
}
@Test
public void testNonGreedyTermination() throws Exception {
String grammar =