⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lex.c

📁 本工具提供一个词法分析器和语法分析器的集成开发环境
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * lex.c	--	Generate all of the lexical type files: parser.dlg tokens.h * * SOFTWARE RIGHTS * * We reserve no LEGAL rights to the Purdue Compiler Construction Tool * Set (PCCTS) -- PCCTS is in the public domain.  An individual or * company may do whatever they wish with source code distributed with * PCCTS or the code generated by PCCTS, including the incorporation of * PCCTS, or its output, into commerical software. * * We encourage users to develop software with PCCTS.  However, we do ask * that credit is given to us for developing PCCTS.  By "credit", * we mean that if you incorporate our source code into one of your * programs (commercial product, research project, or otherwise) that you * acknowledge this fact somewhere in the documentation, research report, * etc...  If you like PCCTS and have developed a nice tool with the * output, please mention that you developed it using PCCTS.  In * addition, we ask that this header remain intact in our source code. * As long as these guidelines are kept, we expect to continue enhancing * this system and expect to make other tools available as they are * completed. * * ANTLR 1.33 * Terence Parr * Parr Research Corporation * with Purdue University and AHPCRC, University of Minnesota * 1989-1998 */#include <stdio.h>#include <ctype.h>/* MR1						                                                *//* MR1  10-Apr-97  MR1	Replace use of __STDC__ with __USE_PROTOS	    *//* MR1				                                                        */#include "pcctscfg.h"#include "set.h"#include "syn.h"#include "hash.h"#include "generic.h"#define DLGErrorString "invalid token"/* Generate a complete lexical description of the lexemes found in the grammar */void#ifdef __USE_PROTOSgenLexDescr( void )#elsegenLexDescr( )#endif{	ListNode *p;	FILE *dlgFile = fopen(OutMetaName(DlgFileName), "w");	require(dlgFile!=NULL, eMsg1("genLexFile: cannot open %s", OutMetaName(DlgFileName)) );#if SPECIAL_FOPEN	special_fopen_actions(OutMetaName(DlgFileName));	             /* MR1 */#endif	fprintf(dlgFile, "<<\n");	fprintf(dlgFile, "/* %s -- DLG Description of scanner\n", DlgFileName);	fprintf(dlgFile, " *\n");	fprintf(dlgFile, " * Generated from:");	{int i; for (i=0; i<NumFiles; i++) fprintf(dlgFile, " %s", FileStr[i]);}	fprintf(dlgFile, "\n");	fprintf(dlgFile, " *\n");	fprintf(dlgFile, " * Terence Parr, Will Cohen, and Hank Dietz: 1989-1998\n");	fprintf(dlgFile, " * Purdue University Electrical Engineering\n");	fprintf(dlgFile, " * With AHPCRC, University of Minnesota\n");	fprintf(dlgFile, " * ANTLR Version %s\n", Version);	fprintf(dlgFile, " */\n\n");    fprintf(dlgFile, "#define ANTLR_VERSION	%s\n", VersionDef);    if (FirstAction != NULL ) dumpAction( FirstAction, dlgFile, 0, -1, 0, 1 );  /* MR11 */	if ( GenCC )	{		if ( !UserDefdTokens ) fprintf(dlgFile, "#include \"%s\"\n", DefFileName);		else fprintf(dlgFile, "#include %s\n", UserTokenDefsFile);		fprintf(dlgFile, "#include \"%s\"\n", ATOKEN_H);		if ( GenAST ) fprintf(dlgFile, "#include \"%s\"\n", ASTBASE_H);		if ( HdrAction != NULL ) dumpAction( HdrAction, dlgFile, 0, -1, 0, 1 );	}	else	{		fprintf(dlgFile, "#include \"pcctscfg.h\"\n");		fprintf(dlgFile, "#include PCCTS_STDIO_H\n");		if ( strcmp(ParserName, DefaultParserName)!=0 )			fprintf(dlgFile, "#define %s %s\n", DefaultParserName, ParserName);		if ( strcmp(ParserName, DefaultParserName)!=0 )			fprintf(dlgFile, "#include \"%s\"\n", RemapFileName);		if ( HdrAction != NULL ) dumpAction( HdrAction, dlgFile, 0, -1, 0, 1 );		if ( FoundGuessBlk )		{			fprintf(dlgFile, "#define ZZCAN_GUESS\n");			fprintf(dlgFile, "#include PCCTS_SETJMP_H\n");		}		if ( OutputLL_k > 1 ) fprintf(dlgFile, "#define LL_K %d\n", OutputLL_k);		if ( DemandLookahead ) fprintf(dlgFile, "#define DEMAND_LOOK\n");		fprintf(dlgFile, "#include \"antlr.h\"\n");		if ( GenAST ) {			fprintf(dlgFile, "#include \"ast.h\"\n");		}		if ( UserDefdTokens )			fprintf(dlgFile, "#include %s\n", UserTokenDefsFile);		/* still need this one as it has the func prototypes */		fprintf(dlgFile, "#include \"%s\"\n", DefFileName);		fprintf(dlgFile, "#include \"dlgdef.h\"\n");		fprintf(dlgFile, "LOOKAHEAD\n");		fprintf(dlgFile, "void zzerraction()\n");		fprintf(dlgFile, "{\n");		fprintf(dlgFile, "\t(*zzerr)(\"%s\");\n", DLGErrorString);		fprintf(dlgFile, "\tzzadvance();\n");		fprintf(dlgFile, "\tzzskip();\n");		fprintf(dlgFile, "}\n");	}	fprintf(dlgFile, ">>\n\n");	/* dump all actions *//* MR1									                                    *//* MR1  11-Apr-97	Provide mechanism for inserting code into DLG class     *//* MR1	 	   	  via <<%%lexmember ....>> & <<%%lexprefix ...>>            *//* MR1				                					    */          if (LexActions != NULL) {            for (p = LexActions->next; p!=NULL; p=p->next)		{/* MR1 */	fprintf(dlgFile, "<<%%%%lexaction\n");			dumpAction( (char *)p->elem, dlgFile, 0, -1, 0, 1 );			fprintf(dlgFile, ">>\n\n");		}	  };/* MR1 */ if (GenCC) {/* MR1 */   fprintf(dlgFile,"<<%%%%parserclass %s>>\n\n",CurrentClassName);/* MR1 */ };/* MR1 */ if (LexPrefixActions != NULL) {/* MR1 */   for (p = LexPrefixActions->next; p!=NULL; p=p->next)/* MR1 */       {/* MR1 */               fprintf(dlgFile, "<<%%%%lexprefix\n");/* MR1 */               dumpAction( (char *)p->elem, dlgFile, 0, -1, 0, 1 );/* MR1 */               fprintf(dlgFile, ">>\n\n");/* MR1 */       }/* MR1 */ };/* MR1 */ if (LexMemberActions != NULL) {/* MR1 */   for (p = LexMemberActions->next; p!=NULL; p=p->next)/* MR1 */       {/* MR1 */               fprintf(dlgFile, "<<%%%%lexmember\n");/* MR1 */               dumpAction( (char *)p->elem, dlgFile, 0, -1, 0, 1 );/* MR1 */               fprintf(dlgFile, ">>\n\n");/* MR1 */       }/* MR1 */ };	/* dump all regular expression rules/actions (skip sentinel node) */	if ( ExprOrder == NULL ) {		warnNoFL("no regular expressions found in grammar");	}	else dumpLexClasses(dlgFile);	fprintf(dlgFile, "%%%%\n");	fclose( dlgFile );}/* For each lexical class, scan ExprOrder looking for expressions * in that lexical class.  Print out only those that match. * Each element of the ExprOrder list has both an expr and an lclass * field. */void#ifdef __USE_PROTOSdumpLexClasses( FILE *dlgFile )#elsedumpLexClasses( dlgFile )FILE *dlgFile;#endif{	int i;	TermEntry *t;	ListNode *p;	Expr *q;	for (i=0; i<NumLexClasses; i++)	{		fprintf(dlgFile, "\n%%%%%s\n\n", lclass[i].classnum);		for (p=ExprOrder->next; p!=NULL; p=p->next)		{			q = (Expr *) p->elem;			if ( q->lclass != i ) continue;			lexmode(i);			t = (TermEntry *) hash_get(Texpr, q->expr);			require(t!=NULL, eMsg1("genLexDescr: rexpr %s not in hash table",q->expr) );			if ( t->token == EpToken ) continue;			fprintf(dlgFile, "%s\n\t<<\n", StripQuotes(q->expr));			/* replace " killed by StripQuotes() */			q->expr[ strlen(q->expr) ] = '"';			if ( !GenCC ) {				if ( TokenString(t->token) != NULL )					fprintf(dlgFile, "\t\tNLA = %s;\n", TokenString(t->token));				else					fprintf(dlgFile, "\t\tNLA = %d;\n", t->token);			}			if ( t->action != NULL ) dumpAction( t->action, dlgFile, 2,-1,0,1 );			if ( GenCC ) {				if ( TokenString(t->token) != NULL )					fprintf(dlgFile, "\t\treturn %s;\n", TokenString(t->token));				else					fprintf(dlgFile, "\t\treturn (ANTLRTokenType)%d;\n", t->token);			}			fprintf(dlgFile, "\t>>\n\n");		}	}}/* Strip the leading path (if any) from a filename */char *#ifdef __USE_PROTOSStripPath( char *fileName )#elseStripPath( fileName )char *fileName;#endif{	char *p;	static char dirSym[2] = DirectorySymbol;	if(NULL != (p = strrchr(fileName, dirSym[0])))		p++;	else		p = fileName;	return(p);}/* Generate a list of #defines && list of struct definitions for * aggregate retv's */void#ifdef __USE_PROTOSgenDefFile( void )#elsegenDefFile( )#endif{	int i;	/* If C++ mode and #tokdef used, then don't need anything in here since	 * C++ puts all definitions in the class file name.	 */	if ( GenCC && UserTokenDefsFile ) return;    if ( MR_Inhibit_Tokens_h_Gen) return;	DefFile = fopen(OutMetaName(DefFileName), "w");	require(DefFile!=NULL, eMsg1("genDefFile: cannot open %s", OutMetaName(DefFileName)) );#if SPECIAL_FOPEN	special_fopen_actions(OutMetaName(DefFileName));	             /* MR1 */#endif	fprintf(DefFile, "#ifndef %s\n", StripPath(gate_symbol(DefFileName)));	fprintf(DefFile, "#define %s\n", StripPath(gate_symbol(DefFileName)));	fprintf(DefFile, "/* %s -- List of labelled tokens and stuff\n", DefFileName);	fprintf(DefFile, " *\n");	fprintf(DefFile, " * Generated from:");	for (i=0; i<NumFiles; i++) fprintf(DefFile, " %s", FileStr[i]);	fprintf(DefFile, "\n");	fprintf(DefFile, " *\n");	fprintf(DefFile, " * Terence Parr, Will Cohen, and Hank Dietz: 1989-1998\n");	fprintf(DefFile, " * Purdue University Electrical Engineering\n");	fprintf(DefFile, " * ANTLR Version %s\n", Version);	fprintf(DefFile, " */\n");	if ( !GenCC && LexGen ) {		fprintf(DefFile,"#define zzEOF_TOKEN %d\n",				TokenInd!=NULL?TokenInd[EofToken]:EofToken);	}	if ( !UserDefdTokens )	{		int first=1;		if ( GenCC ) fprintf(DefFile, "enum ANTLRTokenType {\n");		for (i=1; i<TokenNum; i++)		{			/* Don't do EpToken or expr w/o labels */			if ( TokenString(i)!=NULL && i != EpToken )			{				TermEntry *p;								if ( WarningLevel>1 )				{					int j;					/* look in all lexclasses for the reg expr *//* MR10  Derek Pappas                                                *//* MR10     A #tokclass doesn't have associated regular expressiones *//* MR10        so don't warn user about it's omission                */                    p = (TermEntry *) hash_get(Tname, TokenString(i));                    if (p != NULL && ! p->classname) {    					for (j=0; j<NumLexClasses; j++)    					{    						lexmode(j);    						if ( ExprString(i)!=NULL ) break;    					}    					if ( j>=NumLexClasses )    					{    						warnNoFL(eMsg1("token label has no associated rexpr: %s",TokenString(i)));    					}                    };				}				require((p=(TermEntry *)hash_get(Tname, TokenString(i))) != NULL,						"token not in sym tab when it should be");				if ( !p->classname )				{					if ( GenCC ) {						if ( !first ) fprintf(DefFile, ",\n");						first = 0;						fprintf(DefFile, "\t%s=%d", TokenString(i), i);					}					else						fprintf(DefFile, "#define %s %d\n", TokenString(i), i);				}			}		}/* MR1									                                    *//* MR1  10-Apr-97 133MR1	Prevent use of varying sizes of integer	    *//* MR1				for the enum ANTLRTokenType                             *//* MR1								           */		if ( GenCC ) {				                                 /* MR1 */ 		 	 if ( !first ) fprintf(DefFile, ",\n");                  /* MR14 */		     fprintf(DefFile, "\tDLGminToken=0");                    /* MR1 */		     fprintf(DefFile, ",\n\tDLGmaxToken=9999};\n");          /* MR1 */                };						                             /* MR1 */	}	if ( !GenCC ) GenRulePrototypes(DefFile, SynDiag);	fprintf(DefFile, "\n#endif\n");}void#ifdef __USE_PROTOSGenRemapFile( void )#elseGenRemapFile( )#endif{	if ( strcmp(ParserName, DefaultParserName)!=0 )	{		FILE *f;		int i;		f = fopen(OutMetaName(RemapFileName), "w");		require(f!=NULL, eMsg1("GenRemapFile: cannot open %s", OutMetaName(RemapFileName)) );#ifdef SPECIAL_FOPEN		special_fopen_actions(OutMetaName(RemapFileName));           /* MR1 */#endif		fprintf(f, "/* %s -- List of symbols to remap\n", RemapFileName);		fprintf(f, " *\n");		fprintf(f, " * Generated from:");		for (i=0; i<NumFiles; i++) fprintf(f, " %s", FileStr[i]);		fprintf(f, "\n");		fprintf(f, " *\n");		fprintf(f, " * Terence Parr, Will Cohen, and Hank Dietz: 1989-1998\n");		fprintf(f, " * Purdue University Electrical Engineering\n");		fprintf(f, " * ANTLR Version %s\n", Version);		fprintf(f, " */\n");		GenRuleFuncRedefs(f, SynDiag);		GenPredefinedSymbolRedefs(f);		if ( GenAST ) GenASTSymbolRedefs(f);		GenSetRedefs(f);		fclose(f);	}}/* Generate a bunch of #defines that rename all functions to be "ParserName_func" */void#ifdef __USE_PROTOSGenRuleFuncRedefs( FILE *f, Junction *p )#elseGenRuleFuncRedefs( f, p )FILE *f;Junction *p;#endif{	fprintf(f, "\n/* rename rule functions to be 'ParserName_func' */\n");	while ( p!=NULL )	{		fprintf(f, "#define %s %s_%s\n", p->rname, ParserName, p->rname);		p = (Junction *)p->p2;	}}/* Generate a bunch of #defines that rename all standard symbols to be * "ParserName_symbol".  The list of standard symbols to change is in * globals.c. */void#ifdef __USE_PROTOSGenPredefinedSymbolRedefs( FILE *f )#elseGenPredefinedSymbolRedefs( f )FILE *f;#endif{	char **p;	fprintf(f, "\n/* rename PCCTS-supplied symbols to be 'ParserName_symbol' */\n");	for (p = &StandardSymbols[0]; *p!=NULL; p++)	{		fprintf(f, "#define %s %s_%s\n", *p, ParserName, *p);	}}/* Generate a bunch of #defines that rename all AST symbols to be * "ParserName_symbol".  The list of AST symbols to change is in * globals.c. */void#ifdef __USE_PROTOSGenASTSymbolRedefs( FILE *f )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -