📄 testdfaconversion.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.misc.BitSet;import org.antlr.tool.*;import org.antlr.Tool;import org.antlr.codegen.CodeGenerator;import java.util.*;public class TestDFAConversion extends BaseTest { public void testA() throws Exception { Grammar g = new Grammar( "parser grammar t;\n"+ "a : A C | B;"); String expecting = ".s0-A->:s1=>1\n" + ".s0-B->:s2=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, 0); } public void testAB_or_AC() throws Exception { Grammar g = new Grammar( "parser grammar t;\n"+ "a : A B | A C;"); String expecting = ".s0-A->.s1\n" + ".s1-B->:s3=>1\n" + ".s1-C->:s2=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, 0); } public void testAB_or_AC_k2() throws Exception { Grammar g = new Grammar( "parser grammar t;\n" + "options {k=2;}\n"+ "a : A B | A C;"); String expecting = ".s0-A->.s1\n" + ".s1-B->:s3=>1\n" + ".s1-C->:s2=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, 0); } public void testAB_or_AC_k1() throws Exception { Grammar g = new Grammar( "parser grammar t;\n" + "options {k=1;}\n"+ "a : A B | A C;"); String expecting = ".s0-A->:s1=>1\n"; int[] unreachableAlts = new int[] {2}; int[] nonDetAlts = new int[] {1,2}; String ambigInput = "A" ; int[] danglingAlts = new int[] {2}; int numWarnings = 2; // non-LL(1) abort and ambig upon A checkDecision(g, 1, expecting, unreachableAlts, nonDetAlts, ambigInput, danglingAlts, numWarnings); } public void testselfRecurseNonDet() throws Exception { Grammar g = new Grammar( "parser grammar t;\n"+ "s : a ;\n" + "a : A a X | A a Y;"); // nondeterministic from left edge; no stop state String expecting = ".s0-A->.s1\n" + ".s1-A->:s2=>1\n"; // gets this after failing to do LL(*) int[] unreachableAlts = new int[] {1,2}; int[] nonDetAlts = new int[] {1,2}; String ambigInput = null; int[] danglingAlts = new int[] {1,2}; int numWarnings = 2; // non-LL(*) abort and ambig upon A A checkDecision(g, 1, expecting, unreachableAlts, nonDetAlts, ambigInput, danglingAlts, numWarnings); } public void testCannotSeePastRecursion() throws Exception { Grammar g = new Grammar( "parser grammar t;\n"+ "x : y X\n" + " | y Y\n" + " ;\n" + "y : L y R\n" + " | B\n" + " ;"); String expecting = ".s0-B->.s4\n" + ".s0-L->.s1\n" + ".s1-B->.s3\n" + ".s1-L->:s2=>1\n"; int[] unreachableAlts = new int[] {1,2}; int[] nonDetAlts = new int[] {1,2}; String ambigInput = null; int[] danglingAlts = null; int numWarnings = 2; checkDecision(g, 1, expecting, unreachableAlts, nonDetAlts, ambigInput, danglingAlts, numWarnings); } public void testSynPredResolvesRecursion() throws Exception { Grammar g = new Grammar( "parser grammar t;\n"+ "x : (y X)=> y X\n" + " | y Y\n" + " ;\n" + "y : L y R\n" + " | B\n" + " ;"); String expecting = ".s0-B->.s7\n" + ".s0-L->.s1\n" + ".s1-B->.s5\n" + ".s1-L->.s2\n" + ".s2-{synpred1}?->:s3=>1\n" + ".s2-{true}?->:s4=>2\n" + ".s5-R->.s6\n" + ".s6-X&&{synpred1}?->:s3=>1\n" + ".s6-Y->:s4=>2\n" + ".s7-X&&{synpred1}?->:s3=>1\n" + ".s7-Y->:s4=>2\n"; int[] unreachableAlts = null; int[] nonDetAlts = null; String ambigInput = null; int[] danglingAlts = null; int numWarnings = 0; checkDecision(g, 1, expecting, unreachableAlts, nonDetAlts, ambigInput, danglingAlts, numWarnings); } public void testSynPredResolvesRecursionInLexer() throws Exception { Grammar g = new Grammar( "lexer grammar t;\n"+ "A : (B ';')=> B ';'\n" + " | B '.'\n" + " ;\n" + "fragment\n" + "B : '(' B ')'\n" + " | 'x'\n" + " ;\n"); String expecting = ".s0-'('->.s1\n" + ".s0-'x'->.s7\n" + ".s1-'('->.s2\n" + ".s1-'x'->.s5\n" + ".s2-{synpred1}?->:s3=>1\n" + ".s2-{true}?->:s4=>2\n" + ".s5-')'->.s6\n" + ".s6-'.'->:s4=>2\n" + ".s6-';'&&{synpred1}?->:s3=>1\n" + ".s7-'.'->:s4=>2\n" + ".s7-';'&&{synpred1}?->:s3=>1\n"; int[] unreachableAlts = null; int[] nonDetAlts = null; String ambigInput = null; int[] danglingAlts = null; int numWarnings = 0; checkDecision(g, 1, expecting, unreachableAlts, nonDetAlts, ambigInput, danglingAlts, numWarnings); } public void testAutoBacktrackResolvesRecursionInLexer() throws Exception { Grammar g = new Grammar( "lexer grammar t;\n"+ "options {backtrack=true;}\n"+ "A : B ';'\n" + " | B '.'\n" + " ;\n" + "fragment\n" + "B : '(' B ')'\n" + " | 'x'\n" + " ;\n"); String expecting = ".s0-'('->.s1\n" + ".s0-'x'->.s7\n" + ".s1-'('->.s2\n" + ".s1-'x'->.s5\n" + ".s2-{synpred1}?->:s3=>1\n" + ".s2-{true}?->:s4=>2\n" + ".s5-')'->.s6\n" + ".s6-'.'->:s4=>2\n" + ".s6-';'->:s3=>1\n" + ".s7-'.'->:s4=>2\n" + ".s7-';'->:s3=>1\n"; int[] unreachableAlts = null; int[] nonDetAlts = null; String ambigInput = null; int[] danglingAlts = null; int numWarnings = 0; checkDecision(g, 1, expecting, unreachableAlts, nonDetAlts, ambigInput, danglingAlts, numWarnings); } public void testAutoBacktrackResolvesRecursion() throws Exception { Grammar g = new Grammar( "parser grammar t;\n" + "options {backtrack=true;}\n"+ "x : y X\n" + " | y Y\n" + " ;\n" + "y : L y R\n" + " | B\n" + " ;"); String expecting = ".s0-B->.s7\n" + ".s0-L->.s1\n" + ".s1-B->.s5\n" + ".s1-L->.s2\n" + ".s2-{synpred1}?->:s3=>1\n" + ".s2-{true}?->:s4=>2\n" + ".s5-R->.s6\n" + ".s6-X->:s3=>1\n" + ".s6-Y->:s4=>2\n" + ".s7-X->:s3=>1\n" + ".s7-Y->:s4=>2\n"; int[] unreachableAlts = null; int[] nonDetAlts = null; String ambigInput = null; int[] danglingAlts = null; int numWarnings = 0; checkDecision(g, 1, expecting, unreachableAlts, nonDetAlts, ambigInput, danglingAlts, numWarnings); } public void testselfRecurseNonDet2() throws Exception { Grammar g = new Grammar( "parser grammar t;\n"+ "s : a ;\n" + "a : P a P | P;"); // nondeterministic from left edge String expecting = ".s0-P->.s1\n" + ".s1-EOF->:s2=>2\n"+ ".s1-P->:s3=>1\n"; int[] unreachableAlts = null; int[] nonDetAlts = new int[] {1,2}; String ambigInput = "P P"; int[] danglingAlts = null; int numWarnings = 1; checkDecision(g, 1, expecting, unreachableAlts, nonDetAlts, ambigInput, danglingAlts, numWarnings); } public void testIndirectRecursionLoop() throws Exception { Grammar g = new Grammar( "parser grammar t;\n"+ "s : a ;\n" + "a : b X ;\n"+ "b : a B ;\n"); DecisionProbe.verbose=true; // make sure we get all error info ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Set leftRecursive = g.getLeftRecursiveRules(); Set expectedRules = new HashSet() {{add("a"); add("b");}}; assertEquals(expectedRules, leftRecursive); g.createLookaheadDFAs(); Message msg = (Message)equeue.warnings.get(0); assertTrue("expecting left recursion cycles; found "+msg.getClass().getName(), msg instanceof LeftRecursionCyclesMessage); LeftRecursionCyclesMessage cyclesMsg = (LeftRecursionCyclesMessage)msg; // cycle of [a, b] Collection result = cyclesMsg.cycles; List expecting = new ArrayList(); expecting.add(new HashSet() {{add("a"); add("b");}}); assertEquals(expecting, result); } public void testIndirectRecursionLoop2() throws Exception { Grammar g = new Grammar( "parser grammar t;\n"+ "s : a ;\n" + "a : i b X ;\n"+ // should see through i "b : a B ;\n" + "i : ;\n"); DecisionProbe.verbose=true; // make sure we get all error info ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Set leftRecursive = g.getLeftRecursiveRules(); Set expectedRules = new HashSet() {{add("a"); add("b");}}; assertEquals(expectedRules, leftRecursive); g.createLookaheadDFAs(); Message msg = (Message)equeue.warnings.get(0); assertTrue("expecting left recursion cycles; found "+msg.getClass().getName(), msg instanceof LeftRecursionCyclesMessage); LeftRecursionCyclesMessage cyclesMsg = (LeftRecursionCyclesMessage)msg; // cycle of [a, b] Collection result = cyclesMsg.cycles; List expecting = new ArrayList(); expecting.add(new HashSet() {{add("a"); add("b");}}); assertEquals(expecting, result); } public void testIndirectRecursionLoop3() throws Exception { Grammar g = new Grammar( "parser grammar t;\n"+ "s : a ;\n" + "a : i b X ;\n"+ // should see through i "b : a B ;\n" + "i : ;\n" + "d : e ;\n" + "e : d ;\n"); DecisionProbe.verbose=true; // make sure we get all error info ErrorQueue equeue = new ErrorQueue(); ErrorManager.setErrorListener(equeue); Set leftRecursive = g.getLeftRecursiveRules(); Set expectedRules = new HashSet() {{add("a"); add("b"); add("e"); add("d");}}; assertEquals(expectedRules, leftRecursive); Message msg = (Message)equeue.warnings.get(0); assertTrue("expecting left recursion cycles; found "+msg.getClass().getName(), msg instanceof LeftRecursionCyclesMessage); LeftRecursionCyclesMessage cyclesMsg = (LeftRecursionCyclesMessage)msg; // cycle of [a, b] Collection result = cyclesMsg.cycles; List expecting = new ArrayList(); expecting.add(new HashSet() {{add("a"); add("b");}}); expecting.add(new HashSet() {{add("d"); add("e");}}); assertEquals(expecting, result); } public void testifThenElse() throws Exception { Grammar g = new Grammar( "parser grammar t;\n"+ "s : IF s (E s)? | B;\n" + "slist: s SEMI ;"); String expecting = ".s0-E->:s1=>1\n" + ".s0-SEMI->:s2=>2\n"; int[] unreachableAlts = null; int[] nonDetAlts = new int[] {1,2}; String ambigInput = "E"; int[] danglingAlts = null; int numWarnings = 1; checkDecision(g, 1, expecting, unreachableAlts, nonDetAlts, ambigInput, danglingAlts, numWarnings); expecting = ".s0-B->:s2=>2\n" + ".s0-IF->:s1=>1\n"; checkDecision(g, 2, expecting, null, null, null, null, 0); } public void testifThenElseChecksStackSuffixConflict() throws Exception { // if you don't check stack soon enough, this finds E B not just E // as ambig input Grammar g = new Grammar( "parser grammar t;\n"+ "slist: s SEMI ;\n"+ "s : IF s el | B;\n" + "el: (E s)? ;\n"); String expecting = ".s0-E->:s1=>1\n" + ".s0-SEMI->:s2=>2\n"; int[] unreachableAlts = null; int[] nonDetAlts = new int[] {1,2}; String ambigInput = "E"; int[] danglingAlts = null; int numWarnings = 1; checkDecision(g, 2, expecting, unreachableAlts, nonDetAlts, ambigInput, danglingAlts, numWarnings); expecting = ".s0-B->:s2=>2\n" + ".s0-IF->:s1=>1\n"; checkDecision(g, 1, expecting, null, null, null, null, 0); } public void testInvokeRule() throws Exception { Grammar g = new Grammar( "parser grammar t;\n"+ "a : b A\n" + " | b B\n" + " | C\n" + " ;\n" + "b : X\n" + " ;\n"); String expecting = ".s0-C->:s4=>3\n" + ".s0-X->.s1\n" + ".s1-A->:s3=>1\n" + ".s1-B->:s2=>2\n"; checkDecision(g, 1, expecting, null, null, null, null, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -