• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * [The "BSD license"]
3  *  Copyright (c) 2010 Terence Parr
4  *  All rights reserved.
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions
8  *  are met:
9  *  1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *  3. The name of the author may not be used to endorse or promote products
15  *      derived from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 package org.antlr.test;
29 
30 import org.antlr.analysis.DFA;
31 import org.antlr.analysis.NFA;
32 import org.antlr.runtime.ANTLRStringStream;
33 import org.antlr.tool.Grammar;
34 import org.junit.Test;
35 
36 import static org.junit.Assert.*;
37 
38 public class TestDFAMatching extends BaseTest {
39 
40     /** Public default constructor used by TestRig */
TestDFAMatching()41     public TestDFAMatching() {
42     }
43 
testSimpleAltCharTest()44     @Test public void testSimpleAltCharTest() throws Exception {
45         Grammar g = new Grammar(
46                 "lexer grammar t;\n"+
47                 "A : {;}'a' | 'b' | 'c';");
48 		g.buildNFA();
49 		g.createLookaheadDFAs(false);
50         DFA dfa = g.getLookaheadDFA(1);
51         checkPrediction(dfa,"a",1);
52         checkPrediction(dfa,"b",2);
53         checkPrediction(dfa,"c",3);
54         checkPrediction(dfa,"d", NFA.INVALID_ALT_NUMBER);
55     }
56 
testSets()57     @Test public void testSets() throws Exception {
58         Grammar g = new Grammar(
59                 "lexer grammar t;\n"+
60                 "A : {;}'a'..'z' | ';' | '0'..'9' ;");
61 		g.buildNFA();
62         g.createLookaheadDFAs(false);
63         DFA dfa = g.getLookaheadDFA(1);
64         checkPrediction(dfa,"a",1);
65         checkPrediction(dfa,"q",1);
66         checkPrediction(dfa,"z",1);
67         checkPrediction(dfa,";",2);
68         checkPrediction(dfa,"9",3);
69     }
70 
testFiniteCommonLeftPrefixes()71     @Test public void testFiniteCommonLeftPrefixes() throws Exception {
72         Grammar g = new Grammar(
73                 "lexer grammar t;\n"+
74                 "A : 'a' 'b' | 'a' 'c' | 'd' 'e' ;");
75 		g.buildNFA();
76         g.createLookaheadDFAs(false);
77         DFA dfa = g.getLookaheadDFA(1);
78         checkPrediction(dfa,"ab",1);
79         checkPrediction(dfa,"ac",2);
80         checkPrediction(dfa,"de",3);
81         checkPrediction(dfa,"q", NFA.INVALID_ALT_NUMBER);
82     }
83 
testSimpleLoops()84     @Test public void testSimpleLoops() throws Exception {
85         Grammar g = new Grammar(
86                 "lexer grammar t;\n"+
87                 "A : (DIGIT)+ '.' DIGIT | (DIGIT)+ ;\n" +
88                 "fragment DIGIT : '0'..'9' ;\n");
89 		g.buildNFA();
90         g.createLookaheadDFAs(false);
91         DFA dfa = g.getLookaheadDFA(3);
92         checkPrediction(dfa,"32",2);
93         checkPrediction(dfa,"999.2",1);
94         checkPrediction(dfa,".2", NFA.INVALID_ALT_NUMBER);
95     }
96 
checkPrediction(DFA dfa, String input, int expected)97     protected void checkPrediction(DFA dfa, String input, int expected)
98         throws Exception
99     {
100         ANTLRStringStream stream = new ANTLRStringStream(input);
101         assertEquals(dfa.predict(stream), expected);
102     }
103 
104 }
105