Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
/** Important:
Dependencies: requires NFA2DFA.c (this file) and NFAparse.c
Compilation: use gcc NFA2DFA.c -o <output_file_name> -std=c99
Input: file NFA2DFA.txt is required at runtime. This file holds t
he transition table,list of input sy
mbols and list of final states in a special format.
*/
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include"NFAparse.c"
puts("\n");
printf("%d bytes read from file %s in %d reads.",nbytes,filename
,ntries);
puts("\n----------Buffer dump----------");
puts(buffer);
puts("\n----------End of dump----------");
/* Extract the l
ist of states */
transition[nStat
e-1][col] = getStateList(buffer,&i);
col++;
}
} /* End of switch() */
if(hashFlag)
{
break;
}
++i;
} /* End of while() */
while(1)
{
if( isdigit(buf[*indexPtr]) || buf[*indexPtr
] == '-' )
{
returnVector = (int*)realloc(
returnVector,sizeof(int)*(count+1) );
returnVector[count] = readNum(bu
f,indexPtr);
count++;
}
else if( buf[*indexPtr] == ' ' || buf[*in
dexPtr] == '\n' || buf[*indexPtr] == ',' )
{
(*indexPtr)--;
break;
}
#ifdef __DEBUG
printf("\n%d states are ",count-1);
for(int i =1;i<count;i++)
{
printf (" %d ",returnVector[i]);
}
printf("\n");
#endif
return returnVector;
}
}
/* Detect magnitude */
magnitude = 0;
while(array[*indexPtr] >= 48 && array[*indexPtr]<= 57)
{
magnitude = magnitude * 10 + array[*indexPtr] - 48;
(*indexPtr)++;
}
(*indexPtr)--; /* Unget last character read from buffer */
return (sign*magnitude);
}
printf("\t");
for(int k=1;k<=count;k++)
{
printf(" %d ",ta
ble[i][j][k]);
}
}
printf("\n");
}
}
/* Convert an NFA to equivalent DFA */
int*** convert(int ***table,int ***ptrStates,int nsymbol,int *ptrNstate,char *in
)
{
int i,j,k;
int *uarray ;
int a;
int b;
for(i=0;i<*ptrNstate;i++)
{
for(j=0;j<nsymbol;j++)
{
/* If a multi-state is found */
int count = table[i][j][0];
if( count > 1
)
{
if(duplicate(*pt
rStates,*ptrNstate,table[i][j])) continue;
/* Add a row to
transition table and state list */
table = (int***)
realloc( table,sizeof(int**)*(*ptrNstate + 1));
table[*ptrNstate
] = (int**)malloc(sizeof(int*)*(nsymbol));
(*ptrStates) = (
int**)realloc ( (*ptrStates),sizeof(int*)*(*ptrNstate + 1));
(*ptrStates)[*pt
rNstate] = (int*)malloc(sizeof(int)*(*ptrNstate));
/*Copy multistat
e to state list */
for(k=0;k<=nsymb
ol;k++)
{
(*ptrStates)[*ptrNstate][k] = table[i][j][k];
}
/* Compute trans
itions for new multi-state */
for(a=0;a<nsymb
ol;a++) /* for every input symbol */
{
uarray = NULL;
#ifdef __DEBUG
printf("\n---------------------------------------");
printf("\nSending array 1>>>>");
prt(uarray);
printf("\n");
printf("Sending array 2>>>> ");
prt(table[s][a]);
printf("\n");
#endif
uarray = _union(uarray,table[s][a]);
#ifdef __DEBUG
printf("Got Union array>>>> ");
prt(uarray);
printf("\n-----------------------------------------\n");
#endif
Isort(uarray);
table[*ptrNstate][a] = uarray;
}
#ifdef __DEBUG
displayTable(tab
le,in,*ptrStates,nsymbol,*ptrNstate);
printf("\nPress
any key to continue");
getchar();
#endif
(*ptrNstate)++;
} /* end of if() */
} /* end of for() */
} /* end of far */
return table;
}
else
{
result = (int*)malloc(sizeof(int)*(arr1[0] + arr
2[0] + 1 ));
result[0] = arr1[0] + arr2[0];
int i;
/* copy all elements from arr1[] */
for(i=1;i<=arr1[0];i++)
{
result[i] = arr1[i];
}
if(result[0] > 1)
{
for(int c= result[1] ;c<=result[
0];c++)
{
if(result[c] ==
-1)
{
for(int d = c;d<result[0];d++)
{
result[d] = result[d+1];
}
result[0]--;
}
}
}
}
return result;
}
/* Print a vector */
void prt(int *a)
{
if(a==NULL) {
printf(" = NULL");
return;
}
int count = a[0];
int i = 1;
while(i<=count)
{
printf(" %d ",a[i]);
i++;
}
}
for(i=1;i<=count;i++)
{
tmp = l[i];
j=i-1;
while(l[j] > tmp && j>0 )
{
j--;
}
for(k=i-1;k>j;k--)
{
l[k+1] = l[k];
}
l[j+1] = tmp;
}
}
for(i=0;i<max;i++)
{
lookfor = list[i][1]; /* The only element */
for(j=1;j<=testV[0];j++)
{
if(testV[j] == lookfor)
{
ret = 1;
break;
}
}
}
return ret;
}