跳转到内容

用户:Antigng-bot/regex/regexLexerGenerate

维基百科,自由的百科全书
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "convert.h"
#include "regexFrontEnd.h"
#include "NFARuntime.h"
#include "DFAProcess.h"
#include "DFAMinimize.h"
#include "DFAOptimize.h"
#include "output.h"

unsigned int utf8[8192]={0};
FILE *outfile=NULL;
static void displayerr(unsigned int code)
{
	if(!(code&0x1))
	{
		fprintf(stderr,
			"\tNo input file.\n"
			"\t\tA valid input file must be specified via \"-i\".\n"
			);
	}
	if(code&0x2)
	{
		fprintf(stderr,
			"\tInvalid input file.\n"
			"\t\tCannot open the specified input file.\n"
			);
	}
	if(code&0x4)
	{
		fprintf(stderr,
			"\tInvalid content.\n"
			"\t\tThe input file must contain a single line with length between 0 and 8192, encoded in utf-8.\n"
			);
	}
	if(!(code&0x8))
	{
		fprintf(stderr,
			"\tNo function name.\n"
			"\t\tA function name must be specified via \"-n\".\n"
			);
	}
	if(!(code&0x10))
	{
		fprintf(stderr,
			"\tNo \"get\" statement.\n"
			"\t\tThe way to get a char must be specified via \"-g\".\n"
			);
	}
	if(!(code&0x20))
	{
		fprintf(stderr,
			"\tNo \"judge\" statement.\n"
			"\t\tThe statement giving an end of the input must be set via \"-j\".\n"
			);
	}
	if(!(code&0x40))
	{
		fprintf(stderr,
			"\tNo \"forward\" statement.\n"
			"\t\tThe statement moving the pointer forward must be set via \"-f\".\n"
			);
	}
	if(code&0x80)
	{
		fprintf(stderr,
			"\tInvalid output file.\n"
			"\t\tCannot open the specified output file.\n");
	}
	fprintf(stderr,"\n");
	return;
}
static int parsearg(int argc,const char *argv[])
{
	int cur_arg=0;
	unsigned int err=0;
	FILE *infile=NULL;
	for(cur_arg=1;cur_arg<argc;cur_arg++)
	{
		if(argv[cur_arg][0]=='-'&&((argv[cur_arg+1]&&argv[cur_arg+1][0]!='-')))
		{
			switch(argv[cur_arg][1])
			{
			case 'i':
				infile=fopen(argv[cur_arg+1],"r+");
				if(!infile)
				{
					err|=0x2;
				}
				else
				{
					char line[8192];
					fgets(line,8190,infile);
					fclose(infile);
					if(!line[0]||(strlen(line)>=8190)||utf8tounicode(line,utf8)<=0)
					{
						err|=0x4;
					}
					else
					{
						err|=0x1;
					}
				}
				cur_arg++;
				break;
			case 'n':
				outputName=argv[cur_arg+1];
				err|=0x8;
				cur_arg++;
				break;
			case 'd':
				outputDec=argv[cur_arg+1];
				cur_arg++;
				break;
			case 'g':
				outputGetStat=argv[cur_arg+1];
				err|=0x10;
				cur_arg++;
				break;
			case 'j':
				outputJudgeStat=argv[cur_arg+1];
				err|=0x20;
				cur_arg++;
				break;
			case 'f':
				outputForwardStat=argv[cur_arg+1];
				err|=0x40;
				cur_arg++;
				break;
			case 'b':
				debug=atoi(argv[cur_arg+1]);
				break;
			case 'o':
				outfile=fopen(argv[cur_arg+1],"w+");
				if(!outfile)
				{
					err|=0x80;
				}
				cur_arg++;
				break;
			}
		}
	}
	if(0x1+0x8+0x10+0x20+0x40==err)
	{
		return 0;
	}
	else
	{
		fprintf(stderr,"Error code 0x%x:\n",err);
		displayerr(err);
		return 1;
	}
}

int main(int argc,const char *argv[])
{
	if(parsearg(argc,argv))
	{
		fprintf(stderr,"Usage: %s "
			"-i \"input file\" "
			"-n \"function name\" "
			"-g \"get statement\" "
			"-j \"judge statement\" "
			"-f \"forward statement\" "
			"[-o \"output file\" -d \"additional declaration\" -b <debug level>]\n",argv[0]);
		return -1;
	}
	if(!regexPatternLexer(utf8))
	{
		if(!ConvertNFAToDFA(NFARes))
		{
			FILE *fp;
			int ini_state;
			ini_state=DFAMinimize();
			if(outfile)
			{
				fp=outfile;
			}
			else fp=stdout;
			if(debug>=3||debug<0)
			{
				outputRegexLexer(fp,MDFATop,MDFAStates,ini_state);
			}
			else if(debug==2)
			{
				outputRegexLexer_dirty(fp,MDFATop,MDFAStates,ini_state);
			}
			else
			{
				DFAOptimize(ini_state);
				outputRegexLexer_dirty(fp,ODFATop,ODFAStates,0);
			}
			if(outfile) fclose(outfile);
			return 0;
		}
		else return -3;
	}
	else return -2;
}