From 1a010ce854c5a6bae9048bf6b9ffcebb5e557998 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 15 May 2017 04:47:11 +0200 Subject: [PATCH] automate now browses the whole tree for other matches forever [done] --- src/lib/automate/automate.c | 65 ++++++++++++++++++++++++++++++------- src/linter.c | 26 ++++++++++----- 2 files changed, 70 insertions(+), 21 deletions(-) diff --git a/src/lib/automate/automate.c b/src/lib/automate/automate.c index 07d66de..6e551c8 100644 --- a/src/lib/automate/automate.c +++ b/src/lib/automate/automate.c @@ -18,30 +18,55 @@ unsigned int browse(struct Automate* pAutomate, const char* pString){ =========================================================*/ unsigned int strIndex = 0; // String current char index unsigned int recursive; - int c, len, i, l; // counters + unsigned int c, len, i, l; // counters char* buffer = malloc(1); char* ptr = NULL; struct AutomateDot dotPtr; + 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 + /* [1] For each char =========================================================*/ - c = 0; len = strlen(pString); - buffer = realloc(buffer, len+1); + c = 0; + len = strlen(pString); + buffer = realloc(buffer, len+1); pAutomate->dCurrent = 0; - for( ; c < len ; c++ ){ + + /* (1) Initialize path memory + ---------------------------------------------------------*/ + for( c = 0 ; c < len ; c++ ){ + pathmem[c] = 0; + indexmem[c] = 0; + } + + + /* (2) For each char try to match + ---------------------------------------------------------*/ + c = 0; + strIndex = 0; + while( c < len ){ /* (1) Check if the current char can lead to a state ---------------------------------------------------------*/ /* (1) Check current state choices */ - dotPtr = pAutomate->dot[pAutomate->dCurrent]; - i = 0; l = dotPtr.n; + dotPtr = pAutomate->dot[pAutomate->dCurrent]; + i = pathmem[c]; + l = dotPtr.n; + indexmem[c] = strIndex; + + // printf("* state %d\n", pAutomate->dCurrent); + /* (2) If no more path for this branch -> exit */ + if( i >= l ) + break; + for( ; i < l ; i++ ){ - /* (2) If STRING match */ + /* (3) If STRING match */ if( dotPtr.type[i] == 0 ){ ptr = pAutomate->sedge[dotPtr.edge[i]]; @@ -50,17 +75,15 @@ unsigned int browse(struct Automate* pAutomate, const char* pString){ if( strcmp(buffer, ptr) == 0 ){ pAutomate->dCurrent = dotPtr.dot[i]; - strIndex += strlen(ptr); + strIndex += strlen(ptr); } - } - /* (3) If AUTOMATE match */ - else{ + /* (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 ){ - printf("rec %d from '%s'\n", recursive, pString+strIndex); pAutomate->dCurrent = dotPtr.dot[i]; strIndex += recursive; } @@ -69,9 +92,27 @@ unsigned int browse(struct Automate* pAutomate, const char* pString){ } + + /* (5) If no more path */ + if( strIndex == indexmem[c] ){ + + // if not first branch -> go previous + if( c > 0 ) + c--; + + // else next path of previous branch + strIndex = indexmem[c]; + pathmem[c]++; + // printf("* back\n"); + }else + c++; + } + ptr = NULL; free(buffer); + free(pathmem); + free(indexmem); return strIndex; diff --git a/src/linter.c b/src/linter.c index 524b059..828e568 100644 --- a/src/linter.c +++ b/src/linter.c @@ -28,8 +28,8 @@ int main(int argc, char* argv[]){ addDot(&b); addStringTransition(&b, "c\0"); addStringTransition(&b, "d\0"); - linkSEdge(&b, 0, 0, 0); - linkSEdge(&b, 0, 1, 1); + linkSEdge(&b, 0, 0, 0); // 0-0 + linkSEdge(&b, 0, 1, 1); // 0-1 // A = a+B = a+c*d struct Automate a = createAutomate(); @@ -38,14 +38,22 @@ int main(int argc, char* argv[]){ addDot(&a); addStringTransition(&a, "a\0"); addAutomateTransition(&a, &b); - linkSEdge(&a, 0, 1, 0); - linkSEdge(&a, 1, 1, 0); - linkAEdge(&a, 1, 2, 0); + linkSEdge(&a, 0, 1, 0); // 0-0 + linkSEdge(&a, 1, 1, 0); // 1-0 + linkAEdge(&a, 1, 2, 0); // 1-1 - printf("browse 'acd' : %d ; finalState = %d\n", browse(&a, "acd"), a.dCurrent); - printf("browse 'ad' : %d ; finalState = %d\n", browse(&a, "ad"), a.dCurrent); - printf("browse 'acccd' : %d ; finalState = %d\n", browse(&a, "acccd"), a.dCurrent); - printf("browse 'aaaacccd' : %d ; finalState = %d\n", browse(&a, "aaaacccd"), a.dCurrent); + printf("browse 'acd' : %d\n", browse(&a, "acd")); + printf("* final_state: %d\n", a.dCurrent); + printf("browse 'ad' : %d\n", browse(&a, "ad")); + printf("* final_state: %d\n", a.dCurrent); + printf("browse 'aad' : %d\n", browse(&a, "aad")); + printf("* final_state: %d\n", a.dCurrent); + printf("browse 'acccd' : %d\n", browse(&a, "acccd")); + printf("* final_state: %d\n", a.dCurrent); + printf("browse 'aaaacccd' : %d\n", browse(&a, "aaaacccd")); + printf("* final_state: %d\n", a.dCurrent); + printf("browse 'aaaaccc' : %d\n", browse(&a, "aaaaccc")); + printf("* final_state: %d\n", a.dCurrent); /* Build RegExp */ // char regex[40] = "(a([abc](b+a)(b+e)+ahd)*(b+i)c)sd";