001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.test;
010:
011: import junit.framework.Test;
012: import junit.framework.TestCase;
013: import junit.framework.TestSuite;
014:
015: import net.sourceforge.chaperon.build.EmptyList;
016: import net.sourceforge.chaperon.build.FirstSetCollection;
017: import net.sourceforge.chaperon.model.grammar.Grammar;
018: import net.sourceforge.chaperon.model.grammar.Production;
019: import net.sourceforge.chaperon.model.symbol.Nonterminal;
020: import net.sourceforge.chaperon.model.symbol.SymbolList;
021: import net.sourceforge.chaperon.model.symbol.SymbolSet;
022: import net.sourceforge.chaperon.model.symbol.Terminal;
023:
024: public class FirstSetTestCase extends TestCase {
025: private Terminal plus;
026: private Terminal mult;
027: private Terminal bopen;
028: private Terminal bclose;
029: private Terminal id;
030: private Nonterminal E;
031: private Nonterminal Eprime;
032: private Nonterminal T;
033: private Nonterminal Tprime;
034: private Nonterminal F;
035: private EmptyList emptylist;
036: private Grammar grammar;
037:
038: public FirstSetTestCase(String name) {
039: super (name);
040: }
041:
042: public void setUp() {
043: emptylist = new EmptyList();
044:
045: plus = new Terminal("plus");
046: mult = new Terminal("mult");
047: bopen = new Terminal("bopen");
048: bclose = new Terminal("bclose");
049: id = new Terminal("id");
050:
051: E = new Nonterminal("E");
052: Eprime = new Nonterminal("E'");
053: T = new Nonterminal("T");
054: Tprime = new Nonterminal("T'");
055: F = new Nonterminal("F");
056:
057: grammar = new Grammar();
058:
059: // E -> T E'
060: Production production = new Production(E);
061: production.getDefinition().addSymbol(T);
062: production.getDefinition().addSymbol(Eprime);
063: grammar.addProduction(production);
064:
065: // E' -> + T E'
066: production = new Production(Eprime);
067: production.getDefinition().addSymbol(plus);
068: production.getDefinition().addSymbol(T);
069: production.getDefinition().addSymbol(Eprime);
070: grammar.addProduction(production);
071:
072: // E' ->
073: production = new Production(Eprime);
074: grammar.addProduction(production);
075:
076: // T -> F T'
077: production = new Production(T);
078: production.getDefinition().addSymbol(F);
079: production.getDefinition().addSymbol(Tprime);
080: grammar.addProduction(production);
081:
082: // T' -> * F T'
083: production = new Production(Tprime);
084: production.getDefinition().addSymbol(mult);
085: production.getDefinition().addSymbol(F);
086: production.getDefinition().addSymbol(Tprime);
087: grammar.addProduction(production);
088:
089: // T' ->
090: production = new Production(Tprime);
091: grammar.addProduction(production);
092:
093: // F -> bopen E bclose
094: production = new Production(F);
095: production.getDefinition().addSymbol(bopen);
096: production.getDefinition().addSymbol(E);
097: production.getDefinition().addSymbol(bclose);
098: grammar.addProduction(production);
099:
100: // F -> id
101: production = new Production(F);
102: production.getDefinition().addSymbol(id);
103: grammar.addProduction(production);
104: }
105:
106: public void testFirstOfASymbol() {
107: FirstSetCollection sets = new FirstSetCollection(grammar); /*, new ConsoleLogger());*/
108:
109: SymbolSet result = new SymbolSet();
110: result.addSymbol(id);
111: result.addSymbol(bopen);
112: assertEquals("Test if sets are equal", result, sets
113: .getFirstSet(E));
114: assertEquals("Test if sets are equal", result, sets
115: .getFirstSet(T));
116: assertEquals("Test if sets are equal", result, sets
117: .getFirstSet(F));
118:
119: result = new SymbolSet();
120: result.addSymbol(plus);
121: result.addSymbol(emptylist);
122: assertEquals("Test if sets are equal", result, sets
123: .getFirstSet(Eprime));
124:
125: result = new SymbolSet();
126: result.addSymbol(mult);
127: result.addSymbol(emptylist);
128: assertEquals("Test if sets are equal", result, sets
129: .getFirstSet(Tprime));
130: }
131:
132: public void testFirstOfASymbolList() {
133: FirstSetCollection sets = new FirstSetCollection(grammar); /*, new ConsoleLogger());*/
134:
135: SymbolList list = new SymbolList();
136: list.addSymbol(Eprime);
137:
138: SymbolSet result = new SymbolSet();
139: result.addSymbol(plus);
140: result.addSymbol(emptylist);
141: assertEquals("Test if sets are equal", result, sets
142: .getFirstSet(list));
143:
144: list = new SymbolList();
145: list.addSymbol(Eprime);
146: list.addSymbol(id);
147: list.addSymbol(Tprime);
148:
149: result = new SymbolSet();
150: result.addSymbol(plus);
151: result.addSymbol(id);
152: assertEquals("Test if sets are equal", result, sets
153: .getFirstSet(list));
154:
155: list = new SymbolList();
156: list.addSymbol(Eprime);
157: list.addSymbol(Tprime);
158:
159: result = new SymbolSet();
160: result.addSymbol(plus);
161: result.addSymbol(mult);
162: result.addSymbol(emptylist);
163: assertEquals("Test if sets are equal", result, sets
164: .getFirstSet(list));
165:
166: list = new SymbolList();
167:
168: result = new SymbolSet();
169: result.addSymbol(emptylist);
170: assertEquals("Test if sets are equal", result, sets
171: .getFirstSet(list));
172: }
173:
174: public static Test suite() {
175: return new TestSuite(FirstSetTestCase.class);
176: }
177: }
|