diff --git a/lexer.c b/lexer.c index e566f68..d96b605 100644 --- a/lexer.c +++ b/lexer.c @@ -9,9 +9,13 @@ typedef struct Exploration Exploration; enum opcode { STRING, AND, + OR, STAR, PLUS, - QMARK + CLASS, + QUESTION_MARK, + EXCLAMATION_MARK, + DOT }; struct Exploration { @@ -27,27 +31,34 @@ Exploration *mkExploration(char *text) return exploration; } -char advance(Exploration *exploration) +void advance(Exploration *exploration, int add) { - return exploration->text[exploration->position++]; + exploration->position+= add; } -int endExploration(Exploration *exploration) -{ - if (exploration->position >= strlen(exploration->text)) - { - return 1; - } else - { - return 0; +int atEnd(Exploration *exploration) { + return exploration->text[exploration->position] == 0; } -} void setPosition(Exploration *exploration, int position) { exploration->position= position; } +int getPosition(Exploration *exploration) { + return exploration->position; +} + +int currentChar(Exploration *exploration) +{ + return exploration->text[exploration->position]; +} + +char *currentText(Exploration *exploration) +{ + return exploration->text + exploration->position; +} + struct Node { @@ -97,67 +108,113 @@ Node *mkPlus(Node *child) return node; } -Node *mkQmark(Node *child) +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->stringValue= 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(QMARK); + Node *node= mkNode(EXCLAMATION_MARK); node->children[0]= child; return node; } +Node *mkDot() +{ + Node *node= mkNode(DOT); + return node; +} + + + + int execute(Node *node, Exploration *in) { switch (node->type) { case STRING: { - for (int i=0; i < strlen(node->stringValue);i++) - { - if (advance(in) != node->stringValue[i]) - { - return 0; - } - } - if (endExploration(in)==1) - { - return 1; - } else - { - return -1; + int length= strlen(node->stringValue); + if (strncmp(currentText(in), node->stringValue, length)) { + return 0; } + advance(in, length); + return 1; } case AND: { - if (execute(node->children[0], in) == 0) //si il y a eu une erreur + int pos= getPosition(in); + if (!execute(node->children[0], in)) //si il y a eu une erreur { return 0; - } else //si ça s'est bien passé - { - return execute(node->children[1], in); + } //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: { - if(execute(node->children[0], in) == 1){ - execute(node->children[0], in); - } - else return 1; + while (execute(node->children[0], in)); + return 1; } 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; - - } - - } - case QMARK: { - // 0 ou 1 - if(execute(node->children[0], in)==1) return execute(node->children[0], in); - return 1; - } + 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->stringValue, currentChar(in))) { + advance(in, 1); + return 1; + } + return 0; + } + case QUESTION_MARK: { + execute(node->children[0], in); + return 1; + } + case EXCLAMATION_MARK: { + int pos= getPosition(in); + if (!execute(node->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(); @@ -166,11 +223,10 @@ int execute(Node *node, Exploration *in) int main(int argc, char **argv) { - Exploration *in= mkExploration("acc"); + Exploration *in= mkExploration("aabbcc"); /*Node *program= mkString("aabbcc"); - Node *program= mkAnd(mkString("aab"), mkString("bcc"));*/ @@ -180,37 +236,22 @@ int main(int argc, char **argv) mkAnd(mkString("bb"), mkString("cc")));*/ - /* Node *program= - mkAnd( mkString("aabbcc"), - mkAnd(mkStar(mkString("c")), // "a"*"b"*"c"* + /*Node *program= + mkOr(mkPlus(mkString("a")), // "a"*"b"*"c"* mkAnd(mkStar(mkString("b")), - mkStar(mkString("c"))))); -*/ - Node *program= - mkAnd(mkQmark(mkString("a")), // "a"*"b"*"c"* - mkAnd(mkQmark(mkString("b")), - mkPlus(mkString("c")))); - - if (execute(program, in)<=0) - { - printf("no match\n"); - } else - { - printf("match\n"); - }// 0 => no match, 1 => match - + mkStar(mkString("c"))));*/ + Node *program= mkStar(mkClass("abc")); - /*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*/ + if (!execute(program, in) || !atEnd(in)) + { + printf("no match, current position : %i\n", getPosition(in)); + } else + { + printf("match, current position : %i\n", getPosition(in)); + }// 0 => no match, 1 => match