automate now browses the whole tree for other matches forever [done]
This commit is contained in:
parent
1fbc440c58
commit
1a010ce854
|
@ -18,30 +18,55 @@ unsigned int browse(struct Automate* pAutomate, const char* pString){
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
unsigned int strIndex = 0; // String current char index
|
unsigned int strIndex = 0; // String current char index
|
||||||
unsigned int recursive;
|
unsigned int recursive;
|
||||||
int c, len, i, l; // counters
|
unsigned int c, len, i, l; // counters
|
||||||
char* buffer = malloc(1);
|
char* buffer = malloc(1);
|
||||||
char* ptr = NULL;
|
char* ptr = NULL;
|
||||||
struct AutomateDot dotPtr;
|
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
|
/* [1] For each char
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
c = 0; len = strlen(pString);
|
c = 0;
|
||||||
|
len = strlen(pString);
|
||||||
buffer = realloc(buffer, len+1);
|
buffer = realloc(buffer, len+1);
|
||||||
pAutomate->dCurrent = 0;
|
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 if the current char can lead to a state
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
/* (1) Check current state choices */
|
/* (1) Check current state choices */
|
||||||
dotPtr = pAutomate->dot[pAutomate->dCurrent];
|
dotPtr = pAutomate->dot[pAutomate->dCurrent];
|
||||||
i = 0; l = dotPtr.n;
|
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++ ){
|
for( ; i < l ; i++ ){
|
||||||
|
|
||||||
/* (2) If STRING match */
|
/* (3) If STRING match */
|
||||||
if( dotPtr.type[i] == 0 ){
|
if( dotPtr.type[i] == 0 ){
|
||||||
|
|
||||||
ptr = pAutomate->sedge[dotPtr.edge[i]];
|
ptr = pAutomate->sedge[dotPtr.edge[i]];
|
||||||
|
@ -52,15 +77,13 @@ unsigned int browse(struct Automate* pAutomate, const char* pString){
|
||||||
pAutomate->dCurrent = dotPtr.dot[i];
|
pAutomate->dCurrent = dotPtr.dot[i];
|
||||||
strIndex += strlen(ptr);
|
strIndex += strlen(ptr);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* (3) If AUTOMATE match */
|
/* (4) If AUTOMATE match */
|
||||||
else{
|
}else{
|
||||||
|
|
||||||
recursive = browse(&pAutomate->aedge[dotPtr.edge[i]], pString+strIndex);
|
recursive = browse(&pAutomate->aedge[dotPtr.edge[i]], pString+strIndex);
|
||||||
|
|
||||||
if( pAutomate->aedge[dotPtr.edge[i]].dCurrent == pAutomate->aedge[dotPtr.edge[i]].dFinal ){
|
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];
|
pAutomate->dCurrent = dotPtr.dot[i];
|
||||||
strIndex += recursive;
|
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(buffer);
|
||||||
|
free(pathmem);
|
||||||
|
free(indexmem);
|
||||||
|
|
||||||
return strIndex;
|
return strIndex;
|
||||||
|
|
||||||
|
|
26
src/linter.c
26
src/linter.c
|
@ -28,8 +28,8 @@ int main(int argc, char* argv[]){
|
||||||
addDot(&b);
|
addDot(&b);
|
||||||
addStringTransition(&b, "c\0");
|
addStringTransition(&b, "c\0");
|
||||||
addStringTransition(&b, "d\0");
|
addStringTransition(&b, "d\0");
|
||||||
linkSEdge(&b, 0, 0, 0);
|
linkSEdge(&b, 0, 0, 0); // 0-0
|
||||||
linkSEdge(&b, 0, 1, 1);
|
linkSEdge(&b, 0, 1, 1); // 0-1
|
||||||
|
|
||||||
// A = a+B = a+c*d
|
// A = a+B = a+c*d
|
||||||
struct Automate a = createAutomate();
|
struct Automate a = createAutomate();
|
||||||
|
@ -38,14 +38,22 @@ int main(int argc, char* argv[]){
|
||||||
addDot(&a);
|
addDot(&a);
|
||||||
addStringTransition(&a, "a\0");
|
addStringTransition(&a, "a\0");
|
||||||
addAutomateTransition(&a, &b);
|
addAutomateTransition(&a, &b);
|
||||||
linkSEdge(&a, 0, 1, 0);
|
linkSEdge(&a, 0, 1, 0); // 0-0
|
||||||
linkSEdge(&a, 1, 1, 0);
|
linkSEdge(&a, 1, 1, 0); // 1-0
|
||||||
linkAEdge(&a, 1, 2, 0);
|
linkAEdge(&a, 1, 2, 0); // 1-1
|
||||||
|
|
||||||
printf("browse 'acd' : %d ; finalState = %d\n", browse(&a, "acd"), a.dCurrent);
|
printf("browse 'acd' : %d\n", browse(&a, "acd"));
|
||||||
printf("browse 'ad' : %d ; finalState = %d\n", browse(&a, "ad"), a.dCurrent);
|
printf("* final_state: %d\n", a.dCurrent);
|
||||||
printf("browse 'acccd' : %d ; finalState = %d\n", browse(&a, "acccd"), a.dCurrent);
|
printf("browse 'ad' : %d\n", browse(&a, "ad"));
|
||||||
printf("browse 'aaaacccd' : %d ; finalState = %d\n", browse(&a, "aaaacccd"), a.dCurrent);
|
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 */
|
/* Build RegExp */
|
||||||
// char regex[40] = "(a([abc](b+a)(b+e)+ahd)*(b+i)c)sd";
|
// char regex[40] = "(a([abc](b+a)(b+e)+ahd)*(b+i)c)sd";
|
||||||
|
|
Loading…
Reference in New Issue