Mercurial > hg > Applications > Grep
view c/regexParser/main.cc @ 56:8901bc071d33
implement string() and literal()
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 11 Jun 2015 16:24:40 +0900 |
parents | 883e3473a9f5 |
children | 71b497d25273 |
line wrap: on
line source
/* * <literal> ::= [a-z][A-Z][0-9] * <charClass> ::= '['<literal>'-'<literal>']' * <string> ::= <literal><literal>* * <or> ::= '('<regex>'|'<regex>')' * <*> ::= <regex>'*' * <regex> ::= <literal>|<conc>|<or>|<charClass> */ #include <stdio.h> #include <stdlib.h> #include <string.h> char *ptr; typedef struct node { int character; struct node *left; struct node *right; } Node, *NodePtr; NodePtr charClass(); NodePtr string(); NodePtr _or(); NodePtr asterisk(); NodePtr regex(); NodePtr createNode(int,NodePtr,NodePtr); NodePtr createNode(int character, NodePtr left, NodePtr right) { NodePtr n; n = (NodePtr)malloc(sizeof(Node)); n->character = character; n->left = left; n->right = right; return n; } // <charClass> ::= '['<literal>'-'<literal>']' NodePtr charClass() { NodePtr n = createNode(0,0,0); return n; } // <literal> ::= [a-z][A-Z][0-9] NodePtr literal() { char c = *ptr; createNode(c,0,0); } // <string> ::= <literal><literal>* NodePtr string() { char c = *ptr; NodePtr n = (NodePtr)malloc(sizeof(Node)); if (('a'<=c && c<='z')||('A'<=c && c<='Z')||('0'<=c && c<='9')) { n = createNode(0,literal(),string()); return n; } else { n = createNode(0,0,0); } } // <or> ::= '('<regex>'|'<regex>')' NodePtr _or() { regex(); while(*ptr++ == ')') { if (*ptr == '|') { ptr++; regex(); } } } // <*> ::= <regex>'*' NodePtr asterisk() { } // <regex> ::= <literal>|<string>|<or>|<charClass> // <literal> は <string> に内包されるから、<regex> ::= <string>|<or>|<charClass>が正しい?? NodePtr regex() { NodePtr n; while (int c = *ptr++) { if (c == '(') { ptr++; _or(); } else if (c == '[') { charClass(); } else { n = createNode(0,string(),regex()); } } return n; } int main(int argc, char **argv) { for (int i = 1; i < argc; i++) { if (strcmp(argv[i],"-regex") == 0) { ptr = argv[i+1]; i++; } } printf("regex : %s\n",ptr); NodePtr n = regex(); return 0; }