Exemplo n.º 1
0
bool
ZnkRgxInfo_setup( ZnkRgxInfo info, const char* regexp_str, const ZnkRgxEncode enc, ZnkStr rep_msg )
{
	bool result = false;
	result = initNFA( info->nfa_ary_, &info->nfa_entry_, &info->nfa_exit_,
			regexp_str, enc->func_get_, rep_msg );
	if( !result ){
		return false;
	}
	info->func_get_       = enc->func_get_;
	info->func_increment_ = enc->func_increment_;

	if( st_verbose ){
		/* 生成したNFAの内容を表示する */
		ZnkRgxNFATable_dump( info->nfa_ary_, info->nfa_entry_, info->nfa_exit_ );
	}

	ZnkRgxDFAStateAry_clear( info->dfa_ary_ );
	/* NFAからDFAを生成する */
	info->initial_dfa_state_ = ZnkRgxDFAState_convertFromNFA( info->dfa_ary_,
			info->nfa_ary_, info->nfa_entry_, info->nfa_exit_ );
	return true;
}
nfa* createNFA(char *regex){
	//printf("Creating NFA for |%s|\n",regex);
	int len=strlen(regex),i=0,j=0,temp;
	nfa* automata=initNFA(getStateCount(regex));
	while(i<automata->nStates && j<len){
		//printf("Innitialising state #%d\n",i);
		automata->states[i]=initQ();
		
		//printf("Reading regex at #%d\n",j);
		if(regex[j]!='['){
			//printf("Enqeueing terminal %c to state %d(line 51)\n",regex[j],i);
			enqueue(automata->states[i],regex[j]-0);
		}else{
			//printf("Found segment start\n");
			j++;
			if(regex[j]!='('){
				//printf("Enqeueing terminal %c to state %d(line 57)\n",regex[j],i);
				enqueue(automata->states[i],regex[j]-0);
			}
			j++;
			while(regex[j]!=']'){
				if(regex[j]!='(' && regex[j]!=')' && regex[j]!='-'){
					//printf("Enqeueing terminal %c to state %d(line63)\n",regex[j],i);
					enqueue(automata->states[i],regex[j]-0);
				}
				if(regex[j]=='-'){
					//printf("Found range marker\n");
					temp=getBack(automata->states[i])+1;
					while(temp!=regex[j+1]+1){
						//printf("Enqeueing terminal %c to state %d(line 70)\n",temp,i);
						enqueue(automata->states[i],temp);
						temp++;
					}
					j++;
				}
				j++;
			}
		}
		if(j<len-1 && regex[j+1]=='*' && i<automata->nStates){
			//printf("Found kleene star\n");
			automata->quants[i]=STAR;
			j++;
		}
		else if(j<len-1 && regex[j+1]=='+' && i<automata->nStates){
			//printf("Found plus\n");
			automata->quants[i]=PLUS;
			j++;
		}else if(j<len-1 && regex[j+1]=='|' && i<automata->nStates){
			//printf("Found or\n");
			automata->quants[i]=OR;
			j++;
		}
		else if(i<automata->nStates){
			//printf("No quantifiers found\n");
			automata->quants[i]=NONE;
		}
		j++;
		if(j<len){
			i++;
			//printf("Creating state #%d\n",i); 
		}
	}
	automata->nStates=i+1;
	//printf("\n\n");
	return automata;
}