01: /*
02: [The "BSD licence"]
03: Copyright (c) 2005-2006 Terence Parr
04: All rights reserved.
05:
06: Redistribution and use in source and binary forms, with or without
07: modification, are permitted provided that the following conditions
08: are met:
09: 1. Redistributions of source code must retain the above copyright
10: notice, this list of conditions and the following disclaimer.
11: 2. Redistributions in binary form must reproduce the above copyright
12: notice, this list of conditions and the following disclaimer in the
13: documentation and/or other materials provided with the distribution.
14: 3. The name of the author may not be used to endorse or promote products
15: derived from this software without specific prior written permission.
16:
17: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27: */
28: package org.antlr.analysis;
29:
30: import org.antlr.tool.Grammar;
31: import org.antlr.tool.NFAFactory;
32:
33: import java.util.Vector;
34:
35: /** An NFA (collection of NFAStates) constructed from a grammar. This
36: * NFA is one big machine for entire grammar. Decision points are recorded
37: * by the Grammar object so we can, for example, convert to DFA or simulate
38: * the NFA (interpret a decision).
39: */
40: public class NFA {
41: public static final int INVALID_ALT_NUMBER = -1;
42:
43: /** This NFA represents which grammar? */
44: public Grammar grammar;
45:
46: /** The NFA states in this NFA. Maps state number to NFAState object.
47: * This is a Vector instead of a List because I need to be able to grow
48: * this properly. After talking to Josh Bloch, Collections guy at Sun,
49: * I decided this was easiest solution.
50: */
51: protected Vector numberToStateList = new Vector(1000);
52:
53: /** Which factory created this NFA? */
54: protected NFAFactory factory = null;
55:
56: public NFA(Grammar g) {
57: this .grammar = g;
58: }
59:
60: public void addState(NFAState state) {
61: numberToStateList.setSize(state.stateNumber + 1); // make sure we have room
62: numberToStateList.set(state.stateNumber, state);
63: }
64:
65: public NFAState getState(int s) {
66: return (NFAState) numberToStateList.get(s);
67: }
68:
69: public NFAFactory getFactory() {
70: return factory;
71: }
72:
73: public void setFactory(NFAFactory factory) {
74: this.factory = factory;
75: }
76: }
|