KickJava   Java API By Example, From Geeks To Geeks.

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


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

18
19 package org.sablecc.sablecc.automaton.external;
20
21 import static org.junit.Assert.assertEquals;
22
23 import org.junit.Test;
24 import org.sablecc.sablecc.alphabet.AdjacencyRealm;
25 import org.sablecc.sablecc.alphabet.Realms;
26 import org.sablecc.sablecc.automaton.Dfa;
27 import org.sablecc.sablecc.automaton.MinimalDfa;
28 import org.sablecc.sablecc.automaton.Nfa;
29
30 // Test the org.sablecc.sablecc.automaton package "externally", using its public
31
// interface
32
public class CommentTest {
33
34     // This test creates minimal DFAs for two regular expressions:
35
//
36
// shortest_comment = Shortest('/*' any* '*/')
37
//
38
// construction_comment =
39
// '/*' not_star* ('*' (not_star_slash not_star*)?)* '*/'
40
//
41
// It then asserts that the minimal DFAs are identical (comparing their
42
// toString() output for lack of better way)
43
@Test
44     public void commentTest() {
45
46         AdjacencyRealm<Character JavaDoc> charRealm = Realms.getCharacter();
47
48         // Building Nfa instances for various intervals
49
Nfa<Character JavaDoc> any = new Nfa<Character JavaDoc>(charRealm.createInterval(
50                 (char) 0, (char) 255));
51         Nfa<Character JavaDoc> slash = new Nfa<Character JavaDoc>(charRealm.createInterval('/'));
52         Nfa<Character JavaDoc> star = new Nfa<Character JavaDoc>(charRealm.createInterval('*'));
53         Nfa<Character JavaDoc> notStar = new Nfa<Character JavaDoc>(any.subtract(star));
54         Nfa<Character JavaDoc> notStarSlash = new Nfa<Character JavaDoc>(notStar
55                 .subtract(slash));
56
57         // Building minimal dfa for shortest_comment
58
MinimalDfa<Character JavaDoc> shortestComment = new MinimalDfa<Character JavaDoc>(slash
59                 .concatenateWith(star).concatenateWith(any.zeroOrMore())
60                 .concatenateWith(star).concatenateWith(slash).shortest());
61
62         // Building minimal dfa for construction_comment
63

64         // part1 = not_star_slash not_star*
65
Nfa<Character JavaDoc> part1 = notStarSlash.concatenateWith(notStar
66                 .zeroOrMore());
67
68         // part2 = '*' (part1)?
69
Nfa<Character JavaDoc> part2 = star.concatenateWith(part1.zeroOrOne());
70
71         // construction_comment = '/*' not_star* part2* '*/'
72
Nfa<Character JavaDoc> constructionCommentNfa = slash.concatenateWith(star)
73                 .concatenateWith(notStar.zeroOrMore()).concatenateWith(
74                         part2.zeroOrMore()).concatenateWith(star)
75                 .concatenateWith(slash);
76         MinimalDfa<Character JavaDoc> constructionComment = new MinimalDfa<Character JavaDoc>(
77                 new Dfa<Character JavaDoc>(constructionCommentNfa));
78
79         // Testing for equality of toString() representation
80
assertEquals("The two minimal Dfa instances must be equal.",
81                 shortestComment.toString(), constructionComment.toString());
82     }
83 }
84
Popular Tags