KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > test > FollowSetTestCase


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.test;
10
11 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14
15 import net.sourceforge.chaperon.build.EmptyList;
16 import net.sourceforge.chaperon.build.EndOfFile;
17 import net.sourceforge.chaperon.build.FirstSetCollection;
18 import net.sourceforge.chaperon.build.FollowSetCollection;
19 import net.sourceforge.chaperon.model.grammar.Grammar;
20 import net.sourceforge.chaperon.model.grammar.Production;
21 import net.sourceforge.chaperon.model.symbol.Nonterminal;
22 import net.sourceforge.chaperon.model.symbol.SymbolSet;
23 import net.sourceforge.chaperon.model.symbol.Terminal;
24
25 public class FollowSetTestCase extends TestCase
26 {
27   private Terminal plus;
28   private Terminal mult;
29   private Terminal bopen;
30   private Terminal bclose;
31   private Terminal id;
32   private Nonterminal E;
33   private Nonterminal Eprime;
34   private Nonterminal T;
35   private Nonterminal Tprime;
36   private Nonterminal F;
37   private EmptyList emptylist;
38   private EndOfFile eof;
39   private Grammar grammar;
40
41   public FollowSetTestCase(String JavaDoc name)
42   {
43     super(name);
44   }
45
46   public void setUp()
47   {
48     emptylist = new EmptyList();
49     eof = new EndOfFile();
50
51     plus = new Terminal("plus");
52     mult = new Terminal("mult");
53     bopen = new Terminal("bopen");
54     bclose = new Terminal("bclose");
55     id = new Terminal("id");
56
57     E = new Nonterminal("E");
58     Eprime = new Nonterminal("E'");
59     T = new Nonterminal("T");
60     Tprime = new Nonterminal("T'");
61     F = new Nonterminal("F");
62
63     grammar = new Grammar();
64
65     grammar.setStartSymbol(E);
66
67     // E -> T E'
68
Production production = new Production(E);
69     production.getDefinition().addSymbol(T);
70     production.getDefinition().addSymbol(Eprime);
71     grammar.addProduction(production);
72
73     // E' -> + T E'
74
production = new Production(Eprime);
75     production.getDefinition().addSymbol(plus);
76     production.getDefinition().addSymbol(T);
77     production.getDefinition().addSymbol(Eprime);
78     grammar.addProduction(production);
79
80     // E' ->
81
production = new Production(Eprime);
82     grammar.addProduction(production);
83
84     // T -> F T'
85
production = new Production(T);
86     production.getDefinition().addSymbol(F);
87     production.getDefinition().addSymbol(Tprime);
88     grammar.addProduction(production);
89
90     // T' -> * F T'
91
production = new Production(Tprime);
92     production.getDefinition().addSymbol(mult);
93     production.getDefinition().addSymbol(F);
94     production.getDefinition().addSymbol(Tprime);
95     grammar.addProduction(production);
96
97     // T' ->
98
production = new Production(Tprime);
99     grammar.addProduction(production);
100
101     // F -> bopen E bclose
102
production = new Production(F);
103     production.getDefinition().addSymbol(bopen);
104     production.getDefinition().addSymbol(E);
105     production.getDefinition().addSymbol(bclose);
106     grammar.addProduction(production);
107
108     // F -> id
109
production = new Production(F);
110     production.getDefinition().addSymbol(id);
111     grammar.addProduction(production);
112   }
113
114   public void testFollowSet()
115   {
116     FirstSetCollection firstsets = new FirstSetCollection(grammar); /*, new ConsoleLogger());*/
117     FollowSetCollection followsets = new FollowSetCollection(grammar, firstsets);
118
119     SymbolSet result = new SymbolSet();
120     result.addSymbol(bclose);
121     result.addSymbol(eof);
122     assertEquals("Test if sets are equal", result, followsets.getFollowSet(E));
123     assertEquals("Test if sets are equal", result, followsets.getFollowSet(Eprime));
124
125     result = new SymbolSet();
126     result.addSymbol(plus);
127     result.addSymbol(bclose);
128     result.addSymbol(eof);
129     assertEquals("Test if sets are equal", result, followsets.getFollowSet(T));
130     assertEquals("Test if sets are equal", result, followsets.getFollowSet(Tprime));
131
132     result = new SymbolSet();
133     result.addSymbol(mult);
134     result.addSymbol(plus);
135     result.addSymbol(bclose);
136     result.addSymbol(eof);
137     assertEquals("Test if sets are equal", result, followsets.getFollowSet(F));
138   }
139
140   public static Test suite()
141   {
142     return new TestSuite(FollowSetTestCase.class);
143   }
144 }
145
Popular Tags