Mercurial > hg > Members > masakoha > testcode
view c/regexParser/createRegexParser.cc @ 112:ec485345daf9 pairPro
some function use static
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 23 Nov 2015 15:54:19 +0900 |
parents | 70069d4647a0 |
children |
line wrap: on
line source
#include <stdlib.h> #include <stdio.h> #include "regexParser.h" static NodePtr createNode(RegexInfoPtr,unsigned char,NodePtr,NodePtr); static NodePtr charClass(RegexInfoPtr); static NodePtr group(RegexInfoPtr); static void token(RegexInfoPtr); static NodePtr regexAtom(RegexInfoPtr); NodePtr regex(RegexInfoPtr); /** * Create a node of regex parse tree. * tokenType * regexPosition(state) * stateTransitionTable */ static NodePtr createNode(RegexInfoPtr ri,unsigned char character, NodePtr left, NodePtr right) { NodePtr n = (NodePtr)malloc(sizeof(Node)); if (n == NULL) { mallocFailedMessage(); } n->tokenType = ri->tokenType; n->self = n; n->Value.character = character; n->left = left; n->right = right; if (ri->tokenType == '*') { n->left->parent = n->self; } else if (ri->tokenType != 'a') { n->right = right; n->left->parent = n->right->parent = n->self; } if (ri->tokenType == 'a') { n->nodeNumber = ri->nodeNumber; ri->nodeNumber++; ri->tokenType = 0; } return n; } // <charClass> ::= '['<literal>'-'<literal>']' static NodePtr charClass(RegexInfoPtr ri) { NodePtr n = (NodePtr)malloc(sizeof(Node)); if (n == NULL) { mallocFailedMessage(); } while (ri->ptr[0] == '-') { ri->ptr++; } return n; } // <literal> ::= [a-z][A-Z][0-9] static NodePtr literal(RegexInfoPtr ri) { NodePtr n = createNode(ri,ri->ptr[0],0,0); ri->ptr++; return n; } // <group> ::= '('<regex>')' static NodePtr group(RegexInfoPtr ri) { return regex(ri); } static void token(RegexInfoPtr ri) { while (ri->ptr[0] != '\0') { if (ri->ptr[0] == '('){ ri->ptr++; ri->tokenType = '('; ri->tokenValue = 0; if (ri->ptr[1] == ')') { ri->ptr++; } return; } else if (ri->ptr[0] == ')') { ri->ptr++; ri->tokenType = ')'; ri->tokenValue = ri->ptr[0]; return; } else if (ri->ptr[0] == '[') { ri->ptr++; ri->tokenType = '['; ri->tokenValue = ri->ptr[0]; if (ri->ptr[1] == ']') { ri->ptr++; } return; } else if (ri->ptr[0] == '|'){ ri->ptr++; ri->tokenType = '|'; ri->tokenValue = 0; return; } else if (ri->ptr[0] == '*'){ ri->ptr++; ri->tokenType = '*'; ri->tokenValue = 0; return; } else if (ri->ptr[0] == '\\'){ // need more proccesing /* \277 \0xa5 \[ \\ \utf-8 etc... */ } else { ri->tokenType = 'a'; ri->tokenValue = ri->ptr[0]; return; } } ri->tokenType = 0; ri->tokenValue = 0; return; } // <regexAtom> ::= <literal>|<charClass>|<group> static NodePtr regexAtom(RegexInfoPtr ri) { token(ri); NodePtr n = NULL; if (ri->tokenType == 'a') n = literal(ri); else if (ri->tokenType == '[') n = charClass(ri); else if (ri->tokenType == '(') n = group(ri); return n; } // <regex> ::= <regexAtom>|<regexAtom>'*'|<regexAtom>'|'<regex>|<regexAtom><regex> NodePtr regex(RegexInfoPtr ri) { NodePtr n = regexAtom(ri); while (ri->ptr[0]) { token(ri); if (ri->tokenType == '*') { n = createNode(ri,'*',n,0); } else if (ri->tokenType == '|') { NodePtr n1 = regex(ri); n = createNode(ri,'|',n,n1); } else if (ri->tokenType == ')') { return n; } else { NodePtr n1 = regex(ri); n = createNode(ri,'+',n,n1); } } return n; }