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.Automaton;
016: import net.sourceforge.chaperon.model.grammar.Grammar;
017: import net.sourceforge.chaperon.model.grammar.GrammarFactory;
018: import net.sourceforge.chaperon.model.grammar.Production;
019: import net.sourceforge.chaperon.model.symbol.Nonterminal;
020: import net.sourceforge.chaperon.model.symbol.Terminal;
021:
022: import org.xml.sax.InputSource;
023: import org.xml.sax.XMLReader;
024:
025: import javax.xml.parsers.SAXParserFactory;
026:
027: public class AutomatonTestCase extends TestCase {
028: private Terminal plus;
029: private Terminal mult;
030: private Terminal bopen;
031: private Terminal bclose;
032: private Terminal id;
033: private Nonterminal E;
034: private Nonterminal T;
035: private Nonterminal F;
036: private Grammar grammar;
037:
038: public AutomatonTestCase(String name) {
039: super (name);
040: }
041:
042: public void setUp() {
043: plus = new Terminal("+");
044: mult = new Terminal("*");
045: bopen = new Terminal("(");
046: bclose = new Terminal(")");
047: id = new Terminal("id");
048:
049: E = new Nonterminal("E");
050: T = new Nonterminal("T");
051: F = new Nonterminal("F");
052:
053: grammar = new Grammar();
054:
055: // E -> E + T
056: Production production = new Production(E);
057: production.getDefinition().addSymbol(E);
058: production.getDefinition().addSymbol(plus);
059: production.getDefinition().addSymbol(T);
060: grammar.addProduction(production);
061:
062: // E -> T
063: production = new Production(E);
064: production.getDefinition().addSymbol(T);
065: grammar.addProduction(production);
066:
067: // T -> T * F
068: production = new Production(T);
069: production.getDefinition().addSymbol(T);
070: production.getDefinition().addSymbol(mult);
071: production.getDefinition().addSymbol(F);
072: grammar.addProduction(production);
073:
074: // T -> F
075: production = new Production(T);
076: production.getDefinition().addSymbol(F);
077: grammar.addProduction(production);
078:
079: // F -> ( E )
080: production = new Production(F);
081: production.getDefinition().addSymbol(bopen);
082: production.getDefinition().addSymbol(E);
083: production.getDefinition().addSymbol(bclose);
084: grammar.addProduction(production);
085:
086: production = new Production(F);
087: production.getDefinition().addSymbol(id);
088: grammar.addProduction(production);
089:
090: grammar.setStartSymbol(E);
091: }
092:
093: public void testConstruction() {
094: System.out.println(grammar);
095:
096: //FirstSetCollection firstsets = new FirstSetCollection(grammar);
097: Automaton collection = new Automaton(grammar, /*firstsets,*/
098: null);
099:
100: System.out.println(collection);
101: }
102:
103: public void testBigGrammar() throws Exception {
104: SAXParserFactory factory = SAXParserFactory.newInstance();
105:
106: factory.setNamespaceAware(true);
107:
108: XMLReader parser = factory.newSAXParser().getXMLReader();
109:
110: GrammarFactory handler = new GrammarFactory();
111: parser.setContentHandler(handler);
112: parser.parse(new InputSource(getClass().getResourceAsStream(
113: "java.xgrm")));
114:
115: Grammar grammar = handler.getGrammar();
116:
117: Automaton collection = new Automaton(grammar, /*firstsets,*/
118: null);
119:
120: System.out.println(collection);
121: }
122:
123: public static Test suite() {
124: return new TestSuite(AutomatonTestCase.class);
125: }
126: }
|