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/fbitset.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 FastBitSet
* closure_set
; // epsilon_closure
51 NFA_Node
* out
; // out transition
55 inline void set_out(NFA_Node
* n
) { out
= n
; }
57 virtual void closure (FastBitSet
*) = 0;
58 virtual FastBitSet
* 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 FastBitSet
* set
, register size_t rules
)
71 { int first_bit
= set
->first_bit();
72 return first_bit
< (int)rules
? first_bit
+ 1 : 0;
76 //////////////////////////////////////////////////////////////////////////////
77 // Base class for an accept NFA node
78 //////////////////////////////////////////////////////////////////////////////
79 class NFA_Accept
: public NFA_Node
{
80 NFA_Accept(const NFA_Accept
&); // no copy constructor
81 void operator = (const NFA_Accept
&); // no assignment
83 State state
; // nfa state
84 virtual void closure (FastBitSet
*);
88 virtual FastBitSet
* epsilon_closure (MemPool
&, size_t, size_t, NFA_Delta
* []);
89 friend NFA_Accept
* mkaccept(MemPool
&, State
);
92 //////////////////////////////////////////////////////////////////////////////
94 //////////////////////////////////////////////////////////////////////////////
95 class NFA_Epsilon
: public NFA_Node
{
96 NFA_Epsilon(const NFA_Epsilon
&); // no copy constructor
97 void operator = (const NFA_Epsilon
&); // no assignment
99 virtual void closure (FastBitSet
*);
103 virtual FastBitSet
* epsilon_closure (MemPool
&, size_t, size_t, NFA_Delta
* []);
104 friend NFA_Epsilon
* mkepsilon(MemPool
&, NFA_Node
*);
107 //////////////////////////////////////////////////////////////////////////////
109 //////////////////////////////////////////////////////////////////////////////
110 class NFA_Or
: public NFA_Node
{
111 NFA_Or(const NFA_Or
&); // no copy constructor
112 void operator = (const NFA_Or
&); // no assignment
114 NFA_Node
* out2
; // the second out transition
115 virtual void closure (FastBitSet
*);
119 inline void set_out2(NFA_Node
* n
) { out2
= n
; }
120 virtual FastBitSet
* epsilon_closure (MemPool
&, size_t, size_t, NFA_Delta
* []);
121 friend NFA_Or
* mkor(MemPool
&, NFA_Node
*, NFA_Node
*);
124 //////////////////////////////////////////////////////////////////////////////
126 //////////////////////////////////////////////////////////////////////////////
127 class NFA_Delta
: public NFA_Accept
{
128 NFA_Delta(const NFA_Delta
&); // no copy constructor
129 void operator = (const NFA_Delta
&); // no assignment
131 Symbol c
; // out transition character
132 FastBitSet
* delta_set
; // set of out transition states(after closure)
133 virtual void closure (FastBitSet
*);
137 virtual FastBitSet
* epsilon_closure (MemPool
&, size_t, size_t, NFA_Delta
* []);
138 inline const FastBitSet
* delta () const { return delta_set
; }
139 inline void add_delta (FastBitSet
* set
) const
140 { set
->Union(*delta_set
); }
141 inline Symbol
symbol () const { return c
; }
142 friend NFA_Delta
* mkdelta(MemPool
&, State
, Symbol
, NFA_Node
*);