ecs.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* ecs - equivalence class routines */
  2. /*-
  3. * Copyright (c) 1990 The Regents of the University of California.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Vern Paxson.
  8. *
  9. * The United States Government has rights in this work pursuant
  10. * to contract no. DE-AC03-76SF00098 between the United States
  11. * Department of Energy and the University of California.
  12. *
  13. * Redistribution and use in source and binary forms with or without
  14. * modification are permitted provided that: (1) source distributions retain
  15. * this entire copyright notice and comment, and (2) distributions including
  16. * binaries display the following acknowledgement: ``This product includes
  17. * software developed by the University of California, Berkeley and its
  18. * contributors'' in the documentation or other materials provided with the
  19. * distribution and in all advertising materials mentioning features or use
  20. * of this software. Neither the name of the University nor the names of
  21. * its contributors may be used to endorse or promote products derived from
  22. * this software without specific prior written permission.
  23. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  26. */
  27. /* $Header: /opt/vlysenkov/CVSROOT/arcadia/contrib/tools/flex-old/ecs.c,v 1.2 2007-11-30 02:28:15 pg Exp $ */
  28. #include "flexdef.h"
  29. /* ccl2ecl - convert character classes to set of equivalence classes */
  30. void ccl2ecl()
  31. {
  32. int i, ich, newlen, cclp, ccls, cclmec;
  33. for ( i = 1; i <= lastccl; ++i )
  34. {
  35. /* We loop through each character class, and for each character
  36. * in the class, add the character's equivalence class to the
  37. * new "character" class we are creating. Thus when we are all
  38. * done, character classes will really consist of collections
  39. * of equivalence classes
  40. */
  41. newlen = 0;
  42. cclp = cclmap[i];
  43. for ( ccls = 0; ccls < ccllen[i]; ++ccls )
  44. {
  45. ich = ccltbl[cclp + ccls];
  46. cclmec = ecgroup[ich];
  47. if ( cclmec > 0 )
  48. {
  49. ccltbl[cclp + newlen] = cclmec;
  50. ++newlen;
  51. }
  52. }
  53. ccllen[i] = newlen;
  54. }
  55. }
  56. /* cre8ecs - associate equivalence class numbers with class members
  57. *
  58. * fwd is the forward linked-list of equivalence class members. bck
  59. * is the backward linked-list, and num is the number of class members.
  60. *
  61. * Returned is the number of classes.
  62. */
  63. int cre8ecs( fwd, bck, num )
  64. int fwd[], bck[], num;
  65. {
  66. int i, j, numcl;
  67. numcl = 0;
  68. /* Create equivalence class numbers. From now on, ABS( bck(x) )
  69. * is the equivalence class number for object x. If bck(x)
  70. * is positive, then x is the representative of its equivalence
  71. * class.
  72. */
  73. for ( i = 1; i <= num; ++i )
  74. if ( bck[i] == NIL )
  75. {
  76. bck[i] = ++numcl;
  77. for ( j = fwd[i]; j != NIL; j = fwd[j] )
  78. bck[j] = -numcl;
  79. }
  80. return numcl;
  81. }
  82. /* mkeccl - update equivalence classes based on character class xtions
  83. *
  84. * synopsis
  85. * Char ccls[];
  86. * int lenccl, fwd[llsiz], bck[llsiz], llsiz, NUL_mapping;
  87. * void mkeccl( Char ccls[], int lenccl, int fwd[llsiz], int bck[llsiz],
  88. * int llsiz, int NUL_mapping );
  89. *
  90. * ccls contains the elements of the character class, lenccl is the
  91. * number of elements in the ccl, fwd is the forward link-list of equivalent
  92. * characters, bck is the backward link-list, and llsiz size of the link-list.
  93. *
  94. * NUL_mapping is the value which NUL (0) should be mapped to.
  95. */
  96. void mkeccl( ccls, lenccl, fwd, bck, llsiz, NUL_mapping )
  97. Char ccls[];
  98. int lenccl, fwd[], bck[], llsiz, NUL_mapping;
  99. {
  100. int cclp, oldec, newec;
  101. int cclm, i, j;
  102. static unsigned char cclflags[CSIZE]; /* initialized to all '\0' */
  103. /* Note that it doesn't matter whether or not the character class is
  104. * negated. The same results will be obtained in either case.
  105. */
  106. cclp = 0;
  107. while ( cclp < lenccl )
  108. {
  109. cclm = ccls[cclp];
  110. if ( NUL_mapping && cclm == 0 )
  111. cclm = NUL_mapping;
  112. oldec = bck[cclm];
  113. newec = cclm;
  114. j = cclp + 1;
  115. for ( i = fwd[cclm]; i != NIL && i <= llsiz; i = fwd[i] )
  116. { /* look for the symbol in the character class */
  117. for ( ; j < lenccl; ++j )
  118. {
  119. int ccl_char;
  120. if ( NUL_mapping && ccls[j] == 0 )
  121. ccl_char = NUL_mapping;
  122. else
  123. ccl_char = ccls[j];
  124. if ( ccl_char > i )
  125. break;
  126. if ( ccl_char == i && ! cclflags[j] )
  127. {
  128. /* We found an old companion of cclm
  129. * in the ccl. Link it into the new
  130. * equivalence class and flag it as
  131. * having been processed.
  132. */
  133. bck[i] = newec;
  134. fwd[newec] = i;
  135. newec = i;
  136. /* Set flag so we don't reprocess. */
  137. cclflags[j] = 1;
  138. /* Get next equivalence class member. */
  139. /* continue 2 */
  140. goto next_pt;
  141. }
  142. }
  143. /* Symbol isn't in character class. Put it in the old
  144. * equivalence class.
  145. */
  146. bck[i] = oldec;
  147. if ( oldec != NIL )
  148. fwd[oldec] = i;
  149. oldec = i;
  150. next_pt: ;
  151. }
  152. if ( bck[cclm] != NIL || oldec != bck[cclm] )
  153. {
  154. bck[cclm] = NIL;
  155. fwd[oldec] = NIL;
  156. }
  157. fwd[newec] = NIL;
  158. /* Find next ccl member to process. */
  159. for ( ++cclp; cclflags[cclp] && cclp < lenccl; ++cclp )
  160. {
  161. /* Reset "doesn't need processing" flag. */
  162. cclflags[cclp] = 0;
  163. }
  164. }
  165. }
  166. /* mkechar - create equivalence class for single character */
  167. void mkechar( tch, fwd, bck )
  168. int tch, fwd[], bck[];
  169. {
  170. /* If until now the character has been a proper subset of
  171. * an equivalence class, break it away to create a new ec
  172. */
  173. if ( fwd[tch] != NIL )
  174. bck[fwd[tch]] = bck[tch];
  175. if ( bck[tch] != NIL )
  176. fwd[bck[tch]] = fwd[tch];
  177. fwd[tch] = NIL;
  178. bck[tch] = NIL;
  179. }