diff --git a/parse.leg b/parse.leg index b032995..bbd1e62 100644 --- a/parse.leg +++ b/parse.leg @@ -4,7 +4,7 @@ #include #include ; -enum op { String, Query, Star, Plus } ; +enum op { String, Query, Star, Plus, Or, And } ; typedef union Node Node; @@ -12,6 +12,9 @@ struct String { enum op type; char *string; }; 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; }; union Node { enum op type; @@ -19,6 +22,9 @@ union Node { struct Query Query; struct Star Star; struct Plus Plus; + struct Or Or; + struct And And; + struct Class Class; }; Node *mkString(char *s) @@ -29,6 +35,8 @@ Node *mkString(char *s) return node; } + + Node *mkQuery(Node *n) { Node *node= calloc(1, sizeof(struct Query)); @@ -37,6 +45,22 @@ Node *mkQuery(Node *n) return node; } +Node *mkOr(Node *node1, Node *node2) +{ + Node *node= calloc(1, sizeof(struct Or)); + node->Or.children[0]= node1; + node->Or.children[1]= node2; + return node; +} + +Node *mkAnd(Node *node1, Node *node2) +{ + Node *node= calloc(1, sizeof(struct And)); + node->And.children[0]= node1; + node->And.children[1]= node2; + return node; +} + Node *mkStar(Node *n) { Node *node= calloc(1, sizeof(struct Star)); @@ -45,6 +69,14 @@ Node *mkStar(Node *n) return node; } +Node *mkClass(char* str) +{ + Node *node= calloc(1, sizeof(struct Class)); + node->Class.stringValue= str; + node->Class.len=strlen(str); + return node; +} + Node *mkPlus(Node *n) { Node *node= calloc(1, sizeof(struct Plus)); @@ -71,6 +103,16 @@ void print(Node *node) print(node->Query.children[0]); printf("+"); return; + case Or: + print(node->Or.children[0]); + print(node->Or.children[1]); + printf("Or"); + return; + case And: + print(node->And.children[0]); + print(node->And.children[1]); + printf("And"); + return; } abort(); } @@ -104,7 +146,10 @@ atom = string | class string = '"' < [^"]* > '"' { $$ = mkString(yytext) } - -class = '[' ... ']' +class = '[' <[]*> ']' { $$=mkClass(yytext) } - + + + - = space*