diff --git a/src/lib/automate/automate.c b/src/lib/automate/automate.c index 6e551c8..1ea9d14 100644 --- a/src/lib/automate/automate.c +++ b/src/lib/automate/automate.c @@ -23,8 +23,9 @@ unsigned int browse(struct Automate* pAutomate, const char* pString){ char* ptr = NULL; struct AutomateDot dotPtr; - char* pathmem = malloc( strlen(pString) ); // will contain edge number for each dot + char* pathmem = malloc( strlen(pString) ); // will contain edge number for each dot unsigned int* indexmem = malloc( sizeof(unsigned int) * strlen(pString) ); // will contain offset of @pString for each dot + char* dotmem = malloc( strlen(pString) ); /* [1] For each char =========================================================*/ @@ -52,14 +53,16 @@ unsigned int browse(struct Automate* pAutomate, const char* pString){ /* (1) Check if the current char can lead to a state ---------------------------------------------------------*/ /* (1) Check current state choices */ - dotPtr = pAutomate->dot[pAutomate->dCurrent]; - i = pathmem[c]; - l = dotPtr.n; + dotmem[c] = pAutomate->dCurrent; + dotPtr = pAutomate->dot[dotmem[c]]; indexmem[c] = strIndex; + l = dotPtr.n; + i = pathmem[c]; + + - // printf("* state %d\n", pAutomate->dCurrent); /* (2) If no more path for this branch -> exit */ - if( i >= l ) + if( c == 0 && i >= l ) break; @@ -76,23 +79,25 @@ unsigned int browse(struct Automate* pAutomate, const char* pString){ if( strcmp(buffer, ptr) == 0 ){ pAutomate->dCurrent = dotPtr.dot[i]; strIndex += strlen(ptr); + pathmem[c] = i; + break; } /* (4) If AUTOMATE match */ }else{ - recursive = browse(&pAutomate->aedge[dotPtr.edge[i]], pString+strIndex); if( pAutomate->aedge[dotPtr.edge[i]].dCurrent == pAutomate->aedge[dotPtr.edge[i]].dFinal ){ pAutomate->dCurrent = dotPtr.dot[i]; strIndex += recursive; + pathmem[c] = i; + break; } } } - /* (5) If no more path */ if( strIndex == indexmem[c] ){ @@ -101,11 +106,20 @@ unsigned int browse(struct Automate* pAutomate, const char* pString){ c--; // else next path of previous branch - strIndex = indexmem[c]; + strIndex = indexmem[c]; pathmem[c]++; - // printf("* back\n"); - }else + pAutomate->dCurrent = dotmem[c]; + // printf("* back to q%d:%d\n", dotmem[c], pathmem[c]); + + /* (6) Next branch + reset data (if already browsed in other path) */ + }else{ + // printf("q%d -%.1s-> q%d\n", c, pString+indexmem[c], pAutomate->dCurrent); c++; + pathmem[c] = 0; + } + + + } @@ -258,7 +272,7 @@ char addStringTransition(struct Automate* pAutomate, const char* pSedge){ /* [1] Reallocate memory for sedge =========================================================*/ - pAutomate->sedge = realloc(pAutomate->sedge, sizeof(char) * pAutomate->sedges ); + pAutomate->sedge = realloc(pAutomate->sedge, sizeof(char*) * pAutomate->sedges); pAutomate->sedge[index] = malloc( strlen(pSedge)+1 ); diff --git a/src/linter.c b/src/linter.c index 828e568..6669ec8 100644 --- a/src/linter.c +++ b/src/linter.c @@ -22,37 +22,42 @@ int main(int argc, char* argv[]){ /** TEST **/ /** BUILD AUTOMATE **/ - // B = c*d - struct Automate b = createAutomate(); - addDot(&b); - addDot(&b); - addStringTransition(&b, "c\0"); - addStringTransition(&b, "d\0"); - linkSEdge(&b, 0, 0, 0); // 0-0 - linkSEdge(&b, 0, 1, 1); // 0-1 - // A = a+B = a+c*d + // A = a+(bcx+bcd) struct Automate a = createAutomate(); - addDot(&a); - addDot(&a); - addDot(&a); - addStringTransition(&a, "a\0"); - addAutomateTransition(&a, &b); - linkSEdge(&a, 0, 1, 0); // 0-0 - linkSEdge(&a, 1, 1, 0); // 1-0 - linkAEdge(&a, 1, 2, 0); // 1-1 + addDot(&a); // q0 + addDot(&a); // q1 + addDot(&a); // q2 + addDot(&a); // q3 + addDot(&a); // q4 + addDot(&a); // q5 - printf("browse 'acd' : %d\n", browse(&a, "acd")); + addStringTransition(&a, "a\0"); // t0 + addStringTransition(&a, "b\0"); // t1 + addStringTransition(&a, "c\0"); // t2 + addStringTransition(&a, "d\0"); // t3 + addStringTransition(&a, "x\0"); // t4 + + linkSEdge(&a, 0, 1, 0); // q0 -t0-> q1 || branch-0 path-0 + linkSEdge(&a, 1, 1, 0); // q1 -t0-> q1 || branch-1 path-0 + linkSEdge(&a, 1, 2, 1); // q1 -t1-> q2 || branch-1 path-1 + linkSEdge(&a, 2, 3, 2); // q2 -t2-> q3 || branch-2 path-0 + linkSEdge(&a, 3, 5, 4); // q2 -t4-> q5 || branch-3 path-0 + linkSEdge(&a, 1, 4, 1); // q1 -t1-> q4 || branch-1 path-2 + linkSEdge(&a, 4, 4, 2); // q4 -t2-> q4 || branch-4 path-0 + linkSEdge(&a, 4, 5, 3); // q4 -t3-> q5 || branch-4 path-1 + + printf("browse 'abcx' : %d\n", browse(&a, "abcx")); printf("* final_state: %d\n", a.dCurrent); - printf("browse 'ad' : %d\n", browse(&a, "ad")); + printf("browse 'abcd' : %d\n", browse(&a, "abcd")); printf("* final_state: %d\n", a.dCurrent); - printf("browse 'aad' : %d\n", browse(&a, "aad")); + printf("browse 'aaaaaaabcx' : %d\n", browse(&a, "aaaaaaabcx")); printf("* final_state: %d\n", a.dCurrent); - printf("browse 'acccd' : %d\n", browse(&a, "acccd")); + printf("browse 'aaabc' : %d\n", browse(&a, "aaabc")); printf("* final_state: %d\n", a.dCurrent); - printf("browse 'aaaacccd' : %d\n", browse(&a, "aaaacccd")); + printf("browse 'aaabd' : %d\n", browse(&a, "aaabd")); printf("* final_state: %d\n", a.dCurrent); - printf("browse 'aaaaccc' : %d\n", browse(&a, "aaaaccc")); + printf("browse 'bc' : %d\n", browse(&a, "bc")); printf("* final_state: %d\n", a.dCurrent); /* Build RegExp */