view regexParser/generateSequentialSearchCbC.cc @ 312:c9ac6f06e706

add loop
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 09 Feb 2016 10:59:22 +0900
parents 1d79e61a9365
children
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>

#include "generateSequentialSearch.h"
#include "subsetConstruction.h"
#include "CharClass.h"

static void
generateState1(StatePtr state,long stateNum, bool accept, FILE *fp, TransitionGeneratorPtr tg) {
    fprintf(fp,"__code state%lx(TSValue tsv) {\n",stateNum);
    if (accept && state->accept) {
        fputs("    tsv=tsv.tg->stateMatch(tsv);\n",fp);
    }
    fputs("    if (tsv.buff.buffptr >= tsv.buff.buffend) _exit(0);\n",fp);
    CharClassWalkerPtr ccw = createCharClassWalker(state->cc);
    if (hasNext(ccw)) fputs("    unsigned char c = *tsv.buff.buffptr++;\n",fp);
    fputs("    if (0) ;\n",fp);
    while (hasNext(ccw)) {
        CharClassPtr cc = getNext(ccw);
        unsigned long begin = cc->cond.range.begin;
        unsigned long end = cc->cond.range.end;
        BitVector bi = cc->nextState;
        if (begin == end) {
            fprintf(fp,"    else if (c=='%c') { goto state%lx(tsv);}\n",(unsigned char)begin, bi.bitContainer);
        } else {
            fprintf(fp,"    else if (c<'%c')  { tsv=tsv.tg->stateSkip(tsv);goto state0(tsv);}\n",(unsigned char)begin);
            fprintf(fp,"    else if (c<='%c') { goto state%lx(tsv);} \n",(unsigned char)end,  bi.bitContainer);
        }
    }
    free(ccw);
    fprintf(fp,"    else { tsv=tsv.tg->stateSkip(tsv); goto state0(tsv);}\n");
    fputs("}\n\n",fp);
}

void
exportStateCbC(TransitionGeneratorPtr tg) {
    StatePtr state = tg->stateList;
    FILE *fp = fopen("stateCbC.cc","w");
    if (fp==NULL) {
        perror("");
        fprintf(stderr,"cannot write stateCbC.cc\n");
        exit(1);
    }
    for (;state;state = state->next) {
        fprintf(fp,"__code state%lx(TSValue tsv);\n",state->bitState.bitContainer);
    }
    fputs("\n",fp);
    // initial state must not accept empty string
    generateState1(tg->stateList,0L,false,fp,tg);
    for (state = tg->stateList;state;state = state->next) {
        generateState1(state,state->bitState.bitContainer,true,fp,tg);
    }
    fclose(fp);
}