📄 testsemanticpredicates.java
字号:
/*[The "BSD licence"]Copyright (c) 2005-2006 Terence ParrAll rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditionsare met:1. Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.2. Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.3. The name of the author may not be used to endorse or promote productsderived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS ORIMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIESOF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUTNOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANYTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OFTHIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.antlr.test;import org.antlr.analysis.DFA;import org.antlr.analysis.DecisionProbe;import org.antlr.codegen.CodeGenerator;import org.antlr.misc.BitSet;import org.antlr.tool.*;import java.util.List;public class TestSemanticPredicates extends BaseTest { /** Public default constructor used by TestRig */ public TestSemanticPredicates() { } public void testPredsButSyntaxResolves() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : {p1}? A | {p2}? B ;"); String expecting = ".s0-A->:s1=>1\n" + ".s0-B->:s2=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, null, 0); } public void testLL_1_Pred() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : {p1}? A | {p2}? A ;"); String expecting = ".s0-A->.s1\n" + ".s1-{p1}?->:s2=>1\n" + ".s1-{p2}?->:s3=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, null, 0); } public void testLL_2_Pred() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : {p1}? A B | {p2}? A B ;"); String expecting = ".s0-A->.s1\n" + ".s1-B->.s2\n" + ".s2-{p1}?->:s3=>1\n" + ".s2-{p2}?->:s4=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, null, 0); } public void testPredicatedLoop() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : ( {p1}? A | {p2}? A )+;"); String expecting = // loop back ".s0-A->.s2\n" + ".s0-EOF->:s1=>3\n" + ".s2-{p1}?->:s3=>1\n" + ".s2-{p2}?->:s4=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, null, 0); } public void testPredicatedToStayInLoop() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : ( {p1}? A )+ (A)+;"); String expecting = ".s0-A->.s1\n" + ".s1-{!(p1)}?->:s2=>1\n" + ".s1-{p1}?->:s3=>2\n"; // loop back } public void testAndPredicates() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : {p1}? {p1a}? A | {p2}? A ;"); String expecting = ".s0-A->.s1\n" + ".s1-{(p1&&p1a)}?->:s2=>1\n" + ".s1-{p2}?->:s3=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, null, 0); } public void testOrPredicates() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : b | {p2}? A ;\n" + "b : {p1}? A | {p1a}? A ;"); String expecting = ".s0-A->.s1\n" + ".s1-{(p1||p1a)}?->:s2=>1\n" + ".s1-{p2}?->:s3=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, null, 0); } public void testIgnoresHoistingDepthGreaterThanZero() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : A {p1}? | A {p2}?;"); String expecting = ".s0-A->:s1=>1\n"; checkDecision(g, 1, expecting, new int[] {2}, new int[] {1,2}, "A", null, null, 2); } public void testHoist2() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : b | c ;\n" + "b : {p1}? A ;\n" + "c : {p2}? A ;\n"); String expecting = ".s0-A->.s1\n" + ".s1-{p1}?->:s2=>1\n" + ".s1-{p2}?->:s3=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, null, 0); } public void testHoistCorrectContext() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : b | {p2}? ID ;\n" + "b : {p1}? ID | INT ;\n"); String expecting = // only tests after ID, not INT :) ".s0-ID->.s1\n" + ".s0-INT->:s2=>1\n" + ".s1-{p1}?->:s2=>1\n" + ".s1-{p2}?->:s3=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, null, 0); } public void testDefaultPredNakedAltIsLast() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : b | ID ;\n" + "b : {p1}? ID | INT ;\n"); String expecting = ".s0-ID->.s1\n" + ".s0-INT->:s2=>1\n" + ".s1-{p1}?->:s2=>1\n" + ".s1-{true}?->:s3=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, null, 0); } public void testDefaultPredNakedAltNotLast() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : ID | b ;\n" + "b : {p1}? ID | INT ;\n"); String expecting = ".s0-ID->.s1\n" + ".s0-INT->:s3=>2\n" + ".s1-{!(p1)}?->:s2=>1\n" + ".s1-{p1}?->:s3=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, null, 0); } public void testLeftRecursivePred() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "s : a ;\n" + "a : {p1}? a | ID ;\n"); String expecting = ".s0-ID->.s1\n" + ".s1-{p1}?->:s2=>1\n" + ".s1-{true}?->:s3=>2\n"; DecisionProbe.verbose=true; // make sure we get all error info ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); CodeGenerator generator = new CodeGenerator(newTool(), g, "Java"); g.setCodeGenerator(generator); if ( g.getNumberOfDecisions()==0 ) { g.createNFAs(); g.createLookaheadDFAs(); } DFA dfa = g.getLookaheadDFA(1); FASerializer serializer = new FASerializer(g); String result = serializer.serialize(dfa.startState); assertEquals(expecting, result); assertEquals("unexpected number of expected problems", 1, equeue.size()); Message msg = (Message)equeue.warnings.get(0); assertTrue("warning must be a recursion overflow msg", msg instanceof RecursionOverflowMessage); } public void testIgnorePredFromLL2AltLastAltIsDefaultTrue() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : {p1}? A B | A C | {p2}? A | {p3}? A | A ;\n"); // two situations of note: // 1. A B syntax is enough to predict that alt, so p1 is not used // to distinguish it from alts 2..5 // 2. Alts 3, 4, 5 are nondeterministic with upon A. p2, p3 and the // complement of p2||p3 is sufficient to resolve the conflict. Do // not include alt 1's p1 pred in the "complement of other alts" // because it is not considered nondeterministic with alts 3..5 String expecting = ".s0-A->.s1\n" + ".s1-B->:s2=>1\n" + ".s1-C->:s3=>2\n" + ".s1-{p2}?->:s4=>3\n" + ".s1-{p3}?->:s5=>4\n" + ".s1-{true}?->:s6=>5\n"; checkDecision(g, 1, expecting, null, null, null, null, null, 0); } public void testIgnorePredFromLL2AltPredUnionNeeded() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : {p1}? A B | A C | {p2}? A | A | {p3}? A ;\n"); // two situations of note: // 1. A B syntax is enough to predict that alt, so p1 is not used // to distinguish it from alts 2..5 // 2. Alts 3, 4, 5 are nondeterministic with upon A. p2, p3 and the // complement of p2||p3 is sufficient to resolve the conflict. Do // not include alt 1's p1 pred in the "complement of other alts" // because it is not considered nondeterministic with alts 3..5 String expecting = ".s0-A->.s1\n" + ".s1-B->:s2=>1\n" + ".s1-C->:s3=>2\n" + ".s1-{!((p3||p2))}?->:s5=>4\n" + ".s1-{p2}?->:s4=>3\n" + ".s1-{p3}?->:s6=>5\n"; checkDecision(g, 1, expecting, null, null, null, null, null, 0); } public void testPredGets2SymbolSyntacticContext() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : b | A B | C ;\n" + "b : {p1}? A B ;\n"); String expecting = ".s0-A->.s1\n" + ".s0-C->:s5=>3\n" + ".s1-B->.s2\n" + ".s2-{p1}?->:s3=>1\n" + ".s2-{true}?->:s4=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, null, 0); } public void testMatchesLongestThenTestPred() throws Exception { Grammar g = new Grammar( "parser grammar P;\n"+ "a : b | c ;\n" + "b : {p}? A ;\n" + "c : {q}? (A|B)+ ;"); String expecting = ".s0-A->.s1\n" + ".s0-B->:s3=>2\n" + ".s1-{p}?->:s2=>1\n" + ".s1-{q}?->:s3=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, null, 0); } public void testPredsUsedAfterRecursionOverflow() throws Exception { Grammar g = new Grammar( "grammar P;\n"+ "s : {p1}? e '.' | {p2}? e ':' ;\n" + "e : '(' e ')' | INT ;\n"); String expecting = ".s0-'('->.s1\n" + ".s0-INT->.s7\n" + ".s1-'('->.s2\n" + ".s1-INT->.s5\n" + ".s2-{p1}?->:s3=>1\n" + ".s2-{p2}?->:s4=>2\n" + ".s5-')'->.s6\n" + ".s6-'.'->:s3=>1\n" + ".s6-':'->:s4=>2\n" + ".s7-'.'->:s3=>1\n" + ".s7-':'->:s4=>2\n"; DecisionProbe.verbose=true; // make sure we get all error info ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); CodeGenerator generator = new CodeGenerator(newTool(), g, "Java"); g.setCodeGenerator(generator); if ( g.getNumberOfDecisions()==0 ) { g.createNFAs(); g.createLookaheadDFAs(); } assertEquals("unexpected number of expected problems", 0, equeue.size()); checkDecision(g, 1, expecting, null, null, null, null, null, 0); } public void testLexerMatchesLongestThenTestPred() throws Exception { Grammar g = new Grammar( "lexer grammar P;\n"+ "B : {p}? 'a' ;\n" + "C : {q}? ('a'|'b')+ ;"); String expecting = ".s0-'a'->.s1\n" + ".s0-'b'->:s4=>2\n" + ".s1-'a'..'b'->:s4=>2\n" + ".s1-<EOT>->.s2\n" + ".s2-{p}?->:s3=>1\n" + ".s2-{q}?->:s4=>2\n"; checkDecision(g, 2, expecting, null, null, null, null, null, 0); } public void testGatedPred() throws Exception { // gated preds are present on all arcs in predictor Grammar g = new Grammar( "lexer grammar P;\n"+ "B : {p}? => 'a' ;\n" + "C : {q}? => ('a'|'b')+ ;"); String expecting = ".s0-'a'&&{(p||q)}?->.s1\n" + ".s0-'b'&&{q}?->:s4=>2\n" + ".s1-'a'..'b'&&{q}?->:s4=>2\n" + ".s1-<EOT>&&{(p||q)}?->.s2\n" + ".s2-{p}?->:s3=>1\n" + ".s2-{q}?->:s4=>2\n";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -