Mercurial > hg > Applications > Grep
view c/regexParser/main.cc @ 62:a49b4a8b8c14
implement isLiteral
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 14 Jul 2015 16:45:07 +0900 |
parents | 67cade0e35b0 |
children | 8fd3d35e9861 |
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); bool isLiteral(char c) { if (('a'<=c && c<='z')||('A'<=c && c<='Z')||('0'<=c && c<='9')) { return true; } return false; } void printNodeDate(NodePtr n) { puts("---------------------"); printf("Self Node char : %c\n", n->character); printf("Self Node addr : %p\n", n->self); printf("left Node addr : %p\n", n->left); printf("right Node addr : %p\n", n->right); puts("---------------------"); puts(""); } 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; printNodeDate(n); return n; } // <charClass> ::= '['<literal>'-'<literal>']' NodePtr charClass() { ptr++; NodePtr n = (NodePtr)malloc(sizeof(Node)); 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; if (isLiteral(c)) { 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() { ptr++; NodePtr n = createNode('|',regexHeadNode,regex()); return n; } // <*> ::= <regex>'*' NodePtr asterisk() { ptr++; NodePtr n = createNode('*',regexHeadNode,regex()); return n; } // <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 if (*ptr == '*'){ n = asterisk(); } 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; }