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