Mercurial > hg > Applications > Grep
comparison regexParser/generateLoop.cc @ 312:c9ac6f06e706
add loop
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 09 Feb 2016 10:59:22 +0900 |
parents | regexParser/generateSequentialSearch.cc@1188debbef10 |
children | 90ccd94906c0 |
comparison
equal
deleted
inserted
replaced
311:1d79e61a9365 | 312:c9ac6f06e706 |
---|---|
1 #include <stdio.h> | |
2 #include <stdlib.h> | |
3 | |
4 #include "generateSequentialSearch.h" | |
5 #include "CharClass.h" | |
6 #include "subsetConstruction.h" | |
7 | |
8 | |
9 static void | |
10 generateState1(StatePtr state,long stateNum, bool accept, FILE *fp, TransitionGeneratorPtr tg) { | |
11 | |
12 fprintf(fp,"static TSValue state%lx(TSValue tsv) {\n",stateNum); | |
13 if (accept && state->accept) { | |
14 fputs(" tsv=tsv.tg->stateMatch(tsv);\n",fp); | |
15 } | |
16 fputs(" if (tsv.buff.buffptr >= tsv.buff.buffend) return tsv;\n",fp); | |
17 CharClassWalkerPtr ccw = createCharClassWalker(state->cc); | |
18 if (hasNext(ccw)) fputs(" unsigned char c = *tsv.buff.buffptr++;\n",fp); | |
19 fputs(" if (0) ;\n",fp); | |
20 while (hasNext(ccw)) { | |
21 CharClassPtr cc = getNext(ccw); | |
22 unsigned long begin = cc->cond.range.begin; | |
23 unsigned long end = cc->cond.range.end; | |
24 BitVector bi = cc->nextState; | |
25 if (begin == end) { | |
26 fprintf(fp," else if (c=='%c') { tsv.current=(TState*)state%lx;}\n",(unsigned char)begin, bi.bitContainer); | |
27 } else { | |
28 fprintf(fp," else if (c<'%c') { tsv=tsv.tg->stateSkip(tsv);tsv.current=(TState*) state0;}\n",(unsigned char)begin); | |
29 fprintf(fp," else if (c<='%c') { tsv.current=(TState*) state%lx;} \n",(unsigned char)end, bi.bitContainer); | |
30 } | |
31 } | |
32 free(ccw); | |
33 fprintf(fp," else { tsv=tsv.tg->stateSkip(tsv); tsv.current=(TState*)state0; }\n return tsv;\n"); | |
34 fputs("}\n\n",fp); | |
35 } | |
36 | |
37 void | |
38 exportStateLoop(TransitionGeneratorPtr tg) { | |
39 StatePtr state = tg->stateList; | |
40 FILE *fp = fopen("state.cc","w"); | |
41 if (fp==NULL) { | |
42 perror(""); | |
43 fprintf(stderr,"cannot write state.cc\n"); | |
44 exit(1); | |
45 } | |
46 fprintf(fp,"static TSValue state%lx(TSValue tsv) ;\n",0); | |
47 for (;state;state = state->next) { | |
48 if (state->bitState.bitContainer!=1) // state1 always unused but state0 | |
49 fprintf(fp,"static TSValue state%lx(TSValue tsv);\n",state->bitState.bitContainer); | |
50 } | |
51 fputs("\n",fp); | |
52 | |
53 fprintf(fp, | |
54 "\n" | |
55 "static TSValue \n" | |
56 "stateLoop(TSValue tsv) {\n" | |
57 " tsv.current = (TState*)state0;\n" | |
58 " while(tsv.buff.buffptr < tsv.buff.buffend) {\n" | |
59 " TSValue (*next)(TSValue) = (TSValue (*)(TSValue))tsv.current;\n" | |
60 " tsv = next(tsv);\n" | |
61 " }\n" | |
62 " return tsv;\n" | |
63 "}\n\n" ); | |
64 | |
65 | |
66 // initial state must not accept empty string | |
67 generateState1(tg->stateList,0L,false,fp,tg); | |
68 for (state = tg->stateList;state;state = state->next) { | |
69 if (state->bitState.bitContainer!=1) // state1 always unused but state0 | |
70 generateState1(state,state->bitState.bitContainer,true,fp,tg); | |
71 } | |
72 fprintf(fp,"#define state0 stateLoop\n"); | |
73 fclose(fp); | |
74 } | |
75 |