Mercurial > hg > Applications > Grep
view c/regexParser/main.cc @ 58:4053c3e0fa7f
implement group()
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 12 Jun 2015 19:02:00 +0900 |
parents | 71b497d25273 |
children | af189c727733 |
line wrap: on
line source
/* * <literal> ::= [a-z][A-Z][0-9] * <charClass> ::= '['<literal>'-'<literal>']' * <string> ::= <literal><literal>* * <group> ::= '('<regex>')' * <or> ::= <regex>'|'<regex> * <*> ::= <regex>'*' * <regex> ::= <string>|<or>|<charClass>|<group>|<*> */ #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct node { struct node *self; char character; struct node *left; struct node *right; } Node, *NodePtr; char *ptr; NodePtr regexHeadNode; NodePtr charClass(); NodePtr string(); NodePtr group(); NodePtr _or(); NodePtr asterisk(); NodePtr regex(); NodePtr createNode(char,NodePtr,NodePtr); NodePtr createNode(char character, NodePtr left, NodePtr right) { NodePtr n = (NodePtr)malloc(sizeof(Node)); n->self = n; n->character = character; n->left = left; n->right = right; return n; } // <charClass> ::= '['<literal>'-'<literal>']' NodePtr charClass() { ptr++; NodePtr n = createNode(0,0,0); return n; } // <literal> ::= [a-z][A-Z][0-9] NodePtr literal() { char c = *ptr; NodePtr n = createNode(c,0,0); ptr++; return n; } // <string> ::= <literal><literal>* NodePtr string() { char c = *ptr; NodePtr n = NULL; printf("%c\n",c); if (('a'<=c && c<='z')||('A'<=c && c<='Z')||('0'<=c && c<='9')) { n = createNode(0,literal(),string()); } else { n = createNode(0,0,0); } return n; } // <group> ::= '('<regex>')' | '('<regex>'|'<regex>')' NodePtr group() { NodePtr n; if (*ptr == ')') { n = createNode(0,0,0); ptr++; } else { ptr++; n = regex(); } return n; } // <or> ::= <regex>'|'<regex> NodePtr _or() { NodePtr n = createNode('|',regexHeadNode,regex()); return n; } // <*> ::= <regex>'*' NodePtr asterisk() { } // <regex> ::= <string>|<or>|<charClass>|<group>|<*> NodePtr regex() { NodePtr n; while (*ptr != '\0') { if ((*ptr == '(') || (*ptr == ')')) { n = group(); } else if (*ptr == '[') { n = charClass(); } else if (*ptr == '|'){ n = _or(); } else { n = string(); regexHeadNode = n; } } 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; }