User:Antigng-bot/regex/output

#include <stdio.h>
#include "mem.h"
#include "regexFrontEnd.h"
#include "NFARuntime.h"
#include "DFAProcess.h"
const char *outputName=NULL;
const char *outputDec=NULL;
const char *outputGetStat=NULL;
const char *outputJudgeStat=NULL;
const char *outputForwardStat=NULL;
int debug=0;
static void outputHead(FILE *fp,int start)
{
	fprintf(fp,
		"int %s\n"
		"{\n"
		"\tint state=%d;\n"
		"\tunsigned int ch=0;\n"
		"%s""%s""%s",outputName,start,outputDec?"\t":"",outputDec?outputDec:"",outputDec?";\n":"");
	fprintf(fp,
		"\t%s;\n"
		"\twhile(%s)\n"
		"\t{\n"
		"\t\tswitch(state)\n"
		"\t\t{\n",outputGetStat,outputJudgeStat);
	return;
}
static void outputDFA(FILE *fp,int oDFATop,struct _DFANode *oDFAStates)
{
	int count=0;
	struct _DFACharChain *action;
	for(count=0;count<oDFATop;count++)
	{
		fprintf(fp,"\t\tcase %d:\n",count);
		fprintf(fp,
			"\t\t\tswitch(ch)\n"
			"\t\t\t{\n");
		action=oDFAStates[count].actionlist;
		while(action)
		{
			if(action->state!=count)
			{
				if((action->ch=='\'')||(action->ch=='\\'))
				{
					fprintf(fp,
						"\t\t\tcase '\\%c':\n"
						"\t\t\t\tstate=%d;\n"
						"\t\t\t\tbreak;\n",action->ch,action->state);
				}
				else if((action->ch>=32)&&(action->ch<=127))
				{
					fprintf(fp,
						"\t\t\tcase '%c':\n"
						"\t\t\t\tstate=%d;\n"
						"\t\t\t\tbreak;\n",action->ch,action->state);
				}
				else
				{
					fprintf(fp,
						"\t\t\tcase %d:\n"
						"\t\t\t\tstate=%d;\n"
						"\t\t\t\tbreak;\n",action->ch,action->state);
				}
			}
			else
			{
				if((action->ch=='\'')||(action->ch=='\\'))
				{
					fprintf(fp,
						"\t\t\tcase '\\%c':\n"
						"\t\t\t\tbreak;\n",action->ch);
				}
				else if((action->ch>=32)&&(action->ch<=127))
				{
					fprintf(fp,
						"\t\t\tcase '%c':\n"
						"\t\t\t\tbreak;\n",action->ch);
				}
				else
				{
					fprintf(fp,
						"\t\t\tcase %d:\n"
						"\t\t\t\tbreak;\n",action->ch);
				}
			}
			action=action->next;
		}
		fprintf(fp,
			"\t\t\tdefault:\n"
			"\t\t\t\treturn 0;\n"
			"\t\t\t}\n"
			"\t\t\tbreak;\n");
	}
	fprintf(fp,
		"\t\t}\n"
		"\t\t%s;\n"
		"\t\t%s;\n"
		"\t}\n",outputForwardStat,outputGetStat);
	return;
}
static void outputEndStates(FILE *fp,int oDFATop,struct _DFANode *oDFAStates)
{
	int count=0;
	fprintf(fp,
		"\tswitch(state)\n"
		"\t{\n");
	for(;count<oDFATop;count++)
	{
		if(oDFAStates[count].type)
		{
			fprintf(fp,
				"\tcase %d:\n"
				"\t\treturn 1;\n"
				"\t\tbreak;\n",count);				
		}
	}
	fprintf(fp,
		"\t}\n"
		"\treturn 0;\n"
		"}\n");
	return;

}
static void outputHead_dirty(FILE *fp,int start)
{
	fprintf(fp,
		"int %s\n"
		"{\n"
		"\tunsigned int ch=0;\n"
		"%s""%s""%s",outputName,outputDec?"\t":"",outputDec?outputDec:"",outputDec?";\n":"");
	fprintf(fp,
		"\tgoto _DFAState_%d;\n",start);
	return;
}
int *outputStatesJumpedTo=NULL;
static void outputDFA_dirty(FILE *fp,int oDFATop,struct _DFANode *oDFAStates,int ini_state)
{
	int count=0;
	struct _DFACharChain *action;
	for(count=0;count<oDFATop;count++)
	{
		if(outputStatesJumpedTo[count])
		{
			fprintf(fp,"_DFAState_%d:\n",count);
		}
		else if(debug)
		{
			fprintf(fp,"/*_DFAState_%d: unused*/\n",count);
		}
		if(count!=ini_state)
		{
			fprintf(fp,
				"\t%s;\n",outputForwardStat);
		}
		fprintf(fp,
			"\t%s;\n"
			"\tif(!(%s)) return %d;\n",outputGetStat,outputJudgeStat,oDFAStates[count].type?1:0);
		fprintf(fp,
			"\tswitch(ch)\n"
			"\t{\n");
		action=oDFAStates[count].actionlist;
		while(action)
		{
			fprintf(fp,
				"\tcase %d:\n",action->ch);
			if(action->state==ini_state)
			{
				fprintf(fp,
					"\t\t%s;\n",outputForwardStat);
			}
			if(action->state==count+1)
			{
				if(debug)
				{
					fprintf(fp,
							"\t\t/*Fall through to state %d.*/\n",action->state);
				}
				fprintf(fp,
					"\t\tbreak;\n");
			}
			else
			{
				fprintf(fp,
						"\t\tgoto _DFAState_%d;\n"
						"\t\tbreak;\n",action->state);
			}
			action=action->next;
		}
		fprintf(fp,
			"\tdefault:\n"
			"\t\treturn 0;\n"
			"\t}\n");
	}
	return;
}
static void outputEndStates_dirty(FILE *fp)
{
	fprintf(fp,
		"}");
	s_free(outputStatesJumpedTo);
	return;
}
static void outputCheckStates(int oDFATop,struct _DFANode *oDFAStates,int ini_state)
{
	int count=0;
	struct _DFACharChain *action;
	outputStatesJumpedTo=(int *)s_calloc(sizeof(int),oDFATop);
	outputStatesJumpedTo[ini_state]=1;
	for(count=0;count<oDFATop;count++)
	{
		action=oDFAStates[count].actionlist;
		while(action)
		{
			if(action->state!=count+1)
			{
				outputStatesJumpedTo[action->state]=1;
			}
			action=action->next;
		}
	}
	return;
}

void outputRegexLexer(FILE *fp,int oDFATop,struct _DFANode *oDFAStates,int start)
{
	outputHead(fp,start);
	outputDFA(fp,oDFATop,oDFAStates);
	outputEndStates(fp,oDFATop,oDFAStates);
	return;
}

void outputRegexLexer_dirty(FILE *fp,int oDFATop,struct _DFANode *oDFAStates,int start)
{
	outputCheckStates(oDFATop,oDFAStates,start);
	outputHead_dirty(fp,start);
	outputDFA_dirty(fp,oDFATop,oDFAStates,start);
	outputEndStates_dirty(fp);
	return;
}