2 /********************************************
4 copyright 1991, Michael D. Brennan
6 This is a source file for mawk, an implementation of
7 the AWK programming language.
9 Mawk is distributed without warranty under the terms of
10 the GNU General Public License, version 2, 1991.
11 ********************************************/
14 * Revision 1.3 1993/07/24 17:55:10 mike
17 * Revision 1.2 1993/07/23 13:21:41 mike
20 * Revision 1.1.1.1 1993/07/03 18:58:27 mike
23 * Revision 3.4 1992/02/20 16:08:12 brennan
24 * change new_TWO() to work around sun acc bug
26 * Revision 3.3 91/10/29 10:54:01 brennan
29 * Revision 3.2 91/08/13 09:10:11 brennan
32 * Revision 3.1 91/06/07 10:33:22 brennan
37 /* re machine operations */
41 static void PROTO(new_TWO, (int, MACHINE *)) ;
45 /* initialize a two state machine */
49 MACHINE *mp ; /* init mp-> */
51 mp->start = (STATE *) RE_malloc(2 * STATESZ) ;
52 mp->stop = mp->start + 1 ;
53 mp->start->type = type ;
54 mp->stop->type = M_ACCEPT ;
57 /* build a machine that recognizes any */
67 /* build a machine that recognizes the start of string */
73 new_TWO(M_START, &x) ;
86 /* build a machine that recognizes a class */
93 new_TWO(M_CLASS, &x) ;
94 x.start->data.bvp = bvp ;
116 x.start->data.str = str ;
121 /* replace m and n by a machine that recognizes mn */
126 unsigned sz1, sz2, sz ;
128 sz1 = mp->stop - mp->start ;
129 sz2 = np->stop - np->start + 1 ;
132 mp->start = (STATE *) RE_realloc(mp->start, sz * STATESZ) ;
133 mp->stop = mp->start + (sz - 1) ;
134 memcpy(mp->start + sz1, np->start, sz2 * STATESZ) ;
138 /* replace m by a machine that recognizes m|n */
147 szm = mp->stop - mp->start + 1 ;
148 szn = np->stop - np->start + 1 ;
150 p = (STATE *) RE_malloc((szm + szn + 1) * STATESZ) ;
151 memcpy(p + 1, mp->start, szm * STATESZ) ;
154 (mp->stop = p + szm + szn)->type = M_ACCEPT ;
156 p->data.jump = szm + 1 ;
157 memcpy(p + szm + 1, np->start, szn * STATESZ) ;
159 (p += szm)->type = M_1J ;
163 /* UNARY OPERATIONS */
165 /* replace m by m* */
174 sz = mp->stop - mp->start + 1 ;
175 p = (STATE *) RE_malloc((sz + 2) * STATESZ) ;
176 memcpy(p + 1, mp->start, sz * STATESZ) ;
179 mp->stop = p + (sz + 1) ;
181 p->data.jump = sz + 1 ;
182 (p += sz)->type = M_2JB ;
183 p->data.jump = -(sz - 1) ;
184 (p + 1)->type = M_ACCEPT ;
187 /* replace m by m+ (positive closure) */
196 sz = mp->stop - mp->start + 1 ;
197 mp->start = p = (STATE *) RE_realloc(mp->start, (sz + 1) * STATESZ) ;
202 (p + 1)->type = M_ACCEPT ;
205 /* replace m by m? (zero or one) */
214 sz = mp->stop - mp->start + 1 ;
215 p = (STATE *) RE_malloc((sz + 1) * STATESZ) ;
216 memcpy(p + 1, mp->start, sz * STATESZ) ;
224 /*===================================
226 *==============================*/
235 if (!(p = malloc(sz))) RE_error_trap(MEMORY_FAILURE) ;
244 if (!(p = realloc(p, sz))) RE_error_trap(MEMORY_FAILURE) ;