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 //////////////////////////////////////////////////////////////////////////////
26 #include <AD/automata/compdfa.h>
27 #include <AD/automata/gentable.h>
29 //////////////////////////////////////////////////////////////////////////
30 // Make hidden types visible
31 //////////////////////////////////////////////////////////////////////////
32 typedef CompressedDFA::Symbol Symbol
;
33 typedef CompressedDFA::State State
;
35 //////////////////////////////////////////////////////////////////////////
36 // Constructor and destructor
37 //////////////////////////////////////////////////////////////////////////
38 CompressedDFA::CompressedDFA() : base(0), check(0), next(0) {}
39 CompressedDFA::~CompressedDFA()
40 { delete [] base
; delete [] check
; delete [] next
; }
42 //////////////////////////////////////////////////////////////////////////
43 // Create an initial compressed dfa table
44 //////////////////////////////////////////////////////////////////////////
45 void CompressedDFA::create_tables(int size
, int states
, Symbol min
, Symbol max
)
49 number_of_states
= states
;
52 base
= new Offset
[ number_of_states
];
53 check
= new State
[ table_size
];
54 next
= new State
[ table_size
];
57 for (i
= table_size
- 1; i
>= 0; i
--)
58 check
[i
] = next
[i
] = error_state
;
59 for (i
= number_of_states
- 1; i
>= 0; i
--)
63 //////////////////////////////////////////////////////////////////////////
64 // Expand the check/next tables
65 //////////////////////////////////////////////////////////////////////////
66 void CompressedDFA::grow_tables(int increment
)
67 { State
* new_check
= new State
[ table_size
+ increment
];
68 State
* new_next
= new State
[ table_size
+ increment
];
69 memcpy(new_check
,check
,table_size
* sizeof(State
));
70 memcpy(new_next
,next
,table_size
* sizeof(State
));
75 for (register int i
= table_size
+ increment
- 1; i
>= table_size
; i
--)
76 check
[i
] = next
[i
] = error_state
;
77 table_size
+= increment
;
80 //////////////////////////////////////////////////////////////////////////
81 // Expand the number of states
82 //////////////////////////////////////////////////////////////////////////
83 void CompressedDFA::grow_states(int increment
)
84 { Offset
* new_base
= new Offset
[ number_of_states
+increment
];
85 memcpy(new_base
,base
,number_of_states
* sizeof(Offset
));
86 for (int i
= number_of_states
; i
< number_of_states
+ increment
; i
++)
90 number_of_states
+= increment
;
94 std::ostream
& CompressedDFA::gen_state_table
95 ( std::ostream
& out
, const char name
[], const char table
[],
96 const State s
[], int size
, const char * mytype
) const
98 if (mytype
== 0) mytype
= "static const DFATables::State ";
99 pr
.print(out
, (const char *)s
, size
, sizeof(State
),
100 mytype
, name
, table
, true);
104 std::ostream
& CompressedDFA::gen_offset_table
105 ( std::ostream
& out
, const char name
[], const char table
[],
106 const Offset s
[], int size
) const
108 pr
.print(out
, (const char *)s
, size
, sizeof(Offset
),
109 "static const DFATables::Offset ", name
, table
);
113 std::ostream
& CompressedDFA::gen_check_next_tables
114 (std::ostream
& out
, const char name
[], const char * mytype
) const
116 gen_state_table(out
,name
,"check",check
,max_check
+ 1,mytype
);
117 gen_state_table(out
,name
,"next",next
,max_check
+ 1,mytype
);
121 std::ostream
& CompressedDFA::gen_code (std::ostream
& out
, const char name
[]) const
123 gen_offset_table(out
,name
,"base",base
, max_state
+ 1);
124 CompressedDFA::gen_check_next_tables(out
, name
);