diff --git a/tool/src/org/antlr/v4/semantics/SymbolChecks.java b/tool/src/org/antlr/v4/semantics/SymbolChecks.java index 63221bd81..745ed5687 100644 --- a/tool/src/org/antlr/v4/semantics/SymbolChecks.java +++ b/tool/src/org/antlr/v4/semantics/SymbolChecks.java @@ -147,6 +147,10 @@ public class SymbolChecks { Map labelNameSpace = new HashMap(); for (int i=1; i<=r.numberOfAlts; i++) { + if (r.hasAltSpecificContexts()) { + labelNameSpace.clear(); + } + Alternative a = r.alt[i]; for (List pairs : a.labelDefs.values() ) { for (LabelElementPair p : pairs) { diff --git a/tool/test/org/antlr/v4/test/TestParserExec.java b/tool/test/org/antlr/v4/test/TestParserExec.java index a4d41fe3b..acba18fc4 100644 --- a/tool/test/org/antlr/v4/test/TestParserExec.java +++ b/tool/test/org/antlr/v4/test/TestParserExec.java @@ -307,4 +307,25 @@ public class TestParserExec extends BaseTest { assertNull(this.stderrDuringParse); } + /** + * This is a regression test for antlr/antlr4#195 "label 'label' type + * mismatch with previous definition: TOKEN_LABEL!=RULE_LABEL" + * https://github.com/antlr/antlr4/issues/195 + */ + @Test public void testLabelAliasingAcrossLabeledAlternatives() throws Exception { + String grammar = + "grammar T;\n" + + "start : a* EOF;\n" + + "a\n" + + " : label=subrule {System.out.println($label.text);} #One\n" + + " | label='y' {System.out.println($label.text);} #Two\n" + + " ;\n" + + "subrule : 'x';\n" + + "WS : (' '|'\\n') -> skip ;\n"; + + String found = execParser("T.g4", grammar, "TParser", "TLexer", "start", + "xy", false); + assertEquals("x\ny\n", found); + } + }