Mercurial > hg > Applications > Grep
view c/regexParser/createBitVectorList.cc @ 109:6401c708f5dd impl-bitvector
fix printBitVectorList (print Loop when include asterisk node)
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 20 Nov 2015 15:19:09 +0900 |
parents | 70069d4647a0 |
children |
line wrap: on
line source
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include "bitVector.h" #include "regexParser.h" extern BitVectorPtr bitSet(int); extern void bitPrint(BitVectorPtr); BitVectorListPtr createBitVector(NodePtr); BitVectorListPtr descendTreeNode(NodePtr, BitVectorListPtr, BitVectorListPtr, bool&); BitVectorListPtr setNextBitVectorList(unsigned char, BitVectorListPtr, BitVectorListPtr); BitVectorListPtr initBvl; BitVectorListPtr allocateBitVectorList() { BitVectorListPtr bvl = (BitVectorListPtr)malloc(sizeof(BitVectorList)); if (bvl == NULL) { fprintf(stderr, "Failed to allocate memory.\n"); exit(-1); } bvl->self = bvl; bvl->bi = (BitVectorPtr)malloc(sizeof(BitVector)); if (bvl->bi == NULL) { fprintf(stderr, "Failed to allocate memory.\n"); exit(-1); } return bvl; } BitVectorListPtr createBitVector(NodePtr n,BitVectorListPtr bvl) { BitVectorListPtr nextBvl = allocateBitVectorList(); nextBvl->bi = bitSet(n->nodeNumber); nextBvl->initBvl = initBvl; return nextBvl; } BitVectorListPtr initBitVector() { BitVectorListPtr bvl = allocateBitVectorList(); bvl->initBvl = initBvl = bvl; bvl->bi = bitSet(0); for (int i = 0; i < 256; i++) { bvl->next[i] = NULL; } return bvl; } void printBitVectorList (BitVectorListPtr bvl) { bool isFirstPrint = true; for (int i = 0; i < 256; i++) { if (bvl->next[i] != NULL) { if (isFirstPrint){ puts("----"); printf(" state : "); bitPrint(bvl->bi); isFirstPrint = false; } printf("input char : %c\n",i); printf("next state : ");bitPrint(bvl->next[i]->bi); bvl->isLoop = bvl->isLoopAnker; } } for (int i = 0; i < 256; i++) { if ((bvl->next[i] != NULL) && !(bvl->isLoop && bvl->isLoopAnker)) { // if ((bvl->next[i] != NULL) ) { printBitVectorList(bvl->next[i]); } } } BitVectorListPtr descendTreeNode(NodePtr n,BitVectorListPtr bvl, BitVectorListPtr prev, bool &fromOr, bool &fromAsterisk) { bool leftIsOr, rightIsOr; if (n->Value.character == '*') { bvl = descendTreeNode(n->left, bvl, prev, leftIsOr, fromAsterisk); unsigned char repertChar = 0; for (int i = 0; i < 256; i++) { if (prev->next[i] != NULL) repertChar = i; } setNextBitVectorList(repertChar, bvl, prev->next[repertChar]); // here bvl->isLoopAnker = true; fromAsterisk = true; return prev; } else if (n->Value.character == '|') { bvl = descendTreeNode(n->left, bvl, prev, leftIsOr, fromAsterisk); setNextBitVectorList(n->left->Value.character, prev, bvl); bvl = descendTreeNode(n->right, bvl, prev, rightIsOr, fromAsterisk); setNextBitVectorList(n->right->Value.character, prev, bvl); fromOr = true; return prev; } else if (n->Value.character == '+') { bvl = descendTreeNode(n->left, bvl, prev, leftIsOr, fromAsterisk); setNextBitVectorList(n->left->Value.character, prev, bvl); prev = bvl; bvl = descendTreeNode(n->right, bvl, prev, rightIsOr, fromAsterisk); if (leftIsOr){ for (int i = 0; i < 256; i++) if (prev->next[i] != NULL) setNextBitVectorList(n->right->Value.character, prev->next[i], bvl); } else { setNextBitVectorList(n->right->Value.character, prev, bvl); } fromOr = false; } else if (n->tokenType == 'a') { bvl = createBitVector(n,bvl); fromOr = false; } return bvl; } BitVectorListPtr setNextBitVectorList(unsigned char inputChar, BitVectorListPtr bvl, BitVectorListPtr next){ if (isalnum((int)inputChar)){ bvl->next[(int)inputChar] = next; } return next; } BitVectorListPtr createBitVectorList(NodePtr n) { BitVectorListPtr bvl = initBitVector(); bool fromOr = false; bool fromAsterisk = false; descendTreeNode(n, bvl, bvl, fromOr, fromAsterisk); printBitVectorList(bvl); return bvl; }