KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > test > AutomatonTestCase


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

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 JavaDoc;
23 import org.xml.sax.XMLReader JavaDoc;
24
25 import javax.xml.parsers.SAXParserFactory JavaDoc;
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 JavaDoc 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     // E -> E + T
59
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     // E -> T
66
production = new Production(E);
67     production.getDefinition().addSymbol(T);
68     grammar.addProduction(production);
69
70     // T -> T * F
71
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     // T -> F
78
production = new Production(T);
79     production.getDefinition().addSymbol(F);
80     grammar.addProduction(production);
81
82     // F -> ( E )
83
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     //FirstSetCollection firstsets = new FirstSetCollection(grammar);
101
Automaton collection = new Automaton(grammar, /*firstsets,*/
102                                          null);
103
104     System.out.println(collection);
105   }
106
107   public void testBigGrammar() throws Exception JavaDoc
108   {
109     SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
110
111     factory.setNamespaceAware(true);
112
113     XMLReader JavaDoc parser = factory.newSAXParser().getXMLReader();
114
115     GrammarFactory handler = new GrammarFactory();
116     parser.setContentHandler(handler);
117     parser.parse(new InputSource JavaDoc(getClass().getResourceAsStream("java.xgrm")));
118
119     Grammar grammar = handler.getGrammar();
120
121     Automaton collection = new Automaton(grammar, /*firstsets,*/
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