xref: /minix3/external/bsd/flex/dist/ecs.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1357f1050SThomas Veerman /* ecs - equivalence class routines */
2357f1050SThomas Veerman 
3357f1050SThomas Veerman /*  Copyright (c) 1990 The Regents of the University of California. */
4357f1050SThomas Veerman /*  All rights reserved. */
5357f1050SThomas Veerman 
6357f1050SThomas Veerman /*  This code is derived from software contributed to Berkeley by */
7357f1050SThomas Veerman /*  Vern Paxson. */
8357f1050SThomas Veerman 
9357f1050SThomas Veerman /*  The United States Government has rights in this work pursuant */
10357f1050SThomas Veerman /*  to contract no. DE-AC03-76SF00098 between the United States */
11357f1050SThomas Veerman /*  Department of Energy and the University of California. */
12357f1050SThomas Veerman 
13357f1050SThomas Veerman /* This file is part of flex */
14357f1050SThomas Veerman 
15357f1050SThomas Veerman /*  Redistribution and use in source and binary forms, with or without */
16357f1050SThomas Veerman /*  modification, are permitted provided that the following conditions */
17357f1050SThomas Veerman /*  are met: */
18357f1050SThomas Veerman 
19357f1050SThomas Veerman /*  1. Redistributions of source code must retain the above copyright */
20357f1050SThomas Veerman /*     notice, this list of conditions and the following disclaimer. */
21357f1050SThomas Veerman /*  2. Redistributions in binary form must reproduce the above copyright */
22357f1050SThomas Veerman /*     notice, this list of conditions and the following disclaimer in the */
23357f1050SThomas Veerman /*     documentation and/or other materials provided with the distribution. */
24357f1050SThomas Veerman 
25357f1050SThomas Veerman /*  Neither the name of the University nor the names of its contributors */
26357f1050SThomas Veerman /*  may be used to endorse or promote products derived from this software */
27357f1050SThomas Veerman /*  without specific prior written permission. */
28357f1050SThomas Veerman 
29357f1050SThomas Veerman /*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
30357f1050SThomas Veerman /*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
31357f1050SThomas Veerman /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
32357f1050SThomas Veerman /*  PURPOSE. */
33357f1050SThomas Veerman #include "flexdef.h"
34*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ecs.c,v 1.3 2014/10/30 18:44:05 christos Exp $");
35357f1050SThomas Veerman 
36357f1050SThomas Veerman /* ccl2ecl - convert character classes to set of equivalence classes */
37357f1050SThomas Veerman 
ccl2ecl()38357f1050SThomas Veerman void    ccl2ecl ()
39357f1050SThomas Veerman {
40357f1050SThomas Veerman 	int     i, ich, newlen, cclp, ccls, cclmec;
41357f1050SThomas Veerman 
42357f1050SThomas Veerman 	for (i = 1; i <= lastccl; ++i) {
43357f1050SThomas Veerman 		/* We loop through each character class, and for each character
44357f1050SThomas Veerman 		 * in the class, add the character's equivalence class to the
45357f1050SThomas Veerman 		 * new "character" class we are creating.  Thus when we are all
46357f1050SThomas Veerman 		 * done, character classes will really consist of collections
47357f1050SThomas Veerman 		 * of equivalence classes
48357f1050SThomas Veerman 		 */
49357f1050SThomas Veerman 
50357f1050SThomas Veerman 		newlen = 0;
51357f1050SThomas Veerman 		cclp = cclmap[i];
52357f1050SThomas Veerman 
53357f1050SThomas Veerman 		for (ccls = 0; ccls < ccllen[i]; ++ccls) {
54357f1050SThomas Veerman 			ich = ccltbl[cclp + ccls];
55357f1050SThomas Veerman 			cclmec = ecgroup[ich];
56357f1050SThomas Veerman 
57357f1050SThomas Veerman 			if (cclmec > 0) {
58357f1050SThomas Veerman 				ccltbl[cclp + newlen] = cclmec;
59357f1050SThomas Veerman 				++newlen;
60357f1050SThomas Veerman 			}
61357f1050SThomas Veerman 		}
62357f1050SThomas Veerman 
63357f1050SThomas Veerman 		ccllen[i] = newlen;
64357f1050SThomas Veerman 	}
65357f1050SThomas Veerman }
66357f1050SThomas Veerman 
67357f1050SThomas Veerman 
68357f1050SThomas Veerman /* cre8ecs - associate equivalence class numbers with class members
69357f1050SThomas Veerman  *
70357f1050SThomas Veerman  * fwd is the forward linked-list of equivalence class members.  bck
71357f1050SThomas Veerman  * is the backward linked-list, and num is the number of class members.
72357f1050SThomas Veerman  *
73357f1050SThomas Veerman  * Returned is the number of classes.
74357f1050SThomas Veerman  */
75357f1050SThomas Veerman 
cre8ecs(fwd,bck,num)76357f1050SThomas Veerman int     cre8ecs (fwd, bck, num)
77357f1050SThomas Veerman      int     fwd[], bck[], num;
78357f1050SThomas Veerman {
79357f1050SThomas Veerman 	int     i, j, numcl;
80357f1050SThomas Veerman 
81357f1050SThomas Veerman 	numcl = 0;
82357f1050SThomas Veerman 
83357f1050SThomas Veerman 	/* Create equivalence class numbers.  From now on, ABS( bck(x) )
84357f1050SThomas Veerman 	 * is the equivalence class number for object x.  If bck(x)
85357f1050SThomas Veerman 	 * is positive, then x is the representative of its equivalence
86357f1050SThomas Veerman 	 * class.
87357f1050SThomas Veerman 	 */
88357f1050SThomas Veerman 	for (i = 1; i <= num; ++i)
89357f1050SThomas Veerman 		if (bck[i] == NIL) {
90357f1050SThomas Veerman 			bck[i] = ++numcl;
91357f1050SThomas Veerman 			for (j = fwd[i]; j != NIL; j = fwd[j])
92357f1050SThomas Veerman 				bck[j] = -numcl;
93357f1050SThomas Veerman 		}
94357f1050SThomas Veerman 
95357f1050SThomas Veerman 	return numcl;
96357f1050SThomas Veerman }
97357f1050SThomas Veerman 
98357f1050SThomas Veerman 
99357f1050SThomas Veerman /* mkeccl - update equivalence classes based on character class xtions
100357f1050SThomas Veerman  *
101357f1050SThomas Veerman  * synopsis
102357f1050SThomas Veerman  *    Char ccls[];
103357f1050SThomas Veerman  *    int lenccl, fwd[llsiz], bck[llsiz], llsiz, NUL_mapping;
104357f1050SThomas Veerman  *    void mkeccl( Char ccls[], int lenccl, int fwd[llsiz], int bck[llsiz],
105357f1050SThomas Veerman  *			int llsiz, int NUL_mapping );
106357f1050SThomas Veerman  *
107357f1050SThomas Veerman  * ccls contains the elements of the character class, lenccl is the
108357f1050SThomas Veerman  * number of elements in the ccl, fwd is the forward link-list of equivalent
109357f1050SThomas Veerman  * characters, bck is the backward link-list, and llsiz size of the link-list.
110357f1050SThomas Veerman  *
111357f1050SThomas Veerman  * NUL_mapping is the value which NUL (0) should be mapped to.
112357f1050SThomas Veerman  */
113357f1050SThomas Veerman 
mkeccl(ccls,lenccl,fwd,bck,llsiz,NUL_mapping)114357f1050SThomas Veerman void    mkeccl (ccls, lenccl, fwd, bck, llsiz, NUL_mapping)
115357f1050SThomas Veerman      Char    ccls[];
116357f1050SThomas Veerman      int     lenccl, fwd[], bck[], llsiz, NUL_mapping;
117357f1050SThomas Veerman {
118357f1050SThomas Veerman 	int     cclp, oldec, newec;
119357f1050SThomas Veerman 	int     cclm, i, j;
120357f1050SThomas Veerman 	static unsigned char cclflags[CSIZE];	/* initialized to all '\0' */
121357f1050SThomas Veerman 
122357f1050SThomas Veerman 	/* Note that it doesn't matter whether or not the character class is
123357f1050SThomas Veerman 	 * negated.  The same results will be obtained in either case.
124357f1050SThomas Veerman 	 */
125357f1050SThomas Veerman 
126357f1050SThomas Veerman 	cclp = 0;
127357f1050SThomas Veerman 
128357f1050SThomas Veerman 	while (cclp < lenccl) {
129357f1050SThomas Veerman 		cclm = ccls[cclp];
130357f1050SThomas Veerman 
131357f1050SThomas Veerman 		if (NUL_mapping && cclm == 0)
132357f1050SThomas Veerman 			cclm = NUL_mapping;
133357f1050SThomas Veerman 
134357f1050SThomas Veerman 		oldec = bck[cclm];
135357f1050SThomas Veerman 		newec = cclm;
136357f1050SThomas Veerman 
137357f1050SThomas Veerman 		j = cclp + 1;
138357f1050SThomas Veerman 
139357f1050SThomas Veerman 		for (i = fwd[cclm]; i != NIL && i <= llsiz; i = fwd[i]) {	/* look for the symbol in the character class */
140357f1050SThomas Veerman 			for (; j < lenccl; ++j) {
141357f1050SThomas Veerman 				register int ccl_char;
142357f1050SThomas Veerman 
143357f1050SThomas Veerman 				if (NUL_mapping && ccls[j] == 0)
144357f1050SThomas Veerman 					ccl_char = NUL_mapping;
145357f1050SThomas Veerman 				else
146357f1050SThomas Veerman 					ccl_char = ccls[j];
147357f1050SThomas Veerman 
148357f1050SThomas Veerman 				if (ccl_char > i)
149357f1050SThomas Veerman 					break;
150357f1050SThomas Veerman 
151357f1050SThomas Veerman 				if (ccl_char == i && !cclflags[j]) {
152357f1050SThomas Veerman 					/* We found an old companion of cclm
153357f1050SThomas Veerman 					 * in the ccl.  Link it into the new
154357f1050SThomas Veerman 					 * equivalence class and flag it as
155357f1050SThomas Veerman 					 * having been processed.
156357f1050SThomas Veerman 					 */
157357f1050SThomas Veerman 
158357f1050SThomas Veerman 					bck[i] = newec;
159357f1050SThomas Veerman 					fwd[newec] = i;
160357f1050SThomas Veerman 					newec = i;
161357f1050SThomas Veerman 					/* Set flag so we don't reprocess. */
162357f1050SThomas Veerman 					cclflags[j] = 1;
163357f1050SThomas Veerman 
164357f1050SThomas Veerman 					/* Get next equivalence class member. */
165357f1050SThomas Veerman 					/* continue 2 */
166357f1050SThomas Veerman 					goto next_pt;
167357f1050SThomas Veerman 				}
168357f1050SThomas Veerman 			}
169357f1050SThomas Veerman 
170357f1050SThomas Veerman 			/* Symbol isn't in character class.  Put it in the old
171357f1050SThomas Veerman 			 * equivalence class.
172357f1050SThomas Veerman 			 */
173357f1050SThomas Veerman 
174357f1050SThomas Veerman 			bck[i] = oldec;
175357f1050SThomas Veerman 
176357f1050SThomas Veerman 			if (oldec != NIL)
177357f1050SThomas Veerman 				fwd[oldec] = i;
178357f1050SThomas Veerman 
179357f1050SThomas Veerman 			oldec = i;
180357f1050SThomas Veerman 
181357f1050SThomas Veerman 		      next_pt:;
182357f1050SThomas Veerman 		}
183357f1050SThomas Veerman 
184357f1050SThomas Veerman 		if (bck[cclm] != NIL || oldec != bck[cclm]) {
185357f1050SThomas Veerman 			bck[cclm] = NIL;
186357f1050SThomas Veerman 			fwd[oldec] = NIL;
187357f1050SThomas Veerman 		}
188357f1050SThomas Veerman 
189357f1050SThomas Veerman 		fwd[newec] = NIL;
190357f1050SThomas Veerman 
191357f1050SThomas Veerman 		/* Find next ccl member to process. */
192357f1050SThomas Veerman 
193357f1050SThomas Veerman 		for (++cclp; cclflags[cclp] && cclp < lenccl; ++cclp) {
194357f1050SThomas Veerman 			/* Reset "doesn't need processing" flag. */
195357f1050SThomas Veerman 			cclflags[cclp] = 0;
196357f1050SThomas Veerman 		}
197357f1050SThomas Veerman 	}
198357f1050SThomas Veerman }
199357f1050SThomas Veerman 
200357f1050SThomas Veerman 
201357f1050SThomas Veerman /* mkechar - create equivalence class for single character */
202357f1050SThomas Veerman 
mkechar(tch,fwd,bck)203357f1050SThomas Veerman void    mkechar (tch, fwd, bck)
204357f1050SThomas Veerman      int     tch, fwd[], bck[];
205357f1050SThomas Veerman {
206357f1050SThomas Veerman 	/* If until now the character has been a proper subset of
207357f1050SThomas Veerman 	 * an equivalence class, break it away to create a new ec
208357f1050SThomas Veerman 	 */
209357f1050SThomas Veerman 
210357f1050SThomas Veerman 	if (fwd[tch] != NIL)
211357f1050SThomas Veerman 		bck[fwd[tch]] = bck[tch];
212357f1050SThomas Veerman 
213357f1050SThomas Veerman 	if (bck[tch] != NIL)
214357f1050SThomas Veerman 		fwd[bck[tch]] = fwd[tch];
215357f1050SThomas Veerman 
216357f1050SThomas Veerman 	fwd[tch] = NIL;
217357f1050SThomas Veerman 	bck[tch] = NIL;
218357f1050SThomas Veerman }
219