Blame src/ecs.c

Packit f00812
/* ecs - equivalence class routines */
Packit f00812
Packit f00812
/*  Copyright (c) 1990 The Regents of the University of California. */
Packit f00812
/*  All rights reserved. */
Packit f00812
Packit f00812
/*  This code is derived from software contributed to Berkeley by */
Packit f00812
/*  Vern Paxson. */
Packit f00812
Packit f00812
/*  The United States Government has rights in this work pursuant */
Packit f00812
/*  to contract no. DE-AC03-76SF00098 between the United States */
Packit f00812
/*  Department of Energy and the University of California. */
Packit f00812
Packit f00812
/* This file is part of flex */
Packit f00812
Packit f00812
/*  Redistribution and use in source and binary forms, with or without */
Packit f00812
/*  modification, are permitted provided that the following conditions */
Packit f00812
/*  are met: */
Packit f00812
Packit f00812
/*  1. Redistributions of source code must retain the above copyright */
Packit f00812
/*     notice, this list of conditions and the following disclaimer. */
Packit f00812
/*  2. Redistributions in binary form must reproduce the above copyright */
Packit f00812
/*     notice, this list of conditions and the following disclaimer in the */
Packit f00812
/*     documentation and/or other materials provided with the distribution. */
Packit f00812
Packit f00812
/*  Neither the name of the University nor the names of its contributors */
Packit f00812
/*  may be used to endorse or promote products derived from this software */
Packit f00812
/*  without specific prior written permission. */
Packit f00812
Packit f00812
/*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
Packit f00812
/*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
Packit f00812
/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
Packit f00812
/*  PURPOSE. */
Packit f00812
Packit f00812
Packit f00812
#include "flexdef.h"
Packit f00812
Packit f00812
/* ccl2ecl - convert character classes to set of equivalence classes */
Packit f00812
Packit f00812
void    ccl2ecl (void)
Packit f00812
{
Packit f00812
	int     i, ich, newlen, cclp, ccls, cclmec;
Packit f00812
Packit f00812
	for (i = 1; i <= lastccl; ++i) {
Packit f00812
		/* We loop through each character class, and for each character
Packit f00812
		 * in the class, add the character's equivalence class to the
Packit f00812
		 * new "character" class we are creating.  Thus when we are all
Packit f00812
		 * done, character classes will really consist of collections
Packit f00812
		 * of equivalence classes
Packit f00812
		 */
Packit f00812
Packit f00812
		newlen = 0;
Packit f00812
		cclp = cclmap[i];
Packit f00812
Packit f00812
		for (ccls = 0; ccls < ccllen[i]; ++ccls) {
Packit f00812
			ich = ccltbl[cclp + ccls];
Packit f00812
			cclmec = ecgroup[ich];
Packit f00812
Packit f00812
			if (cclmec > 0) {
Packit f00812
				ccltbl[cclp + newlen] = cclmec;
Packit f00812
				++newlen;
Packit f00812
			}
Packit f00812
		}
Packit f00812
Packit f00812
		ccllen[i] = newlen;
Packit f00812
	}
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* cre8ecs - associate equivalence class numbers with class members
Packit f00812
 *
Packit f00812
 * fwd is the forward linked-list of equivalence class members.  bck
Packit f00812
 * is the backward linked-list, and num is the number of class members.
Packit f00812
 *
Packit f00812
 * Returned is the number of classes.
Packit f00812
 */
Packit f00812
Packit f00812
int     cre8ecs (int fwd[], int bck[], int num)
Packit f00812
{
Packit f00812
	int     i, j, numcl;
Packit f00812
Packit f00812
	numcl = 0;
Packit f00812
Packit f00812
	/* Create equivalence class numbers.  From now on, ABS( bck(x) )
Packit f00812
	 * is the equivalence class number for object x.  If bck(x)
Packit f00812
	 * is positive, then x is the representative of its equivalence
Packit f00812
	 * class.
Packit f00812
	 */
Packit f00812
	for (i = 1; i <= num; ++i)
Packit f00812
		if (bck[i] == NIL) {
Packit f00812
			bck[i] = ++numcl;
Packit f00812
			for (j = fwd[i]; j != NIL; j = fwd[j])
Packit f00812
				bck[j] = -numcl;
Packit f00812
		}
Packit f00812
Packit f00812
	return numcl;
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* mkeccl - update equivalence classes based on character class xtions
Packit f00812
 *
Packit f00812
 * synopsis
Packit f00812
 *    unsigned char ccls[];
Packit f00812
 *    int lenccl, fwd[llsiz], bck[llsiz], llsiz, NUL_mapping;
Packit f00812
 *    void mkeccl( unsigned char ccls[], int lenccl, int fwd[llsiz], int bck[llsiz],
Packit f00812
 *			int llsiz, int NUL_mapping );
Packit f00812
 *
Packit f00812
 * ccls contains the elements of the character class, lenccl is the
Packit f00812
 * number of elements in the ccl, fwd is the forward link-list of equivalent
Packit f00812
 * characters, bck is the backward link-list, and llsiz size of the link-list.
Packit f00812
 *
Packit f00812
 * NUL_mapping is the value which NUL (0) should be mapped to.
Packit f00812
 */
Packit f00812
Packit f00812
void    mkeccl (unsigned char ccls[], int lenccl, int fwd[], int bck[], int llsiz, int NUL_mapping)
Packit f00812
{
Packit f00812
	int     cclp, oldec, newec;
Packit f00812
	int     cclm, i, j;
Packit f00812
	static unsigned char cclflags[CSIZE];	/* initialized to all '\0' */
Packit f00812
Packit f00812
	/* Note that it doesn't matter whether or not the character class is
Packit f00812
	 * negated.  The same results will be obtained in either case.
Packit f00812
	 */
Packit f00812
Packit f00812
	cclp = 0;
Packit f00812
Packit f00812
	while (cclp < lenccl) {
Packit f00812
		cclm = ccls[cclp];
Packit f00812
Packit f00812
		if (NUL_mapping && cclm == 0)
Packit f00812
			cclm = NUL_mapping;
Packit f00812
Packit f00812
		oldec = bck[cclm];
Packit f00812
		newec = cclm;
Packit f00812
Packit f00812
		j = cclp + 1;
Packit f00812
Packit f00812
		for (i = fwd[cclm]; i != NIL && i <= llsiz; i = fwd[i]) {	/* look for the symbol in the character class */
Packit f00812
			for (; j < lenccl; ++j) {
Packit f00812
				int ccl_char;
Packit f00812
Packit f00812
				if (NUL_mapping && ccls[j] == 0)
Packit f00812
					ccl_char = NUL_mapping;
Packit f00812
				else
Packit f00812
					ccl_char = ccls[j];
Packit f00812
Packit f00812
				if (ccl_char > i)
Packit f00812
					break;
Packit f00812
Packit f00812
				if (ccl_char == i && !cclflags[j]) {
Packit f00812
					/* We found an old companion of cclm
Packit f00812
					 * in the ccl.  Link it into the new
Packit f00812
					 * equivalence class and flag it as
Packit f00812
					 * having been processed.
Packit f00812
					 */
Packit f00812
Packit f00812
					bck[i] = newec;
Packit f00812
					fwd[newec] = i;
Packit f00812
					newec = i;
Packit f00812
					/* Set flag so we don't reprocess. */
Packit f00812
					cclflags[j] = 1;
Packit f00812
Packit f00812
					/* Get next equivalence class member. */
Packit f00812
					/* continue 2 */
Packit f00812
					goto next_pt;
Packit f00812
				}
Packit f00812
			}
Packit f00812
Packit f00812
			/* Symbol isn't in character class.  Put it in the old
Packit f00812
			 * equivalence class.
Packit f00812
			 */
Packit f00812
Packit f00812
			bck[i] = oldec;
Packit f00812
Packit f00812
			if (oldec != NIL)
Packit f00812
				fwd[oldec] = i;
Packit f00812
Packit f00812
			oldec = i;
Packit f00812
Packit f00812
		      next_pt:;
Packit f00812
		}
Packit f00812
Packit f00812
		if (bck[cclm] != NIL || oldec != bck[cclm]) {
Packit f00812
			bck[cclm] = NIL;
Packit f00812
			fwd[oldec] = NIL;
Packit f00812
		}
Packit f00812
Packit f00812
		fwd[newec] = NIL;
Packit f00812
Packit f00812
		/* Find next ccl member to process. */
Packit f00812
Packit f00812
		for (++cclp; cclp < lenccl && cclflags[cclp]; ++cclp) {
Packit f00812
			/* Reset "doesn't need processing" flag. */
Packit f00812
			cclflags[cclp] = 0;
Packit f00812
		}
Packit f00812
	}
Packit f00812
}
Packit f00812
Packit f00812
Packit f00812
/* mkechar - create equivalence class for single character */
Packit f00812
Packit f00812
void    mkechar (int tch, int fwd[], int bck[])
Packit f00812
{
Packit f00812
	/* If until now the character has been a proper subset of
Packit f00812
	 * an equivalence class, break it away to create a new ec
Packit f00812
	 */
Packit f00812
Packit f00812
	if (fwd[tch] != NIL)
Packit f00812
		bck[fwd[tch]] = bck[tch];
Packit f00812
Packit f00812
	if (bck[tch] != NIL)
Packit f00812
		fwd[bck[tch]] = fwd[tch];
Packit f00812
Packit f00812
	fwd[tch] = NIL;
Packit f00812
	bck[tch] = NIL;
Packit f00812
}