]> diplodocus.org Git - nmh/blob - zotnet/tws/lexstring.c
Added note that user questions are acceptable on nmh-workers.
[nmh] / zotnet / tws / lexstring.c
1
2 /*
3 * lexstring.c
4 *
5 * $Id$
6 */
7
8 #define ONECASE 1
9
10 #include <stdio.h>
11 #include <ctype.h>
12
13 #define YYLERR yysvec
14 #define YYTYPE int
15 #define YYLMAX 256
16
17 struct yysvf {
18 #ifndef hpux
19 struct yywork *yystoff;
20 #else /* hpux */
21 int yystoff;
22 #endif /* hpux */
23 struct yysvf *yyother;
24 int *yystops;
25 };
26
27 struct yywork {
28 YYTYPE verify;
29 YYTYPE advance;
30 };
31
32 extern int yyvstop[];
33 extern struct yywork yycrank[];
34 extern struct yysvf yysvec[];
35 extern char yymatch[];
36 extern char yyextra[];
37
38 #ifdef LEXDEBUG
39 static int debug = 0;
40 #endif /* LEXDEBUG */
41
42 #ifdef ONECASE
43 static char case_map[] = {
44 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
45 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
46 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
47 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
48 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
49 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
50 60, 61, 62, 63, 64, 97, 98, 99, 100, 101,
51 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
52 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
53 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,
54 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
55 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
56 120, 121, 122, 123, 124, 125, 126, 127,
57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0
64 };
65 #endif /* ONECASE */
66
67
68 int
69 lex_string (char **strptr, int start_cond)
70 {
71 struct yysvf *state, **lsp;
72 struct yywork *tran;
73 int statenum;
74 int ch;
75 char *cp = *strptr;
76 int *found;
77 struct yysvf *yylstate[YYLMAX];
78
79 /* start off machines */
80 lsp = yylstate;
81 statenum = 1 + start_cond;
82 state = yysvec + statenum;
83 for (;;) {
84 #ifdef LEXDEBUG
85 if (debug) {
86 fprintf(stderr,"%d ",statenum - 1);
87 }
88 #endif
89 #ifndef hpux
90 tran = state->yystoff;
91 #else /* hpux */
92 tran = &yycrank[state->yystoff];
93 #endif /* hpux */
94 if(tran == yycrank)
95 /* may not be any transitions */
96 if (state->yyother == 0 ||
97 #ifndef hpux
98 state->yyother->yystoff == yycrank)
99 #else /* hpux */
100 state->yyother->yystoff == 0)
101 #endif /* hpux */
102 break;
103
104 #ifdef ONECASE
105 ch = case_map[(unsigned char) *cp++];
106 #else /* not ONECASE */
107 ch = *cp++;
108 #endif /* ONECASE */
109 #ifdef LEXDEBUG
110 if (debug) {
111 fprintf(stderr,"(");
112 allprint(ch);
113 fprintf(stderr, ")");
114 }
115 #endif
116 tryagain:
117 #ifndef hpux
118 if ( tran > yycrank){
119 #else /* hpux */
120 if ( (int)tran > (int)yycrank){
121 #endif /* hpux */
122 tran += ch;
123 if (tran->verify == statenum){
124 if ((statenum = tran->advance) == 0){
125 /* error transitions */
126 --cp;
127 break;
128 }
129 state = statenum + yysvec;
130 *lsp++ = state;
131 goto contin;
132 }
133
134 #ifndef hpux
135 } else if(tran < yycrank) {
136 #else /* hpux */
137 } else if( (int)tran < (int)yycrank) {
138 #endif /* hpux */
139 tran = yycrank+(yycrank-tran) + ch;
140 #ifdef LEXDEBUG
141 if (debug) {
142 fprintf(stderr," compressed");
143 }
144 #endif
145 if (tran->verify == statenum){
146 if ((statenum = tran->advance) == 0)
147 /* error transitions */
148 break;
149
150 state = statenum + yysvec;
151 *lsp++ = state;
152 goto contin;
153 }
154 tran += (yymatch[ch] - ch);
155 #ifdef LEXDEBUG
156 if (debug) {
157 fprintf(stderr,"(fb ");
158 allprint(yymatch[ch]);
159 fprintf(stderr,")");
160 }
161 #endif
162 if (tran->verify == statenum){
163 if((statenum = tran->advance) == 0)
164 /* error transition */
165 break;
166
167 state = statenum + yysvec;
168 *lsp++ = state;
169 goto contin;
170 }
171 }
172 if ((state = state->yyother) &&
173 #ifndef hpux
174 (tran = state->yystoff) != yycrank){
175 #else /* hpux */
176 (tran = &yycrank[state->yystoff]) != yycrank){
177 #endif /* hpux */
178 statenum = state - yysvec;
179 #ifdef LEXDEBUG
180 if (debug) {
181 fprintf(stderr,"fb %d", statenum - 1);
182 }
183 #endif
184 goto tryagain;
185 } else
186 break;
187
188 contin:
189 #ifdef LEXDEBUG
190 if (debug) {
191 fprintf(stderr,">");
192 }
193 #endif
194 ;
195 }
196 #ifdef LEXDEBUG
197 if (debug) {
198 fprintf(stderr,"\nStopped in state %d (",*(lsp-1)-yysvec-1);
199 allprint(ch);
200 fprintf(stderr, ") ");
201 }
202 #endif
203 while (lsp-- > yylstate){
204 if (*lsp != 0 && (found= (*lsp)->yystops) && *found > 0){
205 if(yyextra[*found]){
206 /* must backup */
207 ch = -*found;
208 do {
209 while (*found && *found++ != ch)
210 ;
211 } while (lsp > yylstate &&
212 (found = (*--lsp)->yystops));
213 }
214 #ifdef LEXDEBUG
215 if (debug) {
216 fprintf(stderr," Match \"");
217 for ( cp = *strptr;
218 cp <= ((*strptr)+(lsp-yylstate));
219 cp++)
220 allprint( *cp );
221 fprintf(stderr,"\" action %d\n",*found);
222 }
223 #endif
224 *strptr += (lsp - yylstate + 1);
225 return(*found);
226 }
227 }
228 /* the string didn't match anything - if we're looking at
229 * eos, just return 0. Otherwise, bump the string pointer
230 * and return -1.
231 */
232 #ifdef LEXDEBUG
233 if (debug) {
234 fprintf(stderr," No match\n");
235 }
236 #endif /* LEXDEBUG */
237 if ( **strptr ) {
238 (*strptr)++;
239 return (-1);
240 }
241 return (0);
242 }
243
244 #ifdef LEXDEBUG
245 void
246 allprint(char c)
247 {
248 if ( c < 32 ) {
249 putc( '^', stderr );
250 c += 32;
251 } else if ( c == 127 ) {
252 putc( '^', stderr );
253 c = '?';
254 }
255 putc( c, stderr );
256 }
257 #endif /* LEXDEBUG */