/*
* Copyright (C) Chaperon. All rights reserved.
* -------------------------------------------------------------------------
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package net.sourceforge.chaperon.test;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import net.sourceforge.chaperon.build.EmptyList;
import net.sourceforge.chaperon.build.EndOfFile;
import net.sourceforge.chaperon.build.FirstSetCollection;
import net.sourceforge.chaperon.build.FollowSetCollection;
import net.sourceforge.chaperon.model.grammar.Grammar;
import net.sourceforge.chaperon.model.grammar.Production;
import net.sourceforge.chaperon.model.symbol.Nonterminal;
import net.sourceforge.chaperon.model.symbol.SymbolSet;
import net.sourceforge.chaperon.model.symbol.Terminal;
public class FollowSetTestCase extends TestCase
{
private Terminal plus;
private Terminal mult;
private Terminal bopen;
private Terminal bclose;
private Terminal id;
private Nonterminal E;
private Nonterminal Eprime;
private Nonterminal T;
private Nonterminal Tprime;
private Nonterminal F;
private EmptyList emptylist;
private EndOfFile eof;
private Grammar grammar;
public FollowSetTestCase(String name)
{
super(name);
}
public void setUp()
{
emptylist = new EmptyList();
eof = new EndOfFile();
plus = new Terminal("plus");
mult = new Terminal("mult");
bopen = new Terminal("bopen");
bclose = new Terminal("bclose");
id = new Terminal("id");
E = new Nonterminal("E");
Eprime = new Nonterminal("E'");
T = new Nonterminal("T");
Tprime = new Nonterminal("T'");
F = new Nonterminal("F");
grammar = new Grammar();
grammar.setStartSymbol(E);
// E -> T E'
Production production = new Production(E);
production.getDefinition().addSymbol(T);
production.getDefinition().addSymbol(Eprime);
grammar.addProduction(production);
// E' -> + T E'
production = new Production(Eprime);
production.getDefinition().addSymbol(plus);
production.getDefinition().addSymbol(T);
production.getDefinition().addSymbol(Eprime);
grammar.addProduction(production);
// E' ->
production = new Production(Eprime);
grammar.addProduction(production);
// T -> F T'
production = new Production(T);
production.getDefinition().addSymbol(F);
production.getDefinition().addSymbol(Tprime);
grammar.addProduction(production);
// T' -> * F T'
production = new Production(Tprime);
production.getDefinition().addSymbol(mult);
production.getDefinition().addSymbol(F);
production.getDefinition().addSymbol(Tprime);
grammar.addProduction(production);
// T' ->
production = new Production(Tprime);
grammar.addProduction(production);
// F -> bopen E bclose
production = new Production(F);
production.getDefinition().addSymbol(bopen);
production.getDefinition().addSymbol(E);
production.getDefinition().addSymbol(bclose);
grammar.addProduction(production);
// F -> id
production = new Production(F);
production.getDefinition().addSymbol(id);
grammar.addProduction(production);
}
public void testFollowSet()
{
FirstSetCollection firstsets = new FirstSetCollection(grammar); /*, new ConsoleLogger());*/
FollowSetCollection followsets = new FollowSetCollection(grammar, firstsets);
SymbolSet result = new SymbolSet();
result.addSymbol(bclose);
result.addSymbol(eof);
assertEquals("Test if sets are equal", result, followsets.getFollowSet(E));
assertEquals("Test if sets are equal", result, followsets.getFollowSet(Eprime));
result = new SymbolSet();
result.addSymbol(plus);
result.addSymbol(bclose);
result.addSymbol(eof);
assertEquals("Test if sets are equal", result, followsets.getFollowSet(T));
assertEquals("Test if sets are equal", result, followsets.getFollowSet(Tprime));
result = new SymbolSet();
result.addSymbol(mult);
result.addSymbol(plus);
result.addSymbol(bclose);
result.addSymbol(eof);
assertEquals("Test if sets are equal", result, followsets.getFollowSet(F));
}
public static Test suite()
{
return new TestSuite(FollowSetTestCase.class);
}
}