From 6bed98370802407cb8611bf41cf9e1b1c3668538 Mon Sep 17 00:00:00 2001 From: Vank143 <78201359+Vank143@users.noreply.github.com> Date: Mon, 21 Jun 2021 17:18:43 +0200 Subject: [PATCH] Update lexer.c --- lexer.c | 64 +++++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/lexer.c b/lexer.c index 6a45097..c7fe20f 100644 --- a/lexer.c +++ b/lexer.c @@ -38,7 +38,7 @@ int endExploration(Exploration *exploration) return 1; } else { - return 0; + return -1; } } @@ -101,50 +101,46 @@ int execute(Node *node, Exploration *in) { switch (node->type) { case STRING: { + int pos= in->position; for (int i=0; i < strlen(node->stringValue);i++) { if (advance(in) != node->stringValue[i]) { + setPosition(in, pos); return 0; } } - if (endExploration(in)==1) - { - return 1; - } else - { - return -1; - } + return endExploration(in); } case AND: { + int pos= in->position; if (execute(node->children[0], in) == 0) //si il y a eu une erreur { + setPosition(in, pos); return 0; } else //si ça s'est bien passé { - return execute(node->children[1], in); + int result= execute(node->children[1], in); + if (result == 0) { + setPosition(in, pos); + } + return result; } } case STAR: { - if(execute(node->children[0], in) == 1){ - execute(node->children[0], in); - } - else return 1; + int pos= in->position; + int result; + while ((result= execute(node->children[0], in)) == -1){ + pos= in->position;; + } + if (result == 0) { + setPosition(in, pos); + } + return endExploration(in); } case PLUS: { - if(execute(node->children[0],in)==0){ - return 0; - } - else{ - while(execute(node->children[0], in) == 1){ - - execute(node->children[0], in); - - } - return 1; - - } - + Node *newNode= mkAnd(node->children[0], mkStar(node->children[0])); + return execute(newNode,in); } } printf("this cannot happen\n"); @@ -158,7 +154,6 @@ int main(int argc, char **argv) /*Node *program= mkString("aabbcc"); - Node *program= mkAnd(mkString("aab"), mkString("bcc"));*/ @@ -168,16 +163,11 @@ int main(int argc, char **argv) mkAnd(mkString("bb"), mkString("cc")));*/ - /* Node *program= - mkAnd( mkString("aabbcc"), - mkAnd(mkStar(mkString("c")), // "a"*"b"*"c"* - mkAnd(mkStar(mkString("b")), - mkStar(mkString("c"))))); -*/ - Node *program= + Node *program= mkAnd(mkPlus(mkString("a")), // "a"*"b"*"c"* - mkAnd(mkPlus(mkString("b")), - mkPlus(mkString("c")))); + mkAnd(mkStar(mkString("b")), + mkStar(mkString("c")))); + if (execute(program, in)<=0) { @@ -193,11 +183,9 @@ int main(int argc, char **argv) /*Input *in1= mkInput("hello"); Input *in2= mkInput("world"); Input *in3= mkInput("hello world"); - Node *program= mkOr(mkString("hello"), // "hello" | "world" mkString("world")); - printf("%i\n", execute(program, in)); // 0 => no match, 1 => match*/