|
@ -29,6 +29,8 @@ typedef union Node Node; |
|
|
typedef struct Symbol Symbol; |
|
|
typedef struct Symbol Symbol; |
|
|
typedef struct Array Array; |
|
|
typedef struct Array Array; |
|
|
typedef struct SymbolTable SymbolTable; |
|
|
typedef struct SymbolTable SymbolTable; |
|
|
|
|
|
typedef struct NodeCount NodeCount; |
|
|
|
|
|
|
|
|
struct Array { |
|
|
struct Array { |
|
|
Node **elements; |
|
|
Node **elements; |
|
|
int length; |
|
|
int length; |
|
@ -310,7 +312,7 @@ postfix = s:atom ( "?" - { s = mkQuery(s) } |
|
|
| "+" - { s = mkPlus(s) } |
|
|
| "+" - { s = mkPlus(s) } |
|
|
)? { $$ = s } |
|
|
)? { $$ = s } |
|
|
|
|
|
|
|
|
atom = string | class | dot | rule | '(' -expression')'- |
|
|
|
|
|
|
|
|
atom = string | class | dot | rule | '(' - expression ')' - |
|
|
|
|
|
|
|
|
rule = i:id !'=' { $$ = mkId(intern(yytext)) } |
|
|
rule = i:id !'=' { $$ = mkId(intern(yytext)) } |
|
|
|
|
|
|
|
@ -411,12 +413,308 @@ int execute(Node *node, InputBuffer *in) |
|
|
abort(); |
|
|
abort(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct NodeCount{ |
|
|
|
|
|
int String; |
|
|
|
|
|
int Query; |
|
|
|
|
|
int Star; |
|
|
|
|
|
int Plus; |
|
|
|
|
|
int Or; |
|
|
|
|
|
int And; |
|
|
|
|
|
int Class; |
|
|
|
|
|
int Dot; |
|
|
|
|
|
int Exc; |
|
|
|
|
|
int Et; |
|
|
|
|
|
int Id; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
NodeCount nodeCount={ |
|
|
|
|
|
.String=0, |
|
|
|
|
|
.Query=0, |
|
|
|
|
|
.Star=0, |
|
|
|
|
|
.Plus=0, |
|
|
|
|
|
.Or=0, |
|
|
|
|
|
.And=0, |
|
|
|
|
|
.Class=0, |
|
|
|
|
|
.Dot=0, |
|
|
|
|
|
.Exc=0, |
|
|
|
|
|
.Et=0, |
|
|
|
|
|
.Id=0 |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
void printVariableName(enum op type, NodeCount count, int increment){ |
|
|
|
|
|
char number[50] ; |
|
|
|
|
|
char *dest=malloc(30); |
|
|
|
|
|
size_t n; |
|
|
|
|
|
switch(type){ |
|
|
|
|
|
case String : |
|
|
|
|
|
printf("string%i",nodeCount.String); |
|
|
|
|
|
if(increment==1)nodeCount.String++; |
|
|
|
|
|
break; |
|
|
|
|
|
case Query : |
|
|
|
|
|
if(increment==1)nodeCount.Query++; |
|
|
|
|
|
number[0]=nodeCount.Query+'0'; |
|
|
|
|
|
n = strlen(number); |
|
|
|
|
|
strncpy(dest,"query",5); |
|
|
|
|
|
strncat(dest, number, n); |
|
|
|
|
|
printf("%s ",dest); |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case Star: |
|
|
|
|
|
printf("star%i",nodeCount.Star); |
|
|
|
|
|
if(increment==1) nodeCount.Star++; |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case Plus : |
|
|
|
|
|
if(increment==1) nodeCount.Plus++; |
|
|
|
|
|
number[0]=nodeCount.Plus+'0'; |
|
|
|
|
|
n = strlen(number); |
|
|
|
|
|
strncpy(dest,"plus",4); |
|
|
|
|
|
strncat(dest, number, n); |
|
|
|
|
|
printf("%s ",dest); |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case Or : |
|
|
|
|
|
if(increment==1) nodeCount.Or++; |
|
|
|
|
|
number[0]=nodeCount.Or+'0'; |
|
|
|
|
|
n = strlen(number); |
|
|
|
|
|
strncpy(dest,"or",2); |
|
|
|
|
|
strncat(dest, number, n); |
|
|
|
|
|
printf("%s ",dest); |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case And: |
|
|
|
|
|
if(increment==1) nodeCount.And++; |
|
|
|
|
|
number[0]=nodeCount.And+'0'; |
|
|
|
|
|
n = strlen(number); |
|
|
|
|
|
strncpy(dest,"and",3); |
|
|
|
|
|
strncat(dest, number, n); |
|
|
|
|
|
printf("%s ",dest); |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case Class: |
|
|
|
|
|
if(increment==1)nodeCount.Class++; |
|
|
|
|
|
number[0]=nodeCount.Class+'0'; |
|
|
|
|
|
n = strlen(number); |
|
|
|
|
|
strncpy(dest,"class",5); |
|
|
|
|
|
strncat(dest, number, n); |
|
|
|
|
|
printf("%s ",dest); |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case Dot: |
|
|
|
|
|
if(increment==1) nodeCount.Dot++; |
|
|
|
|
|
number[0]=nodeCount.Dot+'0'; |
|
|
|
|
|
n = strlen(number); |
|
|
|
|
|
strncpy(dest,"dot",3); |
|
|
|
|
|
strncat(dest, number, n); |
|
|
|
|
|
printf("%s ",dest); |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case Exc: |
|
|
|
|
|
if(increment==1) nodeCount.Exc++; |
|
|
|
|
|
number[0]=nodeCount.Exc+'0'; |
|
|
|
|
|
n = strlen(number); |
|
|
|
|
|
strncpy(dest,"exc",3); |
|
|
|
|
|
strncat(dest, number, n); |
|
|
|
|
|
printf("%s ",dest); |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case Et: |
|
|
|
|
|
if(increment==1) nodeCount.Et++; |
|
|
|
|
|
number[0]=nodeCount.Et+'0'; |
|
|
|
|
|
n = strlen(number); |
|
|
|
|
|
strncpy(dest,"et",2); |
|
|
|
|
|
strncat(dest, number, n); |
|
|
|
|
|
printf("%s ",dest); |
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
case Id: |
|
|
|
|
|
if(increment==1) nodeCount.Id++; |
|
|
|
|
|
number[0]=nodeCount.Id+'0'; |
|
|
|
|
|
n = strlen(number); |
|
|
|
|
|
strncpy(dest,"id",2); |
|
|
|
|
|
strncat(dest, number, n); |
|
|
|
|
|
printf("%s ",dest); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int nodeNumber=0; |
|
|
|
|
|
|
|
|
|
|
|
int printCode(Node *node) |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
switch (node->type) { |
|
|
|
|
|
case String:{ |
|
|
|
|
|
int thisNumber=nodeNumber++; |
|
|
|
|
|
printf("Node node%i = { .String = { String, ",thisNumber); |
|
|
|
|
|
printf("\"%s\"", node->String.string); |
|
|
|
|
|
printf(" , %i }}; \n",node->String.len); |
|
|
|
|
|
return thisNumber; |
|
|
|
|
|
} |
|
|
|
|
|
case Query: |
|
|
|
|
|
printVariableName(node->type,nodeCount,1); |
|
|
|
|
|
printf("= { .Query = { Query, &"); |
|
|
|
|
|
printVariableName(node->Query.children[0]->type,nodeCount,0); |
|
|
|
|
|
printf(" }};\n"); |
|
|
|
|
|
return 0; |
|
|
|
|
|
case Star: { |
|
|
|
|
|
int i = printCode(node->Star.children[0]); |
|
|
|
|
|
int thisNumber=nodeNumber++; |
|
|
|
|
|
printf("Node node%i",thisNumber); |
|
|
|
|
|
printf("= { .Star = { Star, &node%i }};\n",i); |
|
|
|
|
|
return thisNumber; |
|
|
|
|
|
} |
|
|
|
|
|
case Plus:{ |
|
|
|
|
|
int i = printCode(node->Star.children[0]); |
|
|
|
|
|
int thisNumber=nodeNumber++; |
|
|
|
|
|
printf("Node node%i = { .Plus = { Plus, &node%i }};\n",thisNumber,i); |
|
|
|
|
|
return thisNumber; |
|
|
|
|
|
} |
|
|
|
|
|
case Or:{ |
|
|
|
|
|
int i = printCode(node->Star.children[0]); |
|
|
|
|
|
int j = printCode(node->Star.children[1]); |
|
|
|
|
|
int thisNumber=nodeNumber++; |
|
|
|
|
|
printf("Node node%i= { .Or = { Or, &node%i",thisNumber,i); |
|
|
|
|
|
thisNumber=nodeNumber++; |
|
|
|
|
|
printf(" , &node%i }};\n",j); |
|
|
|
|
|
return thisNumber; |
|
|
|
|
|
} |
|
|
|
|
|
case And:{ |
|
|
|
|
|
int i = printCode(node->Star.children[0]); |
|
|
|
|
|
int j = printCode(node->Star.children[1]); |
|
|
|
|
|
int thisNumber=nodeNumber++; |
|
|
|
|
|
printf("Node node%i= { .And = { And, &node%i",thisNumber,i); |
|
|
|
|
|
thisNumber=nodeNumber++; |
|
|
|
|
|
printf(" , &node%i }};\n",j); |
|
|
|
|
|
return thisNumber; |
|
|
|
|
|
} |
|
|
|
|
|
case Class:{ |
|
|
|
|
|
int thisNumber=nodeNumber++; |
|
|
|
|
|
printf("Node node%i= { .Class = { Class, %s }};\n",thisNumber,node->Class.array); |
|
|
|
|
|
return thisNumber; |
|
|
|
|
|
} |
|
|
|
|
|
case Dot:{ |
|
|
|
|
|
int thisNumber=nodeNumber++; |
|
|
|
|
|
printf("Node node%i= { .Dot = { Dot}};\n",thisNumber); |
|
|
|
|
|
return thisNumber; |
|
|
|
|
|
} |
|
|
|
|
|
case Exc:{ |
|
|
|
|
|
int i = printCode(node->Star.children[0]); |
|
|
|
|
|
int thisNumber=nodeNumber++; |
|
|
|
|
|
printf("Node node%i = { .Exc = { Exc, &node%i }};\n",thisNumber,i); |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
case Et: |
|
|
|
|
|
printVariableName(node->type,nodeCount,1); |
|
|
|
|
|
printf("= { .Et = { Et, &"); |
|
|
|
|
|
printVariableName(node->Et.children[0]->type,nodeCount,0); |
|
|
|
|
|
printf(" }};\n"); |
|
|
|
|
|
return 0; |
|
|
|
|
|
case Id: |
|
|
|
|
|
printVariableName(node->type,nodeCount,1); |
|
|
|
|
|
printf("= { .Id = { Id, symb"); |
|
|
|
|
|
printf(" }};\n"); |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
abort(); |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void printlnCode(Node *node) |
|
|
|
|
|
{ |
|
|
|
|
|
printCode(node); |
|
|
|
|
|
printf("\n"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
int main(int argc, char **argv) |
|
|
int main(int argc, char **argv) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
char *opt_f=0; |
|
|
|
|
|
char *opt_c=0; |
|
|
|
|
|
char *opt__=0; |
|
|
|
|
|
for(int i=1;i<argc;i++){ |
|
|
|
|
|
char *arg=argv[i]; |
|
|
|
|
|
if(!strcmp(arg,"-f") && i<argc-1){ |
|
|
|
|
|
i++; |
|
|
|
|
|
opt_f=argv[i]; |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
if(!strcmp(arg,"-c") && i<argc-1){ |
|
|
|
|
|
i++; |
|
|
|
|
|
opt_c=argv[i]; |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
opt__=arg; |
|
|
|
|
|
} |
|
|
|
|
|
if (opt_f && opt__) fatal("file and command line expressions cannot both be supplied"); |
|
|
|
|
|
if (!opt_f && !opt__) fatal("no expression specified"); |
|
|
|
|
|
if (opt__) inputBuffer = mkInputBuffer(argv[1]); |
|
|
|
|
|
if (opt_f){ |
|
|
|
|
|
FILE *fp= fopen(argv[2], "r"); |
|
|
|
|
|
if (!fp) { |
|
|
|
|
|
perror(argv[2]); |
|
|
|
|
|
exit(1); |
|
|
|
|
|
} |
|
|
|
|
|
struct stat sb; |
|
|
|
|
|
if (fstat(fileno(fp), &sb)) { |
|
|
|
|
|
perror(argv[2]); |
|
|
|
|
|
exit(1); |
|
|
|
|
|
} |
|
|
|
|
|
char *text= malloc(sb.st_size); |
|
|
|
|
|
if (!text) { |
|
|
|
|
|
fatal("out of memory"); |
|
|
|
|
|
exit(1); |
|
|
|
|
|
} |
|
|
|
|
|
if (fread(text, sb.st_size, 1, fp) < 1) { |
|
|
|
|
|
perror(argv[2]); |
|
|
|
|
|
exit(1); |
|
|
|
|
|
} |
|
|
|
|
|
fclose(fp); |
|
|
|
|
|
inputBuffer = mkInputBuffer(text); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!yyparse()) { |
|
|
|
|
|
printf("Error\n"); |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if( opt_c){ |
|
|
|
|
|
int i=printCode(intern("start")->rule); |
|
|
|
|
|
printf("Node *start_rule= &node%i;\n",i); |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
char *line=0; |
|
|
|
|
|
size_t line_max=0; |
|
|
|
|
|
ssize_t line_len=0; |
|
|
|
|
|
Node *startRule= intern("start")->rule; |
|
|
|
|
|
|
|
|
|
|
|
if (!startRule) { |
|
|
|
|
|
fatal("no start rule"); |
|
|
|
|
|
return 1; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
while ((line_len=getline(&line,&line_max,stdin))>=0) { |
|
|
|
|
|
if (line_len>0 && line[line_len-1]=='\n') { |
|
|
|
|
|
line[line_len-1]=0; |
|
|
|
|
|
} |
|
|
|
|
|
initInputBuffer(inputBuffer,line); |
|
|
|
|
|
if (!execute(startRule, inputBuffer) || !atEnd(inputBuffer)) { |
|
|
|
|
|
printf("no match, current position : %i\n", getPosition(inputBuffer)); |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
printf("match, current position : %i\n", getPosition(inputBuffer)); |
|
|
|
|
|
} // 0 => no match, 1 => match |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
switch (argc) { |
|
|
switch (argc) { |
|
|
|
|
|
|
|
|
case 1:{ |
|
|
case 1:{ |
|
|
printf("affiche toi ho !\n"); |
|
|
|
|
|
int char_index=0; |
|
|
int char_index=0; |
|
|
char *text_file= malloc(50); |
|
|
char *text_file= malloc(50); |
|
|
int ch; |
|
|
int ch; |
|
@ -427,7 +725,7 @@ int main(int argc, char **argv) |
|
|
} |
|
|
} |
|
|
printf("%s",text_file); |
|
|
printf("%s",text_file); |
|
|
inputBuffer = mkInputBuffer(text_file); |
|
|
inputBuffer = mkInputBuffer(text_file); |
|
|
break; |
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
case 2: { |
|
|
case 2: { |
|
@ -463,8 +761,6 @@ int main(int argc, char **argv) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
default: { |
|
|
default: { |
|
|
printf("%i\n",argc); |
|
|
|
|
|
printf("test akoezpa\n"); |
|
|
|
|
|
fatal("usage: %s parsing-expression | -f filename", argv[0]); |
|
|
fatal("usage: %s parsing-expression | -f filename", argv[0]); |
|
|
exit(1); |
|
|
exit(1); |
|
|
} |
|
|
} |
|
@ -500,6 +796,8 @@ int main(int argc, char **argv) |
|
|
|
|
|
|
|
|
return 0; |
|
|
return 0; |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
(void)yySet; |
|
|
(void)yySet; |
|
|
(void)yyPop; |
|
|
(void)yyPop; |
|
|
(void)yyPush; |
|
|
(void)yyPush; |
|
|