1 | /* Copyright (C) 1995, 1996 Tom Lord |
---|
2 | * |
---|
3 | * This program is free software; you can redistribute it and/or modify |
---|
4 | * it under the terms of the GNU Library General Public License as published by |
---|
5 | * the Free Software Foundation; either version 2, or (at your option) |
---|
6 | * any later version. |
---|
7 | * |
---|
8 | * This program is distributed in the hope that it will be useful, |
---|
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
11 | * GNU Library General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU Library General Public License |
---|
14 | * along with this software; see the file COPYING. If not, write to |
---|
15 | * the Free Software Foundation, 59 Temple Place - Suite 330, |
---|
16 | * Boston, MA 02111-1307, USA. |
---|
17 | */ |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | #include <stdio.h> |
---|
22 | #include "rxall.h" |
---|
23 | #include "rxgnucomp.h" |
---|
24 | #include "rxnfa.h" |
---|
25 | |
---|
26 | |
---|
27 | #ifdef HAVE_POSITIONAL_ARRAY_INITS |
---|
28 | #define AT(X) [X] = |
---|
29 | #else |
---|
30 | #define AT(X) |
---|
31 | #endif |
---|
32 | |
---|
33 | |
---|
34 | char *node_type_names[] = |
---|
35 | { |
---|
36 | AT(r_cset) "r_cset", |
---|
37 | AT(r_concat) "r_concat", |
---|
38 | AT(r_alternate) "r_alternate", |
---|
39 | AT(r_opt) "r_opt", |
---|
40 | AT(r_star) "r_star", |
---|
41 | AT(r_plus) "r_plus", |
---|
42 | AT(r_string) "r_string", |
---|
43 | AT(r_cut) "r_cut", |
---|
44 | |
---|
45 | AT(r_interval) "r_interval", |
---|
46 | AT(r_parens) "r_parens", |
---|
47 | AT(r_context) "r_context" |
---|
48 | }; |
---|
49 | |
---|
50 | void |
---|
51 | print_cset (cset_size, cs) |
---|
52 | int cset_size; |
---|
53 | rx_Bitset cs; |
---|
54 | { |
---|
55 | int x; |
---|
56 | if (!cs) |
---|
57 | printf ("nil"); |
---|
58 | else |
---|
59 | { |
---|
60 | putchar ('['); |
---|
61 | for (x = 0; x < cset_size; ++x) |
---|
62 | if (RX_bitset_member (cs, x)) |
---|
63 | { |
---|
64 | if (isprint(x)) |
---|
65 | putchar (x); |
---|
66 | else |
---|
67 | printf ("\\0%o ", x); |
---|
68 | } |
---|
69 | putchar (']'); |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | void |
---|
74 | print_string(struct rx_string *s, char bracket) |
---|
75 | { |
---|
76 | int x; |
---|
77 | if (!s && bracket) |
---|
78 | printf ("nil"); |
---|
79 | else |
---|
80 | { |
---|
81 | if (bracket) |
---|
82 | putchar ('\"'); |
---|
83 | for (x = 0; x < s->len; ++x) |
---|
84 | if (isprint(s->contents[x])) |
---|
85 | putchar (s->contents[x]); |
---|
86 | else |
---|
87 | printf ("\\0%o ", x); |
---|
88 | if (bracket) |
---|
89 | putchar ('\"'); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | void |
---|
94 | spaces (n) |
---|
95 | int n; |
---|
96 | { |
---|
97 | while (n--) |
---|
98 | putchar (' '); |
---|
99 | } |
---|
100 | |
---|
101 | void |
---|
102 | print_rexp (cset_size, indent, rexp) |
---|
103 | int cset_size; |
---|
104 | int indent; |
---|
105 | struct rexp_node * rexp; |
---|
106 | { |
---|
107 | spaces (indent); |
---|
108 | if (!rexp) |
---|
109 | printf ("nil\n"); |
---|
110 | else |
---|
111 | { |
---|
112 | printf ("Node %d type %d (%s), iv=%d(%c), iv2=%d, len=%d obs=%d cs=", |
---|
113 | rexp->id, rexp->type, node_type_names[rexp->type], |
---|
114 | rexp->params.intval, |
---|
115 | (isprint (rexp->params.intval) |
---|
116 | ? rexp->params.intval |
---|
117 | : ' '), |
---|
118 | rexp->params.intval2, |
---|
119 | rexp->len, |
---|
120 | rexp->observed); |
---|
121 | print_cset (cset_size, rexp->params.cset); |
---|
122 | printf (" s="); |
---|
123 | print_string (&(rexp->params.cstr), 1); |
---|
124 | putchar ('\n'); |
---|
125 | if (rexp->params.pair.left || rexp->params.pair.right) |
---|
126 | { |
---|
127 | print_rexp (cset_size, indent + 2, rexp->params.pair.left); |
---|
128 | print_rexp (cset_size, indent + 2, rexp->params.pair.right); |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | |
---|
136 | void |
---|
137 | unparse_print_rexp (cset_size, rexp) |
---|
138 | int cset_size; |
---|
139 | struct rexp_node * rexp; |
---|
140 | { |
---|
141 | if (!rexp) |
---|
142 | return; |
---|
143 | else |
---|
144 | switch (rexp->type) |
---|
145 | { |
---|
146 | case r_cset: |
---|
147 | if (1 != rx_bitset_population (cset_size, rexp->params.cset)) |
---|
148 | print_cset (cset_size, rexp->params.cset); |
---|
149 | else |
---|
150 | { |
---|
151 | int x; |
---|
152 | rx_Bitset cs; |
---|
153 | |
---|
154 | cs = rexp->params.cset; |
---|
155 | for (x = 0; x < cset_size; ++x) |
---|
156 | if (RX_bitset_member (cs, x)) |
---|
157 | { |
---|
158 | if (isprint(x)) |
---|
159 | putchar (x); |
---|
160 | else |
---|
161 | printf ("\\0%o ", x); |
---|
162 | } |
---|
163 | } |
---|
164 | break; |
---|
165 | |
---|
166 | case r_string: |
---|
167 | print_string (&(rexp->params.cstr), 0); |
---|
168 | break; |
---|
169 | |
---|
170 | case r_parens: |
---|
171 | putchar ('('); |
---|
172 | unparse_print_rexp (cset_size, rexp->params.pair.left); |
---|
173 | putchar (')'); |
---|
174 | break; |
---|
175 | |
---|
176 | case r_context: |
---|
177 | putchar ('\\'); |
---|
178 | putchar (rexp->params.intval); |
---|
179 | break; |
---|
180 | |
---|
181 | case r_cut: |
---|
182 | printf ("[[:cut %d:]]", rexp->params.intval); |
---|
183 | break; |
---|
184 | |
---|
185 | case r_concat: |
---|
186 | unparse_print_rexp (cset_size, rexp->params.pair.left); |
---|
187 | unparse_print_rexp (cset_size, rexp->params.pair.right); |
---|
188 | break; |
---|
189 | |
---|
190 | case r_alternate: |
---|
191 | unparse_print_rexp (cset_size, rexp->params.pair.left); |
---|
192 | putchar ('|'); |
---|
193 | unparse_print_rexp (cset_size, rexp->params.pair.right); |
---|
194 | break; |
---|
195 | |
---|
196 | case r_opt: |
---|
197 | unparse_print_rexp (cset_size, rexp->params.pair.left); |
---|
198 | putchar ('?'); |
---|
199 | break; |
---|
200 | |
---|
201 | case r_star: |
---|
202 | unparse_print_rexp (cset_size, rexp->params.pair.left); |
---|
203 | putchar ('*'); |
---|
204 | break; |
---|
205 | |
---|
206 | case r_plus: |
---|
207 | unparse_print_rexp (cset_size, rexp->params.pair.left); |
---|
208 | putchar ('+'); |
---|
209 | break; |
---|
210 | |
---|
211 | case r_interval: |
---|
212 | unparse_print_rexp (cset_size, rexp->params.pair.left); |
---|
213 | printf ("{%d,%d}", rexp->params.intval, rexp->params.intval2); |
---|
214 | break; |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | void |
---|
220 | print_nfa_state (rx, state) |
---|
221 | struct rx * rx; |
---|
222 | struct rx_nfa_state * state; |
---|
223 | { |
---|
224 | struct rx_nfa_edge * e; |
---|
225 | printf ("state %d, is_final %d, is_start %d\n", |
---|
226 | state->id, state->is_final, state->is_start); |
---|
227 | for (e = state->edges; e; e = e->next) |
---|
228 | { |
---|
229 | printf ("\tEdge %s to %d ", |
---|
230 | (e->type == ne_cset |
---|
231 | ? "cset" |
---|
232 | : (e->type == ne_epsilon |
---|
233 | ? "epsilon" |
---|
234 | : "side effect")), |
---|
235 | e->dest->id); |
---|
236 | if (e->type == ne_cset) |
---|
237 | print_cset (rx->local_cset_size, e->params.cset); |
---|
238 | else |
---|
239 | printf ("%d", (int)e->params.side_effect); |
---|
240 | putchar ('\n'); |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | void |
---|
245 | print_nfa (rx) |
---|
246 | struct rx * rx; |
---|
247 | { |
---|
248 | struct rx_nfa_state * state; |
---|
249 | for (state = rx->nfa_states; state; state = state->next) |
---|
250 | print_nfa_state (rx, state); |
---|
251 | } |
---|