1 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.Automaton; 16 import net.sourceforge.chaperon.model.grammar.Grammar; 17 import net.sourceforge.chaperon.model.grammar.GrammarFactory; 18 import net.sourceforge.chaperon.model.grammar.Production; 19 import net.sourceforge.chaperon.model.symbol.Nonterminal; 20 import net.sourceforge.chaperon.model.symbol.Terminal; 21 22 import org.xml.sax.InputSource ; 23 import org.xml.sax.XMLReader ; 24 25 import javax.xml.parsers.SAXParserFactory ; 26 27 public class AutomatonTestCase extends TestCase 28 { 29 private Terminal plus; 30 private Terminal mult; 31 private Terminal bopen; 32 private Terminal bclose; 33 private Terminal id; 34 private Nonterminal E; 35 private Nonterminal T; 36 private Nonterminal F; 37 private Grammar grammar; 38 39 public AutomatonTestCase(String name) 40 { 41 super(name); 42 } 43 44 public void setUp() 45 { 46 plus = new Terminal("+"); 47 mult = new Terminal("*"); 48 bopen = new Terminal("("); 49 bclose = new Terminal(")"); 50 id = new Terminal("id"); 51 52 E = new Nonterminal("E"); 53 T = new Nonterminal("T"); 54 F = new Nonterminal("F"); 55 56 grammar = new Grammar(); 57 58 Production production = new Production(E); 60 production.getDefinition().addSymbol(E); 61 production.getDefinition().addSymbol(plus); 62 production.getDefinition().addSymbol(T); 63 grammar.addProduction(production); 64 65 production = new Production(E); 67 production.getDefinition().addSymbol(T); 68 grammar.addProduction(production); 69 70 production = new Production(T); 72 production.getDefinition().addSymbol(T); 73 production.getDefinition().addSymbol(mult); 74 production.getDefinition().addSymbol(F); 75 grammar.addProduction(production); 76 77 production = new Production(T); 79 production.getDefinition().addSymbol(F); 80 grammar.addProduction(production); 81 82 production = new Production(F); 84 production.getDefinition().addSymbol(bopen); 85 production.getDefinition().addSymbol(E); 86 production.getDefinition().addSymbol(bclose); 87 grammar.addProduction(production); 88 89 production = new Production(F); 90 production.getDefinition().addSymbol(id); 91 grammar.addProduction(production); 92 93 grammar.setStartSymbol(E); 94 } 95 96 public void testConstruction() 97 { 98 System.out.println(grammar); 99 100 Automaton collection = new Automaton(grammar, 102 null); 103 104 System.out.println(collection); 105 } 106 107 public void testBigGrammar() throws Exception 108 { 109 SAXParserFactory factory = SAXParserFactory.newInstance(); 110 111 factory.setNamespaceAware(true); 112 113 XMLReader parser = factory.newSAXParser().getXMLReader(); 114 115 GrammarFactory handler = new GrammarFactory(); 116 parser.setContentHandler(handler); 117 parser.parse(new InputSource (getClass().getResourceAsStream("java.xgrm"))); 118 119 Grammar grammar = handler.getGrammar(); 120 121 Automaton collection = new Automaton(grammar, 122 null); 123 124 System.out.println(collection); 125 } 126 127 public static Test suite() 128 { 129 return new TestSuite(AutomatonTestCase.class); 130 } 131 } 132 | Popular Tags |