From e81084d437f39c1f5332a26cf76247d5310dd888 Mon Sep 17 00:00:00 2001 From: lquint Date: Tue, 6 Jul 2021 13:27:18 +0200 Subject: [PATCH] leg update, inputBuffer --- inputBuffer | Bin 0 -> 8600 bytes inputBuffer.c | 63 +++++++ lexer.c | 170 ------------------- parse | Bin 0 -> 23640 bytes parse.c | 451 +++++++++++++++++++++++++++++++++++++++++--------- parse.leg | 223 +++++++++++++++++++------ 6 files changed, 607 insertions(+), 300 deletions(-) create mode 100755 inputBuffer create mode 100644 inputBuffer.c create mode 100755 parse diff --git a/inputBuffer b/inputBuffer new file mode 100755 index 0000000000000000000000000000000000000000..bcb92a38ed15095a1aa5724e2518016d53945078 GIT binary patch literal 8600 zcmeHMe{5UT6~2y}Hf>YKv;{&NR<9J+4%JOt8p29PoW$wNmbRfJEo^Py9NS4e9J{ig zOIpDeEd}O4&VA>^ z&o7Rsru{dr^!(lXogeSsbAP;hufHDdXshselq9eEyrR-jrOh<4;O-Ti0W_&bwGhA8 zt2#9w<$0R322k<$h?v9KE#9X zB+n1wo0zZi2ehT|QhvS0=1EP)2PvL+@H9>&O*(kzIxywno%3MY!7mUez~3=X9JN!@ zSqCq3i)6;|yH&)VI$t7s=q=BCoDu0BshpIC~6}vD2muL^E?|-`&8k9qn14jOV$NW_SLBz)2H9A)$VvHde*+t4-| zQN}UDbGGJbb#%PuvGJOx$BTO6ru*V7^AS~4EAOu6w8K*w6}Cr1^AEghu&2&Hp`ASbokJ zp70pA{W4;=`Z6Jy2kbYTK@B7b@|&|Ioe{c$X0f&0>#WK4gJJuAIi z52AYX2|ahIP&fsw#;e~1)&U;@-h|d)0!Dy!Xy_2|cHoo1eZXgc_XA%6J`VgR@D#8H z?LP-h0xtp20b4P3==(y+b7Z&j4EjAQs~1#_c^3GIr#;0tQIDHRfm3;XrPe@B`nAxn zCtaVv%~$({n#K234XHa;Z2j!!HLHmx`&K*w=+WSnM7I0ztVTQa+8OfsAF60sGXD@N z1fJyg$P0`f3YaP1o_R7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OCxGFXx&);OGn@_Oc%ql=B!=baKb<09ZQfh`sEa1?Br9^%p(Cdtpy!Cn?MR zS}>sBI=lW%t0TxzOvPUI;ez9=DDQW-|0vrxv)(j!kf59=$^IHVZu>{UQB3%=GqbOk z{k?pjprRAIm!ire=d_pozo5Kd3?!a{kGbsSoFFLm6?-WQKH;*L@4Vo15?FR1?-hSP zcG=7QfuNkDMc>{2r`TRzBl{b{tHhAeZT}0G{apWpiW|56GcJ2M?+8x12E4>g{GCCD z&JHw(D9Qd<{wHTH1@<^&ROz5wEW*J<5TRU zAL*PyO%!`MkDZ`OA$8g?^DFj(??B?TpV{Z~3*e%S~5+0d=;>JIfj zY>BJK<4j_|&PgIK_D+P Q%iDMHk@m7EGP>>m11q;A;s5{u literal 0 HcmV?d00001 diff --git a/inputBuffer.c b/inputBuffer.c new file mode 100644 index 0000000..d7a3cbc --- /dev/null +++ b/inputBuffer.c @@ -0,0 +1,63 @@ +#include + +typedef struct InputBuffer InputBuffer; + +struct InputBuffer +{ + int position; + char *text; +}; + +InputBuffer *initInputBuffer(InputBuffer *ib,char *text) +{ + ib->position= 0; + ib->text= text; + return ib; +} + +InputBuffer *mkInputBuffer(char *text) +{ + return initInputBuffer(calloc(1,sizeof(InputBuffer*)),text); //sizeof du type pointeur +} + +void advance(InputBuffer *ib, int add) +{ + ib->position+= add; +} + +int atEnd(InputBuffer *ib) { + return ib->text[ib->position] == 0; + } + +void setPosition(InputBuffer *ib, int position) +{ + ib->position= position; +} + +int getPosition(InputBuffer *ib) { + return ib->position; +} + +int currentChar(InputBuffer *ib) +{ + return ib->text[ib->position]; +} + +char *currentText(InputBuffer *ib) +{ + return ib->text + ib->position; +} + +#ifdef DO_TEST +#include +int main(int argc, char **argv){ + + InputBuffer *ib = mkInputBuffer("hello world\n"); + while(!atEnd(ib)){ + putchar(currentChar(ib)); + advance(ib,1); + } + return 0; +} + +#endif \ No newline at end of file diff --git a/lexer.c b/lexer.c index cbf08c7..5517df0 100644 --- a/lexer.c +++ b/lexer.c @@ -7,7 +7,6 @@ typedef struct Exploration Exploration; enum opcode { -<<<<<<< HEAD String, And, Or, @@ -17,17 +16,6 @@ enum opcode { Question_mark, Exclamation_mark, Dot -======= - STRING, - AND, - OR, - STAR, - PLUS, - CLASS, - QUESTION_MARK, - EXCLAMATION_MARK, - DOT ->>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee }; struct Exploration { @@ -65,17 +53,6 @@ int currentChar(Exploration *exploration) { return exploration->text[exploration->position]; } -<<<<<<< HEAD -======= - -char *currentText(Exploration *exploration) -{ - return exploration->text + exploration->position; -} - -struct String { enum opcode type; char *stringValue; int len;}; -struct Class { enum opcode type; char *stringValue; int len;}; ->>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee char *currentText(Exploration *exploration) { @@ -94,7 +71,6 @@ struct Dot { enum opcode type;}; union Node { enum opcode type; -<<<<<<< HEAD struct String String; struct Class Class; struct And And; @@ -104,13 +80,6 @@ union Node struct Question_mark Question_mark; struct Exclamation_mark Exclamation_mark; struct Dot Dot; -======= - union { - struct String STRING; - struct Class CLASS; - Node *children[2]; - }; ->>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee }; @@ -125,15 +94,9 @@ Node *mkNode(size_t size,enum opcode type) Node *mkString(char *value) { -<<<<<<< HEAD Node *node= new(String); node->String.stringValue= value; node->String.len=strlen(value); -======= - Node *node= mkNode(STRING); - node->STRING.stringValue= value; - node->STRING.len=strlen(value); ->>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee return node; } @@ -183,7 +146,6 @@ Node *mkQuestionMark(Node *child) return node; } -<<<<<<< HEAD Node *mkExclamationMark(Node *child) { Node *node= new(Exclamation_mark); @@ -199,41 +161,6 @@ Node *mkDot() if (node==0){ node=new(Dot); } -======= -Node *mkOr(Node *node1, Node *node2) -{ - Node *node= mkNode(OR); - node->children[0]= node1; - node->children[1]= node2; - return node; -} - -Node *mkClass(char* str) -{ - Node *node= mkNode(CLASS); - node->STRING.stringValue= str; - node->STRING.len=strlen(str); - return node; -} - -Node *mkQuestionMark(Node *child) -{ - Node *node= mkNode(QUESTION_MARK); - node->children[0]= child; - return node; -} - -Node *mkExclamationMark(Node *child) -{ - Node *node= mkNode(EXCLAMATION_MARK); - node->children[0]= child; - return node; -} - -Node *mkDot() -{ - Node *node= mkNode(DOT); ->>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee return node; } @@ -251,21 +178,12 @@ const char* getTypeName(enum opcode type) { switch (type) { -<<<<<<< HEAD case Star: return "Star"; case Exclamation_mark: return "NOT"; case Dot: return "Dot"; case Question_mark: return "QMARK"; case Plus : return "Plus"; case Class : return "Class"; -======= - case STAR: return "STAR"; - case EXCLAMATION_MARK: return "NOT"; - case DOT: return "DOT"; - case QUESTION_MARK: return "QMARK"; - case PLUS : return "PLUS"; - case CLASS : return "CLASS"; ->>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee default : printf("unexpected use");return 0; abort(); } @@ -279,7 +197,6 @@ void println(Node *node, int indent) } indent++; switch (node->type) { -<<<<<<< HEAD case String: printf("%.*s", get(node, String, len), get(node, String, stringValue)); return; case And: { printf("AND\n"); @@ -316,31 +233,6 @@ void println(Node *node, int indent) } printf("Unexpected type %i",node->type); -======= - case STRING: printf("%.*s", get(node, STRING, len), get(node, STRING, stringValue)); return; - case AND: { - printf("AND\n"); - println(node->children[0],indent);printf("\n"); - println(node->children[1],indent);printf("\n");return; - } - case OR: { - printf("OR\n"); - println(node->children[0],indent);printf("\n"); - println(node->children[1],indent);printf("\n");return; - } - case DOT:{ - printf("DOT\n");return; - } - case CLASS:{ - printf("CLASS : Char in [%.*s]\n",get(node, CLASS, len),get(node, CLASS, stringValue));return; - } - default : { - printf("%s\n",getTypeName(node->type)); - println(node->children[0],indent);printf("\n");return; - } - } - abort(); ->>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee } @@ -348,7 +240,6 @@ void println(Node *node, int indent) int execute(Node *node, Exploration *in) { switch (node->type) { -<<<<<<< HEAD case String: { if (strncmp(currentText(in), get(node,String,stringValue), get(node,String,len))) { return 0; @@ -390,55 +281,11 @@ int execute(Node *node, Exploration *in) return 0; } if (strchr(get(node,Class,stringValue), currentChar(in))) { -======= - case STRING: { - if (strncmp(currentText(in), node->STRING.stringValue, node->STRING.len)) { - return 0; - } - advance(in, node->STRING.len); - return 1; - } - case AND: { - int pos= getPosition(in); - if (!execute(node->children[0], in)) //si il y a eu une erreur - { - return 0; - } //si ça s'est bien passé - if (!execute(node->children[1], in)) { - setPosition(in, pos); - return 0; - } - return 1; - } - case OR: { - if (execute(node->children[0], in)) { - return 1; - } - return execute(node->children[1], in); - } - case STAR: { - while (execute(node->children[0], in)); - return 1; - } - case PLUS: { - if (!execute(node->children[0], in)) { - return 0; - } - while (execute(node->children[0], in)); - return 1; - } - case CLASS: { - if (!currentChar(in)) { - return 0; - } - if (strchr(node->STRING.stringValue, currentChar(in))) { ->>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee advance(in, 1); return 1; } return 0; } -<<<<<<< HEAD case Question_mark: { execute(get(node,Question_mark,children[0]), in); return 1; @@ -446,25 +293,12 @@ int execute(Node *node, Exploration *in) case Exclamation_mark: { int pos= getPosition(in); if (!execute(get(node,Exclamation_mark,children[0]), in)) { -======= - case QUESTION_MARK: { - execute(node->children[0], in); - return 1; - } - case EXCLAMATION_MARK: { - int pos= getPosition(in); - if (!execute(node->children[0], in)) { ->>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee return 1; } setPosition(in, pos); return 0; } -<<<<<<< HEAD case Dot: { -======= - case DOT: { ->>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee if (atEnd(in)) { return 0; } @@ -498,11 +332,7 @@ int main(int argc, char **argv) mkStar(mkString("c"))));*/ -<<<<<<< HEAD Node *program= mkOr(mkString("don't match pls"),mkStar(mkDot())); -======= - Node *program= mkStar(mkClass("abc")); ->>>>>>> c0cb24584cb692cf472ced68d40700ba949cdeee Node *program3=mkAnd(mkAnd(mkPlus(mkString("hello world")),mkExclamationMark(mkString("how are u world !?"))),mkStar(mkClass("abcde"))); if (!execute(program, in) || !atEnd(in)) diff --git a/parse b/parse new file mode 100755 index 0000000000000000000000000000000000000000..1dc61d2e4a61015d084c358c057ce8ef797e3e05 GIT binary patch literal 23640 zcmcIs4SZD9m48VHL<~tl(1=hUXb`Z3@M+K>lR)sn@)3blRB#xQ2}JVcWI~e`G;Ppz zcA9SL&vwz`meSp|b!)Y5Vb!uBK$l=YY}#&>DwgPCoe)3LmMXR>v;TAMxo_T^%%p7h zw~ya&-u<7Cd+xdC-gn=9FYkfURm)RTQVe~h8Mhi#>K&~Rk1%q}**sBQGyAa>*FGVtiolw|2BdplT3=aD&F z975-dJx#UV?a(7XFZIX~!F5t!vbgj}eVR&hjoCHTTNlosT~j%$rn;`Fc~*1b!dVOF z=Qq~p&*gTLeUiOxjY=E}tC(W`c$5hv{?$42uAg_V=*PeM@V!|(7oPv=jXyd0Rg%rc zPxqUVVj8Vtg7B(xU(TZ6g>`xWaUb79ahx0CQhpNr)+9K!BaxlgfSdTmALLsi`He~N z$w}~F5`0$@{6G@?_9Xacli(i$H}Q)3G-tn>#0Bw4XNU{o~(s_TMP#x{QtW6qD-Dw=)Xs_HslP4#E|q*`0QlVEa@JP8E7 zwLa8T74Z9w+g7b8S?ZmeKWDyu8atbtzrgUW*s$7*`uy9f8-xD9hSf`J>g)U)d|PYC z=C<1UI|sgE)TPCcQIjSk$t z^ntm>f$M7w>1=o4&g)Hs1E;$6(d@u=9}utAfukes<6#FbOAzLDIPgqc+A#Jya3qL* z?04YVHfk8(bl^JIi2bYsAM22R-hn&kl`aQi&W3~Mt?`AP36-hq`aE2|naVUY`U5_gJB~thTcQWgAwtr~h3ZqTW8{ z3MPw#*BM51!YlJl<9znDxENUNi&J2r+-gt%7@)Q2f)#o@cs07{DJr6i#%zxER^?}3 zyNB&>K2E(7D?|S%3AQhI5v1IjaS{5~(TizTXuvueSz@J}uzq$imI`P9b+%$+6!bhe@=D#5ea(f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ResDQ40#yerx!D0716sX#fBK literal 0 HcmV?d00001 diff --git a/parse.c b/parse.c index b3c85e3..a9408b4 100644 --- a/parse.c +++ b/parse.c @@ -3,22 +3,29 @@ #include #include #include -#define YYRULECOUNT 9 +#define YYRULECOUNT 11 #line 1 "parse.leg" #include #include #include +#include "inputBuffer.c" ; -enum op { String, Query, Star, Plus } ; +enum op { String, Query, Star, Plus, Or, And, Class, Dot, Exc } ; + typedef union Node Node; -struct String { enum op type; char *string; }; +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]; }; union Node { enum op type; @@ -26,58 +33,127 @@ union Node { struct Query Query; struct Star Star; struct Plus Plus; + struct Or Or; + struct And And; + struct Class Class; + struct Dot Dot; + struct Exc Exc; }; + +#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= calloc(1, sizeof(struct String)); - node->type= String; + Node *node= new(String); node->String.string= strdup(s); + node->String.len=strlen(s); return node; } Node *mkQuery(Node *n) { - Node *node= calloc(1, sizeof(struct Query)); - node->type= Query; + 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= calloc(1, sizeof(struct Star)); - node->type= Star; + 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= calloc(1, sizeof(struct Plus)); - node->type= Plus; + 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; +} + void print(Node *node) { switch (node->type) { case String: - printf("\"%s\"", node->String.string); - return; + printf("\"%s\"", node->String.string); + return; case Query: - print(node->Query.children[0]); - printf("?"); - return; + print(node->Query.children[0]); + printf("?"); + return; case Star: - print(node->Query.children[0]); - printf("*"); - return; + print(node->Query.children[0]); + printf("*"); + return; case Plus: - print(node->Query.children[0]); - printf("+"); - return; + 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; } abort(); } @@ -87,9 +163,19 @@ void println(Node *node) print(node); printf("\n"); } +InputBuffer *inputBuffer=0; -#define YYSTYPE Node * +#define YY_INPUT(buff,result,maxSize) \ +{if (atEnd(inputBuffer)){ \ + result=0; \ +} \ +else { \ + *buff=currentChar(inputBuffer); \ + advance(inputBuffer,1); \ + result=1; \ +}} +#define YYSTYPE Node * YYSTYPE yylval = 0; @@ -361,16 +447,46 @@ YY_LOCAL(void) yySet(yycontext *yy, char *text, int count) { yy->__val[count]= #define YYACCEPT yyAccept(yy, yythunkpos0) -YY_RULE(int) yy_space(yycontext *yy); /* 9 */ -YY_RULE(int) yy_class(yycontext *yy); /* 8 */ -YY_RULE(int) yy_string(yycontext *yy); /* 7 */ -YY_RULE(int) yy_atom(yycontext *yy); /* 6 */ -YY_RULE(int) yy_postfix(yycontext *yy); /* 5 */ +YY_RULE(int) yy_space(yycontext *yy); /* 11 */ +YY_RULE(int) yy_dot(yycontext *yy); /* 10 */ +YY_RULE(int) yy_class(yycontext *yy); /* 9 */ +YY_RULE(int) yy_string(yycontext *yy); /* 8 */ +YY_RULE(int) yy_atom(yycontext *yy); /* 7 */ +YY_RULE(int) yy_postfix(yycontext *yy); /* 6 */ +YY_RULE(int) yy_prefix(yycontext *yy); /* 5 */ YY_RULE(int) yy_and(yycontext *yy); /* 4 */ YY_RULE(int) yy_or(yycontext *yy); /* 3 */ YY_RULE(int) yy__(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 198 + __=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 196 + __=mkClass(yytext) ; + } +#undef yythunkpos +#undef yypos +#undef yy +} YY_ACTION(void) yy_1_string(yycontext *yy, char *yytext, int yyleng) { #define __ yy->__ @@ -378,7 +494,7 @@ YY_ACTION(void) yy_1_string(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_string\n")); { -#line 105 +#line 194 __ = mkString(yytext) ; } #undef yythunkpos @@ -393,7 +509,7 @@ YY_ACTION(void) yy_4_postfix(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_4_postfix\n")); { -#line 101 +#line 190 __ = s; } #undef yythunkpos @@ -409,7 +525,7 @@ YY_ACTION(void) yy_3_postfix(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_3_postfix\n")); { -#line 100 +#line 189 s = mkPlus(s) ; } #undef yythunkpos @@ -425,7 +541,7 @@ YY_ACTION(void) yy_2_postfix(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_2_postfix\n")); { -#line 99 +#line 188 s = mkStar(s) ; } #undef yythunkpos @@ -441,7 +557,7 @@ YY_ACTION(void) yy_1_postfix(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_postfix\n")); { -#line 98 +#line 187 s = mkQuery(s) ; } #undef yythunkpos @@ -449,6 +565,38 @@ YY_ACTION(void) yy_1_postfix(yycontext *yy, char *yytext, int yyleng) #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 185 + __ = 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 184 + __ = 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] @@ -458,7 +606,7 @@ YY_ACTION(void) yy_2_and(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_2_and\n")); { -#line 96 +#line 182 __ = p ; } #undef yythunkpos @@ -476,8 +624,8 @@ YY_ACTION(void) yy_1_and(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_and\n")); { -#line 95 - __ = mkAnd(p, a) ; +#line 181 + __ = mkAnd(p, a); ; } #undef yythunkpos #undef yypos @@ -494,7 +642,7 @@ YY_ACTION(void) yy_2_or(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_2_or\n")); { -#line 93 +#line 179 __ = a ; } #undef yythunkpos @@ -512,8 +660,8 @@ YY_ACTION(void) yy_1_or(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_or\n")); { -#line 92 - __ = mkOr(o, a) ; +#line 178 + __ = mkOr(o, a) ; } #undef yythunkpos #undef yypos @@ -529,7 +677,7 @@ YY_ACTION(void) yy_1_start(yycontext *yy, char *yytext, int yyleng) #define yythunkpos yy->__thunkpos yyprintf((stderr, "do yy_1_start\n")); { -#line 90 +#line 176 yylval = o ; } #undef yythunkpos @@ -560,18 +708,18 @@ YY_RULE(int) yy_space(yycontext *yy) yyprintf((stderr, " fail %s @ %s\n", "space", yy->__buf+yy->__pos)); return 0; } -YY_RULE(int) yy_class(yycontext *yy) +YY_RULE(int) yy_dot(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; - yyprintf((stderr, "%s\n", "class")); if (!yymatchChar(yy, '[')) goto l9; if (!yymatchDot(yy)) goto l9; if (!yymatchDot(yy)) goto l9; if (!yymatchDot(yy)) goto l9; if (!yymatchChar(yy, ']')) goto l9; - yyprintf((stderr, " ok %s @ %s\n", "class", yy->__buf+yy->__pos)); + yyprintf((stderr, "%s\n", "dot")); if (!yy__(yy)) goto l9; if (!yymatchChar(yy, '.')) goto l9; if (!yy__(yy)) goto l9; yyDo(yy, yy_1_dot, yy->__begin, yy->__end); if (!yy__(yy)) goto l9; + yyprintf((stderr, " ok %s @ %s\n", "dot", yy->__buf+yy->__pos)); return 1; l9:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; - yyprintf((stderr, " fail %s @ %s\n", "class", yy->__buf+yy->__pos)); + yyprintf((stderr, " fail %s @ %s\n", "dot", yy->__buf+yy->__pos)); return 0; } -YY_RULE(int) yy_string(yycontext *yy) +YY_RULE(int) yy_class(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; - yyprintf((stderr, "%s\n", "string")); if (!yymatchChar(yy, '"')) goto l10; yyText(yy, yy->__begin, yy->__end); { + yyprintf((stderr, "%s\n", "class")); if (!yymatchChar(yy, '[')) goto l10; if (!yy__(yy)) goto l10; yyText(yy, yy->__begin, yy->__end); { #define yytext yy->__text #define yyleng yy->__textlen if (!(YY_BEGIN)) goto l10; @@ -579,7 +727,10 @@ if (!(YY_BEGIN)) goto l10; #undef yyleng } l11:; - { int yypos12= yy->__pos, yythunkpos12= 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 l12; goto l11; + { int yypos12= yy->__pos, yythunkpos12= yy->__thunkpos; + { int yypos13= yy->__pos, yythunkpos13= yy->__thunkpos; if (!yymatchChar(yy, ']')) goto l13; goto l12; + l13:; yy->__pos= yypos13; yy->__thunkpos= yythunkpos13; + } if (!yy_string(yy)) goto l12; goto l11; l12:; yy->__pos= yypos12; yy->__thunkpos= yythunkpos12; } yyText(yy, yy->__begin, yy->__end); { #define yytext yy->__text @@ -587,86 +738,125 @@ if (!(YY_BEGIN)) goto l10; if (!(YY_END)) goto l10; #undef yytext #undef yyleng - } if (!yymatchChar(yy, '"')) goto l10; yyDo(yy, yy_1_string, yy->__begin, yy->__end); if (!yy__(yy)) goto l10; - yyprintf((stderr, " ok %s @ %s\n", "string", yy->__buf+yy->__pos)); + } if (!yymatchChar(yy, ']')) goto l10; yyDo(yy, yy_1_class, yy->__begin, yy->__end); if (!yy__(yy)) goto l10; + yyprintf((stderr, " ok %s @ %s\n", "class", yy->__buf+yy->__pos)); return 1; l10:; 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 l14; yyText(yy, yy->__begin, yy->__end); { +#define yytext yy->__text +#define yyleng yy->__textlen +if (!(YY_BEGIN)) goto l14; +#undef yytext +#undef yyleng + } + l15:; + { int yypos16= yy->__pos, yythunkpos16= 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 l16; goto l15; + l16:; yy->__pos= yypos16; yy->__thunkpos= yythunkpos16; + } yyText(yy, yy->__begin, yy->__end); { +#define yytext yy->__text +#define yyleng yy->__textlen +if (!(YY_END)) goto l14; +#undef yytext +#undef yyleng + } if (!yymatchChar(yy, '"')) goto l14; yyDo(yy, yy_1_string, yy->__begin, yy->__end); if (!yy__(yy)) goto l14; + yyprintf((stderr, " ok %s @ %s\n", "string", yy->__buf+yy->__pos)); + return 1; + l14:; 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 yypos14= yy->__pos, yythunkpos14= yy->__thunkpos; if (!yy_string(yy)) goto l15; goto l14; - l15:; yy->__pos= yypos14; yy->__thunkpos= yythunkpos14; if (!yy_class(yy)) goto l13; + { int yypos18= yy->__pos, yythunkpos18= yy->__thunkpos; if (!yy_string(yy)) goto l19; goto l18; + l19:; yy->__pos= yypos18; yy->__thunkpos= yythunkpos18; if (!yy_class(yy)) goto l20; goto l18; + l20:; yy->__pos= yypos18; yy->__thunkpos= yythunkpos18; if (!yy_dot(yy)) goto l17; } - l14:; + l18:; yyprintf((stderr, " ok %s @ %s\n", "atom", yy->__buf+yy->__pos)); return 1; - l13:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; + l17:; 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 l16; yyDo(yy, yySet, -1, 0); - { int yypos17= yy->__pos, yythunkpos17= yy->__thunkpos; - { int yypos19= yy->__pos, yythunkpos19= yy->__thunkpos; if (!yymatchChar(yy, '?')) goto l20; if (!yy__(yy)) goto l20; yyDo(yy, yy_1_postfix, yy->__begin, yy->__end); goto l19; - l20:; yy->__pos= yypos19; yy->__thunkpos= yythunkpos19; if (!yymatchChar(yy, '*')) goto l21; if (!yy__(yy)) goto l21; yyDo(yy, yy_2_postfix, yy->__begin, yy->__end); goto l19; - l21:; yy->__pos= yypos19; yy->__thunkpos= yythunkpos19; if (!yymatchChar(yy, '+')) goto l17; if (!yy__(yy)) goto l17; yyDo(yy, yy_3_postfix, yy->__begin, yy->__end); + yyprintf((stderr, "%s\n", "postfix")); if (!yy_atom(yy)) goto l21; yyDo(yy, yySet, -1, 0); + { int yypos22= yy->__pos, yythunkpos22= yy->__thunkpos; + { int yypos24= yy->__pos, yythunkpos24= yy->__thunkpos; if (!yymatchChar(yy, '?')) goto l25; if (!yy__(yy)) goto l25; yyDo(yy, yy_1_postfix, yy->__begin, yy->__end); goto l24; + l25:; yy->__pos= yypos24; yy->__thunkpos= yythunkpos24; if (!yymatchChar(yy, '*')) goto l26; if (!yy__(yy)) goto l26; yyDo(yy, yy_2_postfix, yy->__begin, yy->__end); goto l24; + l26:; yy->__pos= yypos24; yy->__thunkpos= yythunkpos24; if (!yymatchChar(yy, '+')) goto l22; if (!yy__(yy)) goto l22; yyDo(yy, yy_3_postfix, yy->__begin, yy->__end); } - l19:; goto l18; - l17:; yy->__pos= yypos17; yy->__thunkpos= yythunkpos17; + l24:; goto l23; + l22:; yy->__pos= yypos22; yy->__thunkpos= yythunkpos22; } - l18:; yyDo(yy, yy_4_postfix, yy->__begin, yy->__end); + l23:; 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; - l16:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; + l21:; 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 yypos28= yy->__pos, yythunkpos28= yy->__thunkpos; if (!yymatchChar(yy, '!')) goto l29; if (!yy__(yy)) goto l29; if (!yy_postfix(yy)) goto l29; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_prefix, yy->__begin, yy->__end); goto l28; + l29:; yy->__pos= yypos28; yy->__thunkpos= yythunkpos28; if (!yy_postfix(yy)) goto l27; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_2_prefix, yy->__begin, yy->__end); + } + l28:; + yyprintf((stderr, " ok %s @ %s\n", "prefix", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 1, 0); + return 1; + l27:; 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 yypos23= yy->__pos, yythunkpos23= yy->__thunkpos; if (!yy_postfix(yy)) goto l24; yyDo(yy, yySet, -2, 0); if (!yy_and(yy)) goto l24; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_and, yy->__begin, yy->__end); goto l23; - l24:; yy->__pos= yypos23; yy->__thunkpos= yythunkpos23; if (!yy_postfix(yy)) goto l22; yyDo(yy, yySet, -2, 0); yyDo(yy, yy_2_and, yy->__begin, yy->__end); + { int yypos31= yy->__pos, yythunkpos31= yy->__thunkpos; if (!yy_prefix(yy)) goto l32; yyDo(yy, yySet, -2, 0); if (!yy_and(yy)) goto l32; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_and, yy->__begin, yy->__end); goto l31; + l32:; yy->__pos= yypos31; yy->__thunkpos= yythunkpos31; if (!yy_prefix(yy)) goto l30; yyDo(yy, yySet, -2, 0); yyDo(yy, yy_2_and, yy->__begin, yy->__end); } - l23:; + l31:; yyprintf((stderr, " ok %s @ %s\n", "and", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 2, 0); return 1; - l22:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; + l30:; 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 yypos26= yy->__pos, yythunkpos26= yy->__thunkpos; if (!yy_and(yy)) goto l27; yyDo(yy, yySet, -2, 0); if (!yymatchChar(yy, '|')) goto l27; if (!yy__(yy)) goto l27; if (!yy_or(yy)) goto l27; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_or, yy->__begin, yy->__end); goto l26; - l27:; yy->__pos= yypos26; yy->__thunkpos= yythunkpos26; if (!yy_and(yy)) goto l25; yyDo(yy, yySet, -2, 0); yyDo(yy, yy_2_or, yy->__begin, yy->__end); + { int yypos34= yy->__pos, yythunkpos34= yy->__thunkpos; if (!yy_and(yy)) goto l35; yyDo(yy, yySet, -2, 0); if (!yy__(yy)) goto l35; if (!yymatchChar(yy, '|')) goto l35; if (!yy__(yy)) goto l35; if (!yy_or(yy)) goto l35; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_or, yy->__begin, yy->__end); goto l34; + l35:; yy->__pos= yypos34; yy->__thunkpos= yythunkpos34; if (!yy_and(yy)) goto l33; yyDo(yy, yySet, -2, 0); yyDo(yy, yy_2_or, yy->__begin, yy->__end); } - l26:; + l34:; yyprintf((stderr, " ok %s @ %s\n", "or", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 2, 0); return 1; - l25:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; + l33:; 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", "_")); - l29:; - { int yypos30= yy->__pos, yythunkpos30= yy->__thunkpos; if (!yy_space(yy)) goto l30; goto l29; - l30:; yy->__pos= yypos30; yy->__thunkpos= yythunkpos30; + l37:; + { int yypos38= yy->__pos, yythunkpos38= yy->__thunkpos; if (!yy_space(yy)) goto l38; goto l37; + l38:; yy->__pos= yypos38; yy->__thunkpos= yythunkpos38; } yyprintf((stderr, " ok %s @ %s\n", "_", yy->__buf+yy->__pos)); return 1; } YY_RULE(int) yy_start(yycontext *yy) { int yypos0= yy->__pos, yythunkpos0= yy->__thunkpos; yyDo(yy, yyPush, 1, 0); - yyprintf((stderr, "%s\n", "start")); if (!yy__(yy)) goto l31; if (!yy_or(yy)) goto l31; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_start, yy->__begin, yy->__end); + yyprintf((stderr, "%s\n", "start")); if (!yy__(yy)) goto l39; if (!yy_or(yy)) goto l39; yyDo(yy, yySet, -1, 0); yyDo(yy, yy_1_start, yy->__begin, yy->__end); yyprintf((stderr, " ok %s @ %s\n", "start", yy->__buf+yy->__pos)); yyDo(yy, yyPop, 1, 0); return 1; - l31:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; + l39:; yy->__pos= yypos0; yy->__thunkpos= yythunkpos0; yyprintf((stderr, " fail %s @ %s\n", "start", yy->__buf+yy->__pos)); return 0; } @@ -718,14 +908,119 @@ YY_PARSE(yycontext *) YYRELEASE(yycontext *yyctx) } #endif -#line 113 "parse.leg" +#line 205 "parse.leg" + + +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) + +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; + } + } + printf("this cannot happen\n"); + abort(); +} int main(int argc, char **argv) { - while (yyparse()) { - println(yylval); + 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; diff --git a/parse.leg b/parse.leg index c1ea926..4c6ee4f 100644 --- a/parse.leg +++ b/parse.leg @@ -3,12 +3,14 @@ #include #include #include +#include "inputBuffer.c" ; enum op { String, Query, Star, Plus, Or, And, Class, Dot, Exc } ; + typedef union Node Node; -struct String { enum op type; char *string; }; +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]; }; @@ -31,27 +33,34 @@ union Node { struct Exc Exc; }; + +#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= calloc(1, sizeof(struct String)); - node->type= String; + Node *node= new(String); node->String.string= strdup(s); + node->String.len=strlen(s); return node; } - - Node *mkQuery(Node *n) { - Node *node= calloc(1, sizeof(struct Query)); - node->type= Query; + Node *node= new(Query); node->Query.children[0]= n; return node; } Node *mkOr(Node *node1, Node *node2) { - Node *node= calloc(1, sizeof(struct Or)); + Node *node= new(Or); node->Or.children[0]= node1; node->Or.children[1]= node2; return node; @@ -59,7 +68,7 @@ Node *mkOr(Node *node1, Node *node2) Node *mkAnd(Node *node1, Node *node2) { - Node *node= calloc(1, sizeof(struct And)); + Node *node= new(And); node->And.children[0]= node1; node->And.children[1]= node2; return node; @@ -67,15 +76,14 @@ Node *mkAnd(Node *node1, Node *node2) Node *mkStar(Node *n) { - Node *node= calloc(1, sizeof(struct Star)); - node->type= Star; + Node *node= new(Star); node->Star.children[0]= n; return node; } Node *mkClass(char* str) { - Node *node= calloc(1, sizeof(struct Class)); + Node *node= new(Class); node->Class.stringValue= str; node->Class.len=strlen(str); return node; @@ -83,23 +91,20 @@ Node *mkClass(char* str) Node *mkPlus(Node *n) { - Node *node= calloc(1, sizeof(struct Plus)); - node->type= Plus; + Node *node= new(Plus); node->Plus.children[0]= n; return node; } Node *mkDot() { - Node *node= calloc(1, sizeof(struct Dot)); - node->type= Dot; + Node *node= new(Dot); return node; } Node *mkExc(Node *n) { - Node *node= calloc(1, sizeof(struct Exc)); - node->type= Exc; + Node *node= new(Exc); node->Exc.children[0]= n; return node; } @@ -108,41 +113,40 @@ void print(Node *node) { switch (node->type) { case String: - printf("\"%s\"", node->String.string); - return; + printf("\"%s\"", node->String.string); + return; case Query: - print(node->Query.children[0]); - printf("?"); - return; + print(node->Query.children[0]); + printf("?"); + return; case Star: - print(node->Query.children[0]); - printf("*"); - return; + print(node->Query.children[0]); + printf("*"); + return; case Plus: - print(node->Query.children[0]); - printf("+"); - return; + print(node->Query.children[0]); + return; case Or: - print(node->Or.children[0]); - print(node->Or.children[1]); - printf("Or"); - return; + print(node->Or.children[0]); + printf("Or"); + print(node->Or.children[1]); + return; case And: - print(node->And.children[0]); - print(node->And.children[1]); - printf("And"); - return; + print(node->And.children[0]); + printf("And"); + print(node->And.children[1]); + return; case Class: - printf("Class"); - printf("\"%s\"", node->Class.stringValue); - return; + printf("Class"); + printf("\"%s\"", node->Class.stringValue); + return; case Dot: - printf("Dot"); - return; + printf("Dot"); + return; case Exc: - printf("!"); - print(node->Exc.children[0]); - return; + printf("!"); + print(node->Exc.children[0]); + return; } abort(); } @@ -152,19 +156,29 @@ 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; \ +}} #define YYSTYPE Node * - YYSTYPE yylval = 0; %} start = - o:or { yylval = o } -or = a:and "|" - o:or { $$ = mkOr(o, a) } +or = a:and - "|" - o:or {$$ = mkOr(o, a) } | a:and { $$ = a } -and = p:prefix a:and { $$ = mkAnd(p, a) } +and = p:prefix a:and { $$ = mkAnd(p, a); } | p:prefix { $$ = p } prefix = "!"- p : postfix {$$ = mkExc(p)} @@ -179,9 +193,9 @@ atom = string | class | dot string = '"' < [^"]* > '"' { $$ = mkString(yytext) } - -class = '[' <[]*> ']' { $$=mkClass(yytext) } - +class = '['-<(!']'string)*> ']' { $$=mkClass(yytext) } - -dot = '.' {$$=mkDot()} - +dot = -'.'- {$$=mkDot()} - - = space* @@ -190,11 +204,116 @@ space = [ \t] | '\n' '\r'* | '\r' '\n'* %% +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) + +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; + } + } + printf("this cannot happen\n"); + abort(); +} + int main(int argc, char **argv) { - while (yyparse()) { - println(yylval); + 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;