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.EndOfFile;
017: import net.sourceforge.chaperon.build.FirstSetCollection;
018: import net.sourceforge.chaperon.build.FollowSetCollection;
019: import net.sourceforge.chaperon.model.grammar.Grammar;
020: import net.sourceforge.chaperon.model.grammar.Production;
021: import net.sourceforge.chaperon.model.symbol.Nonterminal;
022: import net.sourceforge.chaperon.model.symbol.SymbolSet;
023: import net.sourceforge.chaperon.model.symbol.Terminal;
024:
025: public class FollowSetTestCase extends TestCase {
026: private Terminal plus;
027: private Terminal mult;
028: private Terminal bopen;
029: private Terminal bclose;
030: private Terminal id;
031: private Nonterminal E;
032: private Nonterminal Eprime;
033: private Nonterminal T;
034: private Nonterminal Tprime;
035: private Nonterminal F;
036: private EmptyList emptylist;
037: private EndOfFile eof;
038: private Grammar grammar;
039:
040: public FollowSetTestCase(String name) {
041: super (name);
042: }
043:
044: public void setUp() {
045: emptylist = new EmptyList();
046: eof = new EndOfFile();
047:
048: plus = new Terminal("plus");
049: mult = new Terminal("mult");
050: bopen = new Terminal("bopen");
051: bclose = new Terminal("bclose");
052: id = new Terminal("id");
053:
054: E = new Nonterminal("E");
055: Eprime = new Nonterminal("E'");
056: T = new Nonterminal("T");
057: Tprime = new Nonterminal("T'");
058: F = new Nonterminal("F");
059:
060: grammar = new Grammar();
061:
062: grammar.setStartSymbol(E);
063:
064: // E -> T E'
065: Production production = new Production(E);
066: production.getDefinition().addSymbol(T);
067: production.getDefinition().addSymbol(Eprime);
068: grammar.addProduction(production);
069:
070: // E' -> + T E'
071: production = new Production(Eprime);
072: production.getDefinition().addSymbol(plus);
073: production.getDefinition().addSymbol(T);
074: production.getDefinition().addSymbol(Eprime);
075: grammar.addProduction(production);
076:
077: // E' ->
078: production = new Production(Eprime);
079: grammar.addProduction(production);
080:
081: // T -> F T'
082: production = new Production(T);
083: production.getDefinition().addSymbol(F);
084: production.getDefinition().addSymbol(Tprime);
085: grammar.addProduction(production);
086:
087: // T' -> * F T'
088: production = new Production(Tprime);
089: production.getDefinition().addSymbol(mult);
090: production.getDefinition().addSymbol(F);
091: production.getDefinition().addSymbol(Tprime);
092: grammar.addProduction(production);
093:
094: // T' ->
095: production = new Production(Tprime);
096: grammar.addProduction(production);
097:
098: // F -> bopen E bclose
099: production = new Production(F);
100: production.getDefinition().addSymbol(bopen);
101: production.getDefinition().addSymbol(E);
102: production.getDefinition().addSymbol(bclose);
103: grammar.addProduction(production);
104:
105: // F -> id
106: production = new Production(F);
107: production.getDefinition().addSymbol(id);
108: grammar.addProduction(production);
109: }
110:
111: public void testFollowSet() {
112: FirstSetCollection firstsets = new FirstSetCollection(grammar); /*, new ConsoleLogger());*/
113: FollowSetCollection followsets = new FollowSetCollection(
114: grammar, firstsets);
115:
116: SymbolSet result = new SymbolSet();
117: result.addSymbol(bclose);
118: result.addSymbol(eof);
119: assertEquals("Test if sets are equal", result, followsets
120: .getFollowSet(E));
121: assertEquals("Test if sets are equal", result, followsets
122: .getFollowSet(Eprime));
123:
124: result = new SymbolSet();
125: result.addSymbol(plus);
126: result.addSymbol(bclose);
127: result.addSymbol(eof);
128: assertEquals("Test if sets are equal", result, followsets
129: .getFollowSet(T));
130: assertEquals("Test if sets are equal", result, followsets
131: .getFollowSet(Tprime));
132:
133: result = new SymbolSet();
134: result.addSymbol(mult);
135: result.addSymbol(plus);
136: result.addSymbol(bclose);
137: result.addSymbol(eof);
138: assertEquals("Test if sets are equal", result, followsets
139: .getFollowSet(F));
140: }
141:
142: public static Test suite() {
143: return new TestSuite(FollowSetTestCase.class);
144: }
145: }
|