1 //////////////////////////////////////////////////////////////////////////////
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef nondeterministic_finite_automata_nodes_h
26 #define nondeterministic_finite_automata_nodes_h
29 #include <AD/automata/dfatable.h> // DFA tables internals
30 #include <AD/contain/bitset.h>
32 //////////////////////////////////////////////////////////////////////////////
34 //////////////////////////////////////////////////////////////////////////////
38 //////////////////////////////////////////////////////////////////////////////
39 // Base class for an NFA node
40 //////////////////////////////////////////////////////////////////////////////
42 NFA_Node(const NFA_Node
&); // no copy constructor
43 void operator = (const NFA_Node
&); // no assignment
45 typedef DFATables::Symbol Symbol
;
46 typedef DFATables::State State
;
47 typedef DFATables::Rule Rule
;
49 enum { NoDeltaBit
= -1 };
50 BitSet
* closure_set
; // epsilon_closure
51 NFA_Node
* out
; // out transition
55 inline void set_out(NFA_Node
* n
) { out
= n
; }
57 virtual int closure (BitSet
*) = 0;
58 virtual BitSet
* epsilon_closure
60 size_t number_of_states
,
61 size_t number_of_rules
,
62 NFA_Delta
* state_table
[]
65 ///////////////////////////////////////////////////////////////////////////
66 // Thin the accept states. I.e. select the first accept state
67 // and eliminate the rest
68 ///////////////////////////////////////////////////////////////////////////
69 inline static Rule accept_thinning
70 (register BitSet
* set
, register size_t rules
)
71 { for (register int i
= 0; i
< rules
; i
++)
73 //for (register int j = i+1; j < rules; j++) set->remove(j);
81 //////////////////////////////////////////////////////////////////////////////
82 // Base class for an accept NFA node
83 //////////////////////////////////////////////////////////////////////////////
84 class NFA_Accept
: public NFA_Node
{
85 NFA_Accept(const NFA_Accept
&); // no copy constructor
86 void operator = (const NFA_Accept
&); // no assignment
88 State state
; // nfa state
89 virtual int closure (BitSet
*);
93 virtual BitSet
* epsilon_closure (MemPool
&, size_t, size_t, NFA_Delta
* []);
94 friend NFA_Accept
* mkaccept(MemPool
&, State
);
97 //////////////////////////////////////////////////////////////////////////////
99 //////////////////////////////////////////////////////////////////////////////
100 class NFA_Epsilon
: public NFA_Node
{
101 NFA_Epsilon(const NFA_Epsilon
&); // no copy constructor
102 void operator = (const NFA_Epsilon
&); // no assignment
104 virtual int closure (BitSet
*);
108 virtual BitSet
* epsilon_closure (MemPool
&, size_t, size_t, NFA_Delta
* []);
109 friend NFA_Epsilon
* mkepsilon(MemPool
&, NFA_Node
*);
112 //////////////////////////////////////////////////////////////////////////////
114 //////////////////////////////////////////////////////////////////////////////
115 class NFA_Or
: public NFA_Node
{
116 NFA_Or(const NFA_Or
&); // no copy constructor
117 void operator = (const NFA_Or
&); // no assignment
119 NFA_Node
* out2
; // the second out transition
120 virtual int closure (BitSet
*);
124 inline void set_out2(NFA_Node
* n
) { out2
= n
; }
125 virtual BitSet
* epsilon_closure (MemPool
&, size_t, size_t, NFA_Delta
* []);
126 friend NFA_Or
* mkor(MemPool
&, NFA_Node
*, NFA_Node
*);
129 //////////////////////////////////////////////////////////////////////////////
131 //////////////////////////////////////////////////////////////////////////////
132 class NFA_Delta
: public NFA_Accept
{
133 NFA_Delta(const NFA_Delta
&); // no copy constructor
134 void operator = (const NFA_Delta
&); // no assignment
136 Symbol c
; // out transition character
137 int delta_bit
; // state bit of delta_set if delta_set is
138 // a singleton or empty.
139 BitSet
* delta_set
; // set of out transition states(after closure)
140 virtual int closure (BitSet
*);
144 virtual BitSet
* epsilon_closure (MemPool
&, size_t, size_t, NFA_Delta
* []);
145 inline BitSet
* delta () const { return delta_set
; }
146 inline void add_delta (BitSet
* set
) const
147 { if (delta_bit
>= 0) set
->add(delta_bit
);
148 else set
->Union(*delta_set
);
150 inline Symbol
symbol () const { return c
; }
151 friend NFA_Delta
* mkdelta(MemPool
&, State
, Symbol
, NFA_Node
*);