NetBSD/usr.bin/lex/ecs.c

226 lines
5.5 KiB
C
Raw Normal View History

1993-05-04 11:44:39 +04:00
/* ecs - equivalence class routines */
1993-03-21 12:45:37 +03:00
/*-
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
1993-05-04 11:44:39 +04:00
* Vern Paxson.
1993-03-21 12:45:37 +03:00
*
* 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.
*
1993-05-04 11:44:39 +04:00
* Redistribution and use in source and binary forms are permitted provided
* that: (1) source distributions retain this entire copyright notice and
* comment, and (2) distributions including binaries display the following
* acknowledgement: ``This product includes software developed by the
* University of California, Berkeley and its contributors'' in the
* documentation or other materials provided with the distribution and in
* all advertising materials mentioning features or use of this software.
* 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.
1993-03-21 12:45:37 +03:00
*/
1993-12-09 22:06:03 +03:00
/* $Header: /cvsroot/src/usr.bin/lex/Attic/ecs.c,v 1.6 1993/12/09 19:06:07 jtc Exp $ */
1993-03-21 12:45:37 +03:00
#include "flexdef.h"
1993-12-02 22:17:24 +03:00
/* ccl2ecl - convert character classes to set of equivalence classes */
1993-03-21 12:45:37 +03:00
void ccl2ecl()
{
1993-12-02 22:17:24 +03:00
int i, ich, newlen, cclp, ccls, cclmec;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
for ( i = 1; i <= lastccl; ++i )
1993-03-21 12:45:37 +03:00
{
1993-12-02 22:17:24 +03: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
1993-03-21 12:45:37 +03:00
*/
1993-12-02 22:17:24 +03:00
newlen = 0;
cclp = cclmap[i];
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
for ( ccls = 0; ccls < ccllen[i]; ++ccls )
{
ich = ccltbl[cclp + ccls];
cclmec = ecgroup[ich];
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
if ( cclmec > 0 )
{
ccltbl[cclp + newlen] = cclmec;
++newlen;
}
}
ccllen[i] = newlen;
}
1993-03-21 12:45:37 +03:00
}
/* cre8ecs - associate equivalence class numbers with class members
*
1993-12-02 22:17:24 +03:00
* fwd is the forward linked-list of equivalence class members. bck
* is the backward linked-list, and num is the number of class members.
1993-03-21 12:45:37 +03:00
*
1993-12-02 22:17:24 +03:00
* Returned is the number of classes.
1993-03-21 12:45:37 +03:00
*/
int cre8ecs( fwd, bck, num )
int fwd[], bck[], num;
{
1993-12-02 22:17:24 +03:00
int i, j, numcl;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
numcl = 0;
1993-03-21 12:45:37 +03:00
1993-12-09 22:06:03 +03:00
/* Create equivalence class numbers. From now on, ABS( bck(x) )
1993-12-02 22:17:24 +03:00
* is the equivalence class number for object x. If bck(x)
* is positive, then x is the representative of its equivalence
* class.
1993-03-21 12:45:37 +03:00
*/
1993-12-02 22:17:24 +03:00
for ( i = 1; i <= num; ++i )
if ( bck[i] == NIL )
{
bck[i] = ++numcl;
for ( j = fwd[i]; j != NIL; j = fwd[j] )
bck[j] = -numcl;
}
return numcl;
1993-03-21 12:45:37 +03:00
}
/* mkeccl - update equivalence classes based on character class xtions
*
* synopsis
* Char ccls[];
* int lenccl, fwd[llsiz], bck[llsiz], llsiz, NUL_mapping;
1993-12-02 22:17:24 +03:00
* void mkeccl( Char ccls[], int lenccl, int fwd[llsiz], int bck[llsiz],
* int llsiz, int NUL_mapping );
1993-03-21 12:45:37 +03:00
*
1993-12-02 22:17:24 +03:00
* ccls contains the elements of the character class, lenccl is the
1993-03-21 12:45:37 +03:00
* number of elements in the ccl, fwd is the forward link-list of equivalent
1993-12-02 22:17:24 +03:00
* characters, bck is the backward link-list, and llsiz size of the link-list.
1993-03-21 12:45:37 +03:00
*
* NUL_mapping is the value which NUL (0) should be mapped to.
*/
void mkeccl( ccls, lenccl, fwd, bck, llsiz, NUL_mapping )
Char ccls[];
int lenccl, fwd[], bck[], llsiz, NUL_mapping;
1993-12-02 22:17:24 +03:00
{
int cclp, oldec, newec;
int cclm, i, j;
static unsigned char cclflags[CSIZE]; /* initialized to all '\0' */
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* Note that it doesn't matter whether or not the character class is
* negated. The same results will be obtained in either case.
*/
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
cclp = 0;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
while ( cclp < lenccl )
{
cclm = ccls[cclp];
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
if ( NUL_mapping && cclm == 0 )
cclm = NUL_mapping;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
oldec = bck[cclm];
newec = cclm;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
j = cclp + 1;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
for ( i = fwd[cclm]; i != NIL && i <= llsiz; i = fwd[i] )
{ /* look for the symbol in the character class */
for ( ; j < lenccl; ++j )
{
register int ccl_char;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
if ( NUL_mapping && ccls[j] == 0 )
ccl_char = NUL_mapping;
else
ccl_char = ccls[j];
if ( ccl_char > i )
break;
if ( ccl_char == i && ! cclflags[j] )
{
/* We found an old companion of cclm
* in the ccl. Link it into the new
* equivalence class and flag it as
* having been processed.
*/
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
bck[i] = newec;
fwd[newec] = i;
newec = i;
/* Set flag so we don't reprocess. */
cclflags[j] = 1;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* Get next equivalence class member. */
/* continue 2 */
goto next_pt;
}
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
/* Symbol isn't in character class. Put it in the old
* equivalence class.
*/
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
bck[i] = oldec;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
if ( oldec != NIL )
fwd[oldec] = i;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
oldec = i;
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
next_pt: ;
}
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
if ( bck[cclm] != NIL || oldec != bck[cclm] )
{
bck[cclm] = NIL;
fwd[oldec] = NIL;
}
fwd[newec] = NIL;
/* Find next ccl member to process. */
for ( ++cclp; cclflags[cclp] && cclp < lenccl; ++cclp )
{
/* Reset "doesn't need processing" flag. */
cclflags[cclp] = 0;
}
}
1993-03-21 12:45:37 +03:00
}
1993-12-02 22:17:24 +03:00
/* mkechar - create equivalence class for single character */
1993-03-21 12:45:37 +03:00
void mkechar( tch, fwd, bck )
int tch, fwd[], bck[];
1993-12-02 22:17:24 +03:00
{
/* If until now the character has been a proper subset of
* an equivalence class, break it away to create a new ec
*/
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
if ( fwd[tch] != NIL )
bck[fwd[tch]] = bck[tch];
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
if ( bck[tch] != NIL )
fwd[bck[tch]] = fwd[tch];
1993-03-21 12:45:37 +03:00
1993-12-02 22:17:24 +03:00
fwd[tch] = NIL;
bck[tch] = NIL;
}