Home | History | Annotate | Download | only in test
      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 public class TestDFAMatching extends BaseTest {
     37 
     38     /** Public default constructor used by TestRig */
     39     public TestDFAMatching() {
     40     }
     41 
     42     @Test public void testSimpleAltCharTest() throws Exception {
     43         Grammar g = new Grammar(
     44                 "lexer grammar t;\n"+
     45                 "A : {;}'a' | 'b' | 'c';");
     46 		g.buildNFA();
     47 		g.createLookaheadDFAs(false);
     48         DFA dfa = g.getLookaheadDFA(1);
     49         checkPrediction(dfa,"a",1);
     50         checkPrediction(dfa,"b",2);
     51         checkPrediction(dfa,"c",3);
     52         checkPrediction(dfa,"d", NFA.INVALID_ALT_NUMBER);
     53     }
     54 
     55     @Test public void testSets() throws Exception {
     56         Grammar g = new Grammar(
     57                 "lexer grammar t;\n"+
     58                 "A : {;}'a'..'z' | ';' | '0'..'9' ;");
     59 		g.buildNFA();
     60         g.createLookaheadDFAs(false);
     61         DFA dfa = g.getLookaheadDFA(1);
     62         checkPrediction(dfa,"a",1);
     63         checkPrediction(dfa,"q",1);
     64         checkPrediction(dfa,"z",1);
     65         checkPrediction(dfa,";",2);
     66         checkPrediction(dfa,"9",3);
     67     }
     68 
     69     @Test public void testFiniteCommonLeftPrefixes() throws Exception {
     70         Grammar g = new Grammar(
     71                 "lexer grammar t;\n"+
     72                 "A : 'a' 'b' | 'a' 'c' | 'd' 'e' ;");
     73 		g.buildNFA();
     74         g.createLookaheadDFAs(false);
     75         DFA dfa = g.getLookaheadDFA(1);
     76         checkPrediction(dfa,"ab",1);
     77         checkPrediction(dfa,"ac",2);
     78         checkPrediction(dfa,"de",3);
     79         checkPrediction(dfa,"q", NFA.INVALID_ALT_NUMBER);
     80     }
     81 
     82     @Test public void testSimpleLoops() throws Exception {
     83         Grammar g = new Grammar(
     84                 "lexer grammar t;\n"+
     85                 "A : (DIGIT)+ '.' DIGIT | (DIGIT)+ ;\n" +
     86                 "fragment DIGIT : '0'..'9' ;\n");
     87 		g.buildNFA();
     88         g.createLookaheadDFAs(false);
     89         DFA dfa = g.getLookaheadDFA(3);
     90         checkPrediction(dfa,"32",2);
     91         checkPrediction(dfa,"999.2",1);
     92         checkPrediction(dfa,".2", NFA.INVALID_ALT_NUMBER);
     93     }
     94 
     95     protected void checkPrediction(DFA dfa, String input, int expected)
     96         throws Exception
     97     {
     98         ANTLRStringStream stream = new ANTLRStringStream(input);
     99         assertEquals(dfa.predict(stream), expected);
    100     }
    101 
    102 }
    103