Mercurial > hg > Members > masakoha > testcode
changeset 89:50a146c05192
add NodeNumber in Regex Parser tree
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 23 Oct 2015 17:20:54 +0900 |
parents | 7809fb223dc9 |
children | d139af3bbd67 |
files | c/regexParser/createRegexTree.cc c/regexParser/printTree.cc c/regexParser/regexParser.h |
diffstat | 3 files changed, 23 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/c/regexParser/createRegexTree.cc Fri Oct 23 15:38:49 2015 +0900 +++ b/c/regexParser/createRegexTree.cc Fri Oct 23 17:20:54 2015 +0900 @@ -1,7 +1,8 @@ #include <stdlib.h> +#include <stdio.h> #include "regexParser.h" -NodePtr createNode(unsigned char,NodePtr,NodePtr); +NodePtr createNode(RegexInfoPtr,unsigned char,NodePtr,NodePtr); NodePtr charClass(RegexInfoPtr); NodePtr group(RegexInfoPtr); void token(RegexInfoPtr); @@ -14,12 +15,20 @@ * regexPosition(state) * stateTransitionTable */ -NodePtr createNode(unsigned char character, NodePtr left, NodePtr right) { +NodePtr createNode(RegexInfoPtr ri,unsigned char character, NodePtr left, NodePtr right) { NodePtr n = (NodePtr)malloc(sizeof(Node)); + n->tokenType = ri->tokenType; n->self = n; n->Value.character = character; n->left = left; n->right = right; + + if (ri->tokenType == 'a') { + static int number = 1; + n->nodeNumber = number; + number++; + ri->tokenType = 0; + } return n; } @@ -39,7 +48,7 @@ // <literal> ::= [a-z][A-Z][0-9] NodePtr literal(RegexInfoPtr ri) { unsigned char *top = ri->ptr; - NodePtr n = createNode(ri->ptr[0],0,0); + NodePtr n = createNode(ri,ri->ptr[0],0,0); ri->ptr++; return n; } @@ -99,7 +108,6 @@ return; } } - ri->tokenType = 0; ri->tokenValue = 0; return; @@ -123,15 +131,15 @@ while (ri->ptr[0]) { token(ri); if (ri->tokenType == '*') { - n = createNode('*',n,0); + n = createNode(ri,'*',n,0); } else if (ri->tokenType == '|') { NodePtr n1 = regex(ri); - n = createNode('|',n,n1); + n = createNode(ri,'|',n,n1); } else if (ri->tokenType == ')') { return n; } else { NodePtr n1 = regex(ri); - n = createNode('+',n,n1); + n = createNode(ri,'+',n,n1); } } return n; }
--- a/c/regexParser/printTree.cc Fri Oct 23 15:38:49 2015 +0900 +++ b/c/regexParser/printTree.cc Fri Oct 23 17:20:54 2015 +0900 @@ -8,7 +8,12 @@ descendTree(n->right); d--; } - printf("%*c%c\n",d*4, ' ',n->Value.character); + if (n->tokenType == 'a') { + printf("%*c(%d)%c\n",d*4, ' ',n->nodeNumber,n->Value.character); + } else { + printf("%*c%c\n",d*4, ' ',n->Value.character); + } + if (n->left != NULL) { d++; descendTree(n->left);
--- a/c/regexParser/regexParser.h Fri Oct 23 15:38:49 2015 +0900 +++ b/c/regexParser/regexParser.h Fri Oct 23 17:20:54 2015 +0900 @@ -13,7 +13,8 @@ } Word, *WordPtr; typedef struct node { - unsigned char type; + unsigned char tokenType; + int nodeNumber; union value { CharClassPtr cc; unsigned char character;