/* A recursive-descent parser generated by peg 0.1.18 */ #include #include #include #define YYRULECOUNT 15 #line 1 "parse.leg" #include #include #include #include "inputBuffer.c" ; enum op { String, Query, Star, Plus, Or, And, Class, Dot, Exc, Id } ; typedef union Node Node; typedef struct Symbol Symbol; typedef struct Array Array; typedef struct SymbolTable SymbolTable; struct Array { Node **elements; int length; }; struct String { enum op type; char *string; int len; }; struct Query { enum op type; Node *children[1]; }; struct Star { enum op type; Node *children[1]; }; struct Plus { enum op type; Node *children[1]; }; struct Or { enum op type; Node *children[2]; }; struct And { enum op type; Node *children[2]; }; struct Class { enum op type; char *stringValue; int len; }; struct Dot { enum op type; }; struct Exc { enum op type; Node *children[1]; }; struct Id { enum op type; Symbol *symbol; }; union Node { enum op type; struct String String; struct Query Query; struct Star Star; struct Plus Plus; struct Or Or; struct And And; struct Class Class; struct Dot Dot; struct Exc Exc; struct Id Id; }; struct Symbol{ char* name; Node *rule; }; struct SymbolTable { Symbol **elements; int length; }; #define SymbolTable_initialiser {0,0} SymbolTable symbolTable= SymbolTable_initialiser; Symbol *createSymbol(char *name) { Symbol *symbol= calloc(1, sizeof(Symbol)); symbol->name= strdup(name); return symbol; } #define new(type) mkNode(sizeof(struct type),type) Node *mkNode(size_t size,enum op type) { Node *node= calloc(1, size); node->type= type; return node; } Node *mkString(char *s) { Node *node= new(String); node->String.string= strdup(s); node->String.len=strlen(s); return node; } Node *mkId(Symbol *s){ Node *node= new(Id); node->Id.symbol=s; return node; } Node *mkQuery(Node *n) { Node *node= new(Query); node->Query.children[0]= n; return node; } Node *mkOr(Node *node1, Node *node2) { Node *node= new(Or); node->Or.children[0]= node1; node->Or.children[1]= node2; return node; } Node *mkAnd(Node *node1, Node *node2) { Node *node= new(And); node->And.children[0]= node1; node->And.children[1]= node2; return node; } Node *mkStar(Node *n) { Node *node= new(Star); node->Star.children[0]= n; return node; } Node *mkClass(char* str) { Node *node= new(Class); node->Class.stringValue= str; node->Class.len=strlen(str); return node; } Node *mkPlus(Node *n) { Node *node= new(Plus); node->Plus.children[0]= n; return node; } Node *mkDot() { Node *node= new(Dot); return node; } Node *mkExc(Node *n) { Node *node= new(Exc); node->Exc.children[0]= n; return node; } Node *_checktype(Node *object, enum op type) { if (object->type == type) return object; fprintf(stderr, "\naccesing type %i as if it were a %i\n", object->type, type); exit(1); return 0; } #define get(object, type, member) (_checktype(object, type)->type.member) void print(Node *node) { switch (node->type) { case String: printf("\"%s\"", node->String.string); return; case Query: print(node->Query.children[0]); printf("?"); return; case Star: print(node->Query.children[0]); printf("*"); return; case Plus: print(node->Query.children[0]); return; case Or: print(node->Or.children[0]); printf("Or"); print(node->Or.children[1]); return; case And: print(node->And.children[0]); printf("And"); print(node->And.children[1]); return; case Class: printf("Class"); printf("\"%s\"", node->Class.stringValue); return; case Dot: printf("Dot"); return; case Exc: printf("!"); print(node->Exc.children[0]); return; case Id: printf("%s\n",get(node,Id,symbol)->name); return; } abort(); } void println(Node *node) { print(node); printf("\n"); } InputBuffer *inputBuffer=0; #define YY_INPUT(buff,result,maxSize) \ {if (atEnd(inputBuffer)){ \ result=0; \ } \ else { \ *buff=currentChar(inputBuffer); \ advance(inputBuffer,1); \ result=1; \ }} Symbol *intern(char *name){ int left=0,right=symbolTable.length-1; while(left<=right){ int middle=(left+right)/2; int comp=strcmp(name,symbolTable.elements[middle]->name); if(comp<0){ right=middle-1; } else if(comp>0){ left=middle+1; } else{ return symbolTable.elements[middle]; } } symbolTable.elements= realloc(symbolTable.elements,sizeof(symbolTable.elements[0]) * (symbolTable.length+1)); memmove(symbolTable.elements+left+1,symbolTable.elements+left,(symbolTable.length-left)*sizeof(symbolTable.elements[0])); symbolTable.length++; return symbolTable.elements[left]=createSymbol(name); } int binary_search(SymbolTable *st, char *c){ int start=0,end=st->length-1; int m=0; while(end-start>1){ m=(end+start)/2; //printf("char : %s\n %s",c,st->elements[m]->name); // printf("start : %i End : %i\n Compare : %i\n",start,end,strcmp(c,st->elements[m]->name));/**/ if(strcmp(c,st->elements[m]->name)==0){ return m; } else if(strcmp(c,st->elements[m]->name)<0){ end=m-1; } else{ start=m+1; } m=(end+start)/2; } // printf("Start : %i, m : %i, end : %i char : %s size :%i\n",start,m,end,c,st->length); if(strcmp(c,st->elements[m]->name)<0){ return start; } else{ return end; } } void addSymbol(SymbolTable *symbolTable, Symbol *symbol) { if(symbolTable->length==0){ symbolTable->length++; symbolTable->elements= realloc(symbolTable->elements,sizeof(Symbol) * symbolTable->length); symbolTable->elements[0]=symbol;//printf("indice de %s = %i\n",symbolTable->elements[0]->name,0); return; } else{ symbolTable->length++; symbolTable->elements= realloc(symbolTable->elements,sizeof(Symbol*) * symbolTable->length); int indice=binary_search(symbolTable, symbol->name); for(int k=symbolTable->length-1;k>indice;k--){ symbolTable->elements[k]=symbolTable->elements[k-1]; } symbolTable->elements[indice]=symbol;//printf("indice de %s = %i\n",symbolTable->elements[indice]->name,indice); } } void setRule(SymbolTable *symbolTable, char *name, Node *rule) { for (int i= 0; i < symbolTable->length;i++) { if (strcmp(symbolTable->elements[i]->name, name) == 0) { symbolTable->elements[i]->rule= rule; break; } } } void arrayAppend(Array *array, Node *statement) { array->length++; array->elements= realloc(array->elements,sizeof(Node*) * array->length); array->elements[array->length-1] = statement; } #define YYSTYPE Node * YYSTYPE yylval = 0; #ifndef YY_MALLOC #define YY_MALLOC(C, N) malloc(N) #endif #ifndef YY_REALLOC #define YY_REALLOC(C, P, N) realloc(P, N) #endif #ifndef YY_FREE #define YY_FREE(C, P) free(P) #endif #ifndef YY_LOCAL #define YY_LOCAL(T) static T #endif #ifndef YY_ACTION #define YY_ACTION(T) static T #endif #ifndef YY_RULE #define YY_RULE(T) static T #endif #ifndef YY_PARSE #define YY_PARSE(T) T #endif #ifndef YYPARSE #define YYPARSE yyparse #endif #ifndef YYPARSEFROM #define YYPARSEFROM yyparsefrom #endif #ifndef YYRELEASE #define YYRELEASE yyrelease #endif #ifndef YY_BEGIN #define YY_BEGIN ( yy->__begin= yy->__pos, 1) #endif #ifndef YY_END #define YY_END ( yy->__end= yy->__pos, 1) #endif #ifdef YY_DEBUG # define yyprintf(args) fprintf args #else # define yyprintf(args) #endif #ifndef YYSTYPE #define YYSTYPE int #endif #ifndef YY_STACK_SIZE #define YY_STACK_SIZE 128 #endif #ifndef YY_BUFFER_SIZE #define YY_BUFFER_SIZE 1024 #endif #ifndef YY_PART typedef struct _yycontext yycontext; typedef void (*yyaction)(yycontext *yy, char *yytext, int yyleng); typedef struct _yythunk { int begin, end; yyaction action; struct _yythunk *next; } yythunk; struct _yycontext { char *__buf; int __buflen; int __pos; int __limit; char *__text; int __textlen; int __begin; int __end; int __textmax; yythunk *__thunks; int __thunkslen; int __thunkpos; YYSTYPE __; YYSTYPE *__val; YYSTYPE *__vals; int __valslen; #ifdef YY_CTX_MEMBERS YY_CTX_MEMBERS #endif }; #ifdef YY_CTX_LOCAL #define YY_CTX_PARAM_ yycontext *yyctx, #define YY_CTX_PARAM yycontext *yyctx #define YY_CTX_ARG_ yyctx, #define YY_CTX_ARG yyctx #ifndef YY_INPUT #define YY_INPUT(yy, buf, result, max_size) \ { \ int yyc= getchar(); \ result= (EOF == yyc) ? 0 : (*(buf)= yyc, 1); \ yyprintf((stderr, "<%c>", yyc)); \ } #endif #else #define YY_CTX_PARAM_ #define YY_CTX_PARAM #define YY_CTX_ARG_ #define YY_CTX_ARG yycontext _yyctx= { 0, 0 }; yycontext *yyctx= &_yyctx; #ifndef YY_INPUT #define YY_INPUT(buf, result, max_size) \ { \ int yyc= getchar(); \ result= (EOF == yyc) ? 0 : (*(buf)= yyc, 1); \ yyprintf((stderr, "<%c>", yyc)); \ } #endif #endif YY_LOCAL(int) yyrefill(yycontext *yy) { int yyn; while (yy->__buflen - yy->__pos < 512) { yy->__buflen *= 2; yy->__buf= (char *)YY_REALLOC(yy, yy->__buf, yy->__buflen); } #ifdef YY_CTX_LOCAL YY_INPUT(yy, (yy->__buf + yy->__pos), yyn, (yy->__buflen - yy->__pos)); #else YY_INPUT((yy->__buf + yy->__pos), yyn, (yy->__buflen - yy->__pos)); #endif if (!yyn) return 0; yy->__limit += yyn; return 1; } YY_LOCAL(int) yymatchDot(yycontext *yy) { if (yy->__pos >= yy->__limit && !yyrefill(yy)) return 0; ++yy->__pos; return 1; } YY_LOCAL(int) yymatchChar(yycontext *yy, int c) { if (yy->__pos >= yy->__limit && !yyrefill(yy)) return 0; if ((unsigned char)yy->__buf[yy->__pos] == c) { ++yy->__pos; yyprintf((stderr, " ok yymatchChar(yy, %c) @ %s\n", c, yy->__buf+yy->__pos)); return 1; } yyprintf((stderr, " fail yymatchChar(yy, %c) @ %s\n", c, yy->__buf+yy->__pos)); return 0; } YY_LOCAL(int) yymatchString(yycontext *yy, const char *s) { int yysav= yy->__pos; while (*s) { if (yy->__pos >= yy->__limit && !yyrefill(yy)) return 0; if (yy->__buf[yy->__pos] != *s) { yy->__pos= yysav; return 0; } ++s; ++yy->__pos; } return 1; } YY_LOCAL(int) yymatchClass(yycontext *yy, unsigned char *bits) { int c; if (yy->__pos >= yy->__limit && !yyrefill(yy)) return 0; c= (unsigned char)yy->__buf[yy->__pos]; if (bits[c >> 3] & (1 << (c & 7))) { ++yy->__pos; yyprintf((stderr, " ok yymatchClass @ %s\n", yy->__buf+yy->__pos)); return 1; } yyprintf((stderr, " fail yymatchClass @ %s\n", yy->__buf+yy->__pos)); return 0; } YY_LOCAL(void) yyDo(yycontext *yy, yyaction action, int begin, int end) { while (yy->__thunkpos >= yy->__thunkslen) { yy->__thunkslen *= 2; yy->__thunks= (yythunk *)YY_REALLOC(yy, yy->__thunks, sizeof(yythunk) * yy->__thunkslen); } yy->__thunks[yy->__thunkpos].begin= begin; yy->__thunks[yy->__thunkpos].end= end; yy->__thunks[yy->__thunkpos].action= action; ++yy->__thunkpos; } YY_LOCAL(int) yyText(yycontext *yy, int begin, int end) { int yyleng= end - begin; if (yyleng <= 0) yyleng= 0; else { while (yy->__textlen < (yyleng + 1)) { yy->__textlen *= 2; yy->__text= (char *)YY_REALLOC(yy, yy->__text, yy->__textlen); } memcpy(yy->__text, yy->__buf + begin, yyleng); } yy->__text[yyleng]= '\0'; return yyleng; } YY_LOCAL(void) yyDone(yycontext *yy) { int pos; for (pos= 0; pos < yy->__thunkpos; ++pos) { yythunk *thunk= &yy->__thunks[pos]; int yyleng= thunk->end ? yyText(yy, thunk->begin, thunk->end) : thunk->begin; yyprintf((stderr, "DO [%d] %p %s\n", pos, thunk->action, yy->__text)); thunk->action(yy, yy->__text, yyleng); } yy->__thunkpos= 0; } YY_LOCAL(void) yyCommit(yycontext *yy) { if ((yy->__limit -= yy->__pos)) { memmove(yy->__buf, yy->__buf + yy->__pos, yy->__limit); } yy->__begin -= yy->__pos; yy->__end -= yy->__pos; yy->__pos= yy->__thunkpos= 0; } YY_LOCAL(int) yyAccept(yycontext *yy, int tp0) { if (tp0) { fprintf(stderr, "accept denied at %d\n", tp0); return 0; } else { yyDone(yy); yyCommit(yy); } return 1; } YY_LOCAL(void) yyPush(yycontext *yy, char *text, int count) { yy->__val += count; while (yy->__valslen <= yy->__val - yy->__vals) { long offset= yy->__val - yy->__vals; yy->__valslen *= 2; yy->__vals= (YYSTYPE *)YY_REALLOC(yy, yy->__vals, sizeof(YYSTYPE) * yy->__valslen); yy->__val= yy->__vals + offset; } } YY_LOCAL(void) yyPop(yycontext *yy, char *text, int count) { yy->__val -= count; } YY_LOCAL(void) yySet(yycontext *yy, char *text, int count) { yy->__val[count]= yy->__; } #endif /* YY_PART */ #define YYACCEPT yyAccept(yy, yythunkpos0) YY_RULE(int) yy_space(yycontext *yy); /* 15 */ YY_RULE(int) yy_rule(yycontext *yy); /* 14 */ YY_RULE(int) yy_dot(yycontext *yy); /* 13 */ YY_RULE(int) yy_class(yycontext *yy); /* 12 */ YY_RULE(int) yy_string(yycontext *yy); /* 11 */ YY_RULE(int) yy_atom(yycontext *yy); /* 10 */ YY_RULE(int) yy_postfix(yycontext *yy); /* 9 */ YY_RULE(int) yy_prefix(yycontext *yy); /* 8 */ YY_RULE(int) yy_and(yycontext *yy); /* 7 */ YY_RULE(int) yy_or(yycontext *yy); /* 6 */ YY_RULE(int) yy_expression(yycontext *yy); /* 5 */ YY_RULE(int) yy__(yycontext *yy); /* 4 */ YY_RULE(int) yy_id(yycontext *yy); /* 3 */ YY_RULE(int) yy_declaration(yycontext *yy); /* 2 */ YY_RULE(int) yy_start(yycontext *yy); /* 1 */ YY_ACTION(void) yy_1_dot(yycontext *yy, char *yytext, int yyleng) { #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_dot\n")); { #line 355 __=mkDot(); } #undef yythunkpos #undef yypos #undef yy } YY_ACTION(void) yy_1_class(yycontext *yy, char *yytext, int yyleng) { #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_class\n")); { #line 353 __=mkClass(yytext) ; } #undef yythunkpos #undef yypos #undef yy } YY_ACTION(void) yy_1_string(yycontext *yy, char *yytext, int yyleng) { #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_string\n")); { #line 351 __ = mkString(yytext) ; } #undef yythunkpos #undef yypos #undef yy } YY_ACTION(void) yy_1_id(yycontext *yy, char *yytext, int yyleng) { #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_id\n")); { #line 349 __ = mkId(intern(yytext)) ; } #undef yythunkpos #undef yypos #undef yy } YY_ACTION(void) yy_1_rule(yycontext *yy, char *yytext, int yyleng) { #define i yy->__val[-1] #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_rule\n")); { #line 347 __ = mkId(intern(yytext)) ; } #undef yythunkpos #undef yypos #undef yy #undef i } YY_ACTION(void) yy_4_postfix(yycontext *yy, char *yytext, int yyleng) { #define s yy->__val[-1] #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_4_postfix\n")); { #line 343 __ = s; } #undef yythunkpos #undef yypos #undef yy #undef s } YY_ACTION(void) yy_3_postfix(yycontext *yy, char *yytext, int yyleng) { #define s yy->__val[-1] #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_3_postfix\n")); { #line 342 s = mkPlus(s) ; } #undef yythunkpos #undef yypos #undef yy #undef s } YY_ACTION(void) yy_2_postfix(yycontext *yy, char *yytext, int yyleng) { #define s yy->__val[-1] #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_2_postfix\n")); { #line 341 s = mkStar(s) ; } #undef yythunkpos #undef yypos #undef yy #undef s } YY_ACTION(void) yy_1_postfix(yycontext *yy, char *yytext, int yyleng) { #define s yy->__val[-1] #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_postfix\n")); { #line 340 s = mkQuery(s) ; } #undef yythunkpos #undef yypos #undef yy #undef s } YY_ACTION(void) yy_2_prefix(yycontext *yy, char *yytext, int yyleng) { #define p yy->__val[-1] #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_2_prefix\n")); { #line 338 __ = p; } #undef yythunkpos #undef yypos #undef yy #undef p } YY_ACTION(void) yy_1_prefix(yycontext *yy, char *yytext, int yyleng) { #define p yy->__val[-1] #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_prefix\n")); { #line 337 __ = mkExc(p); } #undef yythunkpos #undef yypos #undef yy #undef p } YY_ACTION(void) yy_2_and(yycontext *yy, char *yytext, int yyleng) { #define a yy->__val[-1] #define p yy->__val[-2] #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_2_and\n")); { #line 335 __ = p ; } #undef yythunkpos #undef yypos #undef yy #undef a #undef p } YY_ACTION(void) yy_1_and(yycontext *yy, char *yytext, int yyleng) { #define a yy->__val[-1] #define p yy->__val[-2] #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_and\n")); { #line 334 __ = mkAnd(p, a); ; } #undef yythunkpos #undef yypos #undef yy #undef a #undef p } YY_ACTION(void) yy_2_or(yycontext *yy, char *yytext, int yyleng) { #define o yy->__val[-1] #define a yy->__val[-2] #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_2_or\n")); { #line 332 __ = a ; } #undef yythunkpos #undef yypos #undef yy #undef o #undef a } YY_ACTION(void) yy_1_or(yycontext *yy, char *yytext, int yyleng) { #define o yy->__val[-1] #define a yy->__val[-2] #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_or\n")); { #line 331 __ = mkOr(o, a) ; } #undef yythunkpos #undef yypos #undef yy #undef o #undef a } YY_ACTION(void) yy_1_declaration(yycontext *yy, char *yytext, int yyleng) { #define e yy->__val[-1] #define i yy->__val[-2] #define __ yy->__ #define yypos yy->__pos #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_declaration\n")); { #line 329 setRule(&symbolTable,get(i, Id, symbol)->name, e) ; } #undef yythunkpos #undef yypos #undef yy #undef e #undef i } YY_RULE(int) yy_space(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyprintf((stderr, "%s\n", "space")); { int yypos2= yy->__pos, yythunkpos2= yy->__thunkpos; if (!yymatchClass(yy, (unsigned char *)"\000\002\000\000\001\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l3; goto l2; l3:; yy->__pos= yypos2; yy->__thunkpos= yythunkpos2; if (!yymatchChar(yy, '\n')) goto l4; l5:; { int yypos6= yy->__pos, yythunkpos6= yy->__thunkpos; if (!yymatchChar(yy, '\r')) goto l6; goto l5; l6:; yy->__pos= yypos6; yy->__thunkpos= yythunkpos6; } goto l2; l4:; yy->__pos= yypos2; yy->__thunkpos= yythunkpos2; if (!yymatchChar(yy, '\r')) goto l1; l7:; { int yypos8= yy->__pos, yythunkpos8= yy->__thunkpos; if (!yymatchChar(yy, '\n')) goto l8; goto l7; l8:; yy->__pos= yypos8; yy->__thunkpos= yythunkpos8; } } l2:; yyprintf((stderr, " ok %s @ %s\n", "space", yy->__buf+yy->__pos)); return 1; l1:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; yyprintf((stderr, " fail %s @ %s\n", "space", yy->__buf+yy->__pos)); return 0; } YY_RULE(int) yy_rule(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyDo(yy, yyPush, 1, 0); yyprintf((stderr, "%s\n", "rule")); if (!yy_id(yy)) goto l9; yyDo(yy, yySet, -1, 0); { int yypos10= yy->__pos, yythunkpos10= yy->__thunkpos; if (!yymatchChar(yy, '=')) goto l10; goto l9; l10:; yy->__pos= yypos10; yy->__thunkpos= yythunkpos10; } yyDo(yy, yy_1_rule, yy->__begin, yy->__end); yyprintf((stderr, " ok %s @ %s\n", "rule", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 1, 0); return 1; l9:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; yyprintf((stderr, " fail %s @ %s\n", "rule", yy->__buf+yy->__pos)); return 0; } YY_RULE(int) yy_dot(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyprintf((stderr, "%s\n", "dot")); if (!yy__(yy)) goto l11; if (!yymatchChar(yy, '.')) goto l11; if (!yy__(yy)) goto l11; yyDo(yy, yy_1_dot, yy->__begin, yy->__end); if (!yy__(yy)) goto l11; yyprintf((stderr, " ok %s @ %s\n", "dot", yy->__buf+yy->__pos)); return 1; l11:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; yyprintf((stderr, " fail %s @ %s\n", "dot", yy->__buf+yy->__pos)); return 0; } YY_RULE(int) yy_class(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyprintf((stderr, "%s\n", "class")); if (!yymatchChar(yy, '[')) goto l12; if (!yy__(yy)) goto l12; yyText(yy, yy->__begin, yy->__end); { #define yytext yy->__text #define yyleng yy->__textlen if (!(YY_BEGIN)) goto l12; #undef yytext #undef yyleng } l13:; { int yypos14= yy->__pos, yythunkpos14= yy->__thunkpos; { int yypos15= yy->__pos, yythunkpos15= yy->__thunkpos; if (!yymatchChar(yy, ']')) goto l15; goto l14; l15:; yy->__pos= yypos15; yy->__thunkpos= yythunkpos15; } if (!yy_string(yy)) goto l14; goto l13; l14:; yy->__pos= yypos14; yy->__thunkpos= yythunkpos14; } yyText(yy, yy->__begin, yy->__end); { #define yytext yy->__text #define yyleng yy->__textlen if (!(YY_END)) goto l12; #undef yytext #undef yyleng } if (!yymatchChar(yy, ']')) goto l12; yyDo(yy, yy_1_class, yy->__begin, yy->__end); if (!yy__(yy)) goto l12; yyprintf((stderr, " ok %s @ %s\n", "class", yy->__buf+yy->__pos)); return 1; l12:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; yyprintf((stderr, " fail %s @ %s\n", "class", yy->__buf+yy->__pos)); return 0; } YY_RULE(int) yy_string(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyprintf((stderr, "%s\n", "string")); if (!yymatchChar(yy, '"')) goto l16; yyText(yy, yy->__begin, yy->__end); { #define yytext yy->__text #define yyleng yy->__textlen if (!(YY_BEGIN)) goto l16; #undef yytext #undef yyleng } l17:; { int yypos18= yy->__pos, yythunkpos18= yy->__thunkpos; if (!yymatchClass(yy, (unsigned char *)"\377\377\377\377\373\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377")) goto l18; goto l17; l18:; yy->__pos= yypos18; yy->__thunkpos= yythunkpos18; } yyText(yy, yy->__begin, yy->__end); { #define yytext yy->__text #define yyleng yy->__textlen if (!(YY_END)) goto l16; #undef yytext #undef yyleng } if (!yymatchChar(yy, '"')) goto l16; yyDo(yy, yy_1_string, yy->__begin, yy->__end); if (!yy__(yy)) goto l16; yyprintf((stderr, " ok %s @ %s\n", "string", yy->__buf+yy->__pos)); return 1; l16:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; yyprintf((stderr, " fail %s @ %s\n", "string", yy->__buf+yy->__pos)); return 0; } YY_RULE(int) yy_atom(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyprintf((stderr, "%s\n", "atom")); { int yypos20= yy->__pos, yythunkpos20= yy->__thunkpos; if (!yy_string(yy)) goto l21; goto l20; l21:; yy->__pos= yypos20; yy->__thunkpos= yythunkpos20; if (!yy_class(yy)) goto l22; goto l20; l22:; yy->__pos= yypos20; yy->__thunkpos= yythunkpos20; if (!yy_dot(yy)) goto l23; goto l20; l23:; yy->__pos= yypos20; yy->__thunkpos= yythunkpos20; if (!yy_rule(yy)) goto l19; } l20:; yyprintf((stderr, " ok %s @ %s\n", "atom", yy->__buf+yy->__pos)); return 1; l19:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; yyprintf((stderr, " fail %s @ %s\n", "atom", yy->__buf+yy->__pos)); return 0; } YY_RULE(int) yy_postfix(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyDo(yy, yyPush, 1, 0); yyprintf((stderr, "%s\n", "postfix")); if (!yy_atom(yy)) goto l24; yyDo(yy, yySet, -1, 0); { int yypos25= yy->__pos, yythunkpos25= yy->__thunkpos; { int yypos27= yy->__pos, yythunkpos27= yy->__thunkpos; if (!yymatchChar(yy, '?')) goto l28; if (!yy__(yy)) goto l28; yyDo(yy, yy_1_postfix, yy->__begin, yy->__end); goto l27; l28:; yy->__pos= yypos27; yy->__thunkpos= yythunkpos27; if (!yymatchChar(yy, '*')) goto l29; if (!yy__(yy)) goto l29; yyDo(yy, yy_2_postfix, yy->__begin, yy->__end); goto l27; l29:; yy->__pos= yypos27; yy->__thunkpos= yythunkpos27; if (!yymatchChar(yy, '+')) goto l25; if (!yy__(yy)) goto l25; yyDo(yy, yy_3_postfix, yy->__begin, yy->__end); } l27:; goto l26; l25:; yy->__pos= yypos25; yy->__thunkpos= yythunkpos25; } l26:; yyDo(yy, yy_4_postfix, yy->__begin, yy->__end); yyprintf((stderr, " ok %s @ %s\n", "postfix", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 1, 0); return 1; l24:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; yyprintf((stderr, " fail %s @ %s\n", "postfix", yy->__buf+yy->__pos)); return 0; } YY_RULE(int) yy_prefix(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyDo(yy, yyPush, 1, 0); yyprintf((stderr, "%s\n", "prefix")); { int yypos31= yy->__pos, yythunkpos31= yy->__thunkpos; if (!yymatchChar(yy, '!')) goto l32; if (!yy__(yy)) goto l32; if (!yy_postfix(yy)) goto l32; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_prefix, yy->__begin, yy->__end); goto l31; l32:; yy->__pos= yypos31; yy->__thunkpos= yythunkpos31; if (!yy_postfix(yy)) goto l30; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_2_prefix, yy->__begin, yy->__end); } l31:; yyprintf((stderr, " ok %s @ %s\n", "prefix", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 1, 0); return 1; l30:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; yyprintf((stderr, " fail %s @ %s\n", "prefix", yy->__buf+yy->__pos)); return 0; } YY_RULE(int) yy_and(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyDo(yy, yyPush, 2, 0); yyprintf((stderr, "%s\n", "and")); { int yypos34= yy->__pos, yythunkpos34= yy->__thunkpos; if (!yy_prefix(yy)) goto l35; yyDo(yy, yySet, -2, 0); if (!yy_and(yy)) goto l35; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_and, yy->__begin, yy->__end); goto l34; l35:; yy->__pos= yypos34; yy->__thunkpos= yythunkpos34; if (!yy_prefix(yy)) goto l33; yyDo(yy, yySet, -2, 0); yyDo(yy, yy_2_and, yy->__begin, yy->__end); } l34:; yyprintf((stderr, " ok %s @ %s\n", "and", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 2, 0); return 1; l33:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; yyprintf((stderr, " fail %s @ %s\n", "and", yy->__buf+yy->__pos)); return 0; } YY_RULE(int) yy_or(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyDo(yy, yyPush, 2, 0); yyprintf((stderr, "%s\n", "or")); { int yypos37= yy->__pos, yythunkpos37= yy->__thunkpos; if (!yy_and(yy)) goto l38; yyDo(yy, yySet, -2, 0); if (!yy__(yy)) goto l38; if (!yymatchChar(yy, '|')) goto l38; if (!yy__(yy)) goto l38; if (!yy_or(yy)) goto l38; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_or, yy->__begin, yy->__end); goto l37; l38:; yy->__pos= yypos37; yy->__thunkpos= yythunkpos37; if (!yy_and(yy)) goto l36; yyDo(yy, yySet, -2, 0); yyDo(yy, yy_2_or, yy->__begin, yy->__end); } l37:; yyprintf((stderr, " ok %s @ %s\n", "or", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 2, 0); return 1; l36:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; yyprintf((stderr, " fail %s @ %s\n", "or", yy->__buf+yy->__pos)); return 0; } YY_RULE(int) yy__(yycontext *yy) { yyprintf((stderr, "%s\n", "_")); l40:; { int yypos41= yy->__pos, yythunkpos41= yy->__thunkpos; if (!yy_space(yy)) goto l41; goto l40; l41:; yy->__pos= yypos41; yy->__thunkpos= yythunkpos41; } yyprintf((stderr, " ok %s @ %s\n", "_", yy->__buf+yy->__pos)); return 1; } YY_RULE(int) yy_id(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyprintf((stderr, "%s\n", "id")); yyText(yy, yy->__begin, yy->__end); { #define yytext yy->__text #define yyleng yy->__textlen if (!(YY_BEGIN)) goto l42; #undef yytext #undef yyleng } if (!yymatchClass(yy, (unsigned char *)"\000\000\000\000\000\000\000\000\376\377\377\377\377\377\377\007\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l42; l43:; { int yypos44= yy->__pos, yythunkpos44= yy->__thunkpos; if (!yymatchClass(yy, (unsigned char *)"\000\000\000\000\000\000\377\003\376\377\377\377\377\377\377\007\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l44; goto l43; l44:; yy->__pos= yypos44; yy->__thunkpos= yythunkpos44; } yyText(yy, yy->__begin, yy->__end); { #define yytext yy->__text #define yyleng yy->__textlen if (!(YY_END)) goto l42; #undef yytext #undef yyleng } if (!yy__(yy)) goto l42; yyDo(yy, yy_1_id, yy->__begin, yy->__end); yyprintf((stderr, " ok %s @ %s\n", "id", yy->__buf+yy->__pos)); return 1; l42:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; yyprintf((stderr, " fail %s @ %s\n", "id", yy->__buf+yy->__pos)); return 0; } YY_RULE(int) yy_declaration(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyDo(yy, yyPush, 2, 0); yyprintf((stderr, "%s\n", "declaration")); if (!yy_id(yy)) goto l45; yyDo(yy, yySet, -2, 0); if (!yymatchChar(yy, '=')) goto l45; if (!yy__(yy)) goto l45; if (!yy_expression(yy)) goto l45; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_declaration, yy->__begin, yy->__end); yyprintf((stderr, " ok %s @ %s\n", "declaration", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 2, 0); return 1; l45:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; yyprintf((stderr, " fail %s @ %s\n", "declaration", yy->__buf+yy->__pos)); return 0; } YY_RULE(int) yy_start(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyprintf((stderr, "%s\n", "start")); if (!yy_declaration(yy)) goto l46; l47:; { int yypos48= yy->__pos, yythunkpos48= yy->__thunkpos; if (!yy_declaration(yy)) goto l48; goto l47; l48:; yy->__pos= yypos48; yy->__thunkpos= yythunkpos48; } yyprintf((stderr, " ok %s @ %s\n", "start", yy->__buf+yy->__pos)); return 1; l46:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; yyprintf((stderr, " fail %s @ %s\n", "start", yy->__buf+yy->__pos)); return 0; } #ifndef YY_PART typedef int (*yyrule)(yycontext *yy); YY_PARSE(int) YYPARSEFROM(YY_CTX_PARAM_ yyrule yystart) { int yyok; if (!yyctx->__buflen) { yyctx->__buflen= YY_BUFFER_SIZE; yyctx->__buf= (char *)YY_MALLOC(yyctx, yyctx->__buflen); yyctx->__textlen= YY_BUFFER_SIZE; yyctx->__text= (char *)YY_MALLOC(yyctx, yyctx->__textlen); yyctx->__thunkslen= YY_STACK_SIZE; yyctx->__thunks= (yythunk *)YY_MALLOC(yyctx, sizeof(yythunk) * yyctx->__thunkslen); yyctx->__valslen= YY_STACK_SIZE; yyctx->__vals= (YYSTYPE *)YY_MALLOC(yyctx, sizeof(YYSTYPE) * yyctx->__valslen); yyctx->__begin= yyctx->__end= yyctx->__pos= yyctx->__limit= yyctx->__thunkpos= 0; } yyctx->__begin= yyctx->__end= yyctx->__pos; yyctx->__thunkpos= 0; yyctx->__val= yyctx->__vals; yyok= yystart(yyctx); if (yyok) yyDone(yyctx); yyCommit(yyctx); return yyok; } YY_PARSE(int) YYPARSE(YY_CTX_PARAM) { return YYPARSEFROM(YY_CTX_ARG_ yy_start); } YY_PARSE(yycontext *) YYRELEASE(yycontext *yyctx) { if (yyctx->__buflen) { yyctx->__buflen= 0; YY_FREE(yyctx, yyctx->__buf); YY_FREE(yyctx, yyctx->__text); YY_FREE(yyctx, yyctx->__thunks); YY_FREE(yyctx, yyctx->__vals); } return yyctx; } #endif #line 362 "parse.leg" int execute(Node *node, InputBuffer *in) { switch (node->type) { case String: { if (strncmp(currentText(in), get(node,String,string), get(node,String,len))) { return 0; } advance(in, get(node,String,len)); return 1; } case And: { int pos= getPosition(in); if (!execute(get(node,And,children[0]), in)) //si il y a eu une erreur { return 0; } //si ça s'est bien passé if (!execute(get(node,And,children[1]), in)) { setPosition(in, pos); return 0; } return 1; } case Or: { if (execute(get(node,Or,children[0]), in)) { return 1; } return execute(get(node,Or,children[1]), in); } case Star: { while (execute(get(node,Star,children[0]), in)); return 1; } case Plus: { if (!execute(get(node,Plus,children[0]), in)) { return 0; } while (execute(get(node,Plus,children[0]), in)); return 1; } case Class: { if (!currentChar(in)) { return 0; } if (strchr(get(node,Class,stringValue), currentChar(in))) { advance(in, 1); return 1; } return 0; } case Query: { execute(get(node,Query,children[0]), in); return 1; } case Exc: { int pos= getPosition(in); if (!execute(get(node,Exc,children[0]), in)) { return 1; } setPosition(in, pos); return 0; } case Dot: { if (atEnd(in)) { return 0; } advance(in, 1); return 1; } case Id: { Symbol *symbol= get(node, Id, symbol); return execute(symbol->rule, in); } } printf("this cannot happen\n"); abort(); } int main(int argc, char **argv) { if(argc<2) return 0; inputBuffer = mkInputBuffer(argv[1]); if (!yyparse()) { printf("Error\n"); return 1; } println(yylval); char *line=0; size_t line_max=0; ssize_t line_len=0; 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(yylval, 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; (void)yySet; (void)yyPop; (void)yyPush; (void)yyAccept; (void)yymatchDot; (void)yymatchString; (void)yymatchChar; }