KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > automaton > external > NameTest


1 /* This file is part of SableCC ( http://sablecc.org ).
2  *
3  * Copyright 2007 Raymond Audet <raymond.audet@gmail.com>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.sablecc.sablecc.automaton.external;
19
20 import static org.junit.Assert.assertEquals;
21
22 import org.junit.Test;
23 import org.sablecc.sablecc.alphabet.AdjacencyRealm;
24 import org.sablecc.sablecc.alphabet.Realms;
25 import org.sablecc.sablecc.automaton.Dfa;
26 import org.sablecc.sablecc.automaton.MinimalDfa;
27 import org.sablecc.sablecc.automaton.Nfa;
28
29 public class NameTest {
30
31     @Test
32     public void nameTest() {
33
34         AdjacencyRealm<Character JavaDoc> charRealm = Realms.getCharacter();
35
36         Nfa<Character JavaDoc> any = new Nfa<Character JavaDoc>(charRealm.createInterval('A',
37                 'Z'));
38         Nfa<Character JavaDoc> r = new Nfa<Character JavaDoc>(charRealm.createInterval('R'));
39         Nfa<Character JavaDoc> a = new Nfa<Character JavaDoc>(charRealm.createInterval('A'));
40         Nfa<Character JavaDoc> y = new Nfa<Character JavaDoc>(charRealm.createInterval('Y'));
41
42         // Minimal Dfa by subtraction
43
Dfa<Character JavaDoc> allButRay = any.zeroOrMore().subtract(
44                 any.zeroOrMore().concatenateWith(r).concatenateWith(a)
45                         .concatenateWith(y).concatenateWith(any.zeroOrMore()));
46
47         MinimalDfa<Character JavaDoc> minimalBySubstraction = new MinimalDfa<Character JavaDoc>(
48                 allButRay);
49
50         // Minimal Dfa by construction
51
Nfa<Character JavaDoc> allButR = new Nfa<Character JavaDoc>(charRealm.createInterval(
52                 'A', 'Q')).unionWith(new Nfa<Character JavaDoc>(charRealm
53                 .createInterval('S', 'Z')));
54
55         Nfa<Character JavaDoc> allButRY = new Nfa<Character JavaDoc>(charRealm.createInterval(
56                 'A', 'Q')).unionWith(
57                 new Nfa<Character JavaDoc>(charRealm.createInterval('S', 'X')))
58                 .unionWith(new Nfa<Character JavaDoc>(charRealm.createInterval('Z')));
59
60         Nfa<Character JavaDoc> allButRA = new Nfa<Character JavaDoc>(charRealm.createInterval(
61                 'B', 'Q')).unionWith(new Nfa<Character JavaDoc>(charRealm
62                 .createInterval('S', 'Z')));
63
64         // not_ry not_r*
65
Nfa<Character JavaDoc> firstPart = allButRY.concatenateWith(allButR
66                 .zeroOrMore());
67
68         // a firstPart? | not_ra not_r*
69
Nfa<Character JavaDoc> secondPart = a.concatenateWith(firstPart.zeroOrOne())
70                 .unionWith(allButRA.concatenateWith(allButR.zeroOrMore()));
71
72         // r secondPart?
73
Nfa<Character JavaDoc> thirdPart = r.concatenateWith(secondPart.zeroOrOne());
74
75         // not_r* (r (a ( not_ry not_r*)? | not_ra not_r* )? )*
76
Nfa<Character JavaDoc> allButRayNfa = allButR.zeroOrMore().concatenateWith(
77                 thirdPart.zeroOrMore());
78
79         allButRay = new Dfa<Character JavaDoc>(allButRayNfa);
80
81         MinimalDfa<Character JavaDoc> minimalByConstruction = new MinimalDfa<Character JavaDoc>(
82                 allButRay);
83
84         // Testing if the two minimal Dfa's are equals
85
assertEquals(minimalBySubstraction.toString(), minimalByConstruction
86                 .toString());
87
88     }
89 }
90
Popular Tags