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

📄 antlr.c

📁 本工具提供一个词法分析器和语法分析器的集成开发环境
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * A n t l r  T r a n s l a t i o n  H e a d e r * * Terence Parr, Will Cohen, and Hank Dietz: 1989-1998 * Purdue University Electrical Engineering * With AHPCRC, University of Minnesota * ANTLR Version 1.33MR14 * *   ../bin/antlr -gh antlr.g * */#define ANTLR_VERSION	13314#include "pcctscfg.h"#include PCCTS_STDIO_H#include "set.h"#include <ctype.h>#include "syn.h"#include "hash.h"#include "generic.h"#define zzcr_attr(attr,tok,t)#define zzSET_SIZE 20#include "antlr.h"#include "tokens.h"#include "dlgdef.h"#include "mode.h"#ifndef PURIFY#define PURIFY(r,s) memset((char *) &(r),'\0',(s));#endifANTLR_INFO#ifdef __USE_PROTOSstatic void chkToken(char *, char *, char *, int);#elsestatic void chkToken();#endif#ifdef __USE_PROTOSstatic int isDLGmaxToken(char *Token);				     /* MR3 */#elsestatic int isDLGmaxToken();				                             /* MR3 */#endifstatic int class_nest_level = 0;extern int inAlt;extern set attribsRefdFromAction;extern int UsedOldStyleAttrib;extern int UsedNewStyleLabel;void#ifdef __USE_PROTOSgrammar(void)#elsegrammar()#endif{	zzRULE;	zzBLOCK(zztasp1);	zzMake0;	{	Graph g;	{		zzBLOCK(zztasp2);		zzMake0;		{		while ( 1 ) {			if ( !((setwd1[LA(1)]&0x1))) break;			if ( (LA(1)==92) ) {				zzmatch(92); zzCONSUME;				zzmatch(Action);								if ( HdrAction==NULL ) {					HdrAction = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));					require(HdrAction!=NULL, "rule grammar: cannot allocate header action");					strcpy(HdrAction, LATEXT(1));				}				else warn("additional #header statement ignored"); zzCONSUME;			}			else {				if ( (LA(1)==93) ) {					zzmatch(93); zzCONSUME;					zzmatch(Action);										if ( FirstAction==NULL ) {						FirstAction = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));						require(FirstAction!=NULL, "rule grammar: cannot allocate #first action");						strcpy(FirstAction, LATEXT(1));					} else {						warn("additional #first statement ignored");					}; zzCONSUME;				}				else {					if ( (LA(1)==94) ) {						zzmatch(94); zzCONSUME;						zzmatch(QuotedTerm);												if ( GenCC ) {							warn("#parser meta-op incompatible with -CC; ignored");						}						else {							if ( strcmp(ParserName,"zzparser")==0 ) {								ParserName=StripQuotes(mystrdup(LATEXT(1)));								if ( RulePrefix[0]!='\0' )								{									warn("#parser meta-op incompatible with '-gp prefix'; '-gp' ignored");									RulePrefix[0]='\0';								}							}							else warn("additional #parser statement ignored");						} zzCONSUME;					}					else {						if ( (LA(1)==95) ) {							zzmatch(95); zzCONSUME;							zzmatch(QuotedTerm);							{								char *fname;								zzantlr_state st; FILE *f; struct zzdlg_state dst;								UserTokenDefsFile = mystrdup(LATEXT(1));								zzsave_antlr_state(&st);								zzsave_dlg_state(&dst);								fname = mystrdup(LATEXT(1));								f = fopen(StripQuotes(fname), "r");								if ( f==NULL ) {warn(eMsg1("cannot open token defs file '%s'", fname+1));}								else {									ANTLRm(enum_file(fname+1), f, PARSE_ENUM_FILE);									UserDefdTokens = 1;								}								zzrestore_antlr_state(&st);								zzrestore_dlg_state(&dst);							} zzCONSUME;						}						else break; /* MR6 code for exiting loop "for sure" */					}				}			}			zzLOOP(zztasp2);		}		zzEXIT(zztasp2);		}	}	{		zzBLOCK(zztasp2);		zzMake0;		{		while ( 1 ) {			if ( !((setwd1[LA(1)]&0x2))) break;			if ( (LA(1)==Action) ) {				zzmatch(Action);				{					UserAction *ua = newUserAction(LATEXT(1));					ua->file = action_file; ua->line = action_line;					if ( class_nest_level>0 ) list_add(&class_before_actions, ua);					else list_add(&BeforeActions, ua);				} zzCONSUME;			}			else {				if ( (LA(1)==106) ) {					laction();				}				else {					if ( (LA(1)==107) ) {						lmember();					}					else {						if ( (LA(1)==108) ) {							lprefix();						}						else {							if ( (LA(1)==114) ) {								aLexclass();							}							else {								if ( (LA(1)==118) ) {									token();								}								else {									if ( (LA(1)==115) ) {										error();									}									else {										if ( (LA(1)==116) ) {											tclass();										}										else {											if ( (LA(1)==109) ) {												aPred();											}											else {												if ( (LA(1)==132) ) {													default_exception_handler();												}												else {													if ( (LA(1)==97) ) {														class_def();													}													else {														if ( (LA(1)==96) ) {															zzmatch(96);																														if ( class_nest_level==0 )															warn("missing class definition for trailing '}'");															class_nest_level--; zzCONSUME;														}														else break; /* MR6 code for exiting loop "for sure" */													}												}											}										}									}								}							}						}					}				}			}			zzLOOP(zztasp2);		}		zzEXIT(zztasp2);		}	}	rule();	g=zzaArg(zztasp1,3); SynDiag = (Junction *) zzaArg(zztasp1,3 ).left;	{		zzBLOCK(zztasp2);		zzMake0;		{		while ( 1 ) {			if ( !((setwd1[LA(1)]&0x4))) break;			if ( (LA(1)==NonTerminal) ) {				rule();				if ( zzaArg(zztasp2,1 ).left!=NULL ) {g.right = NULL; g = Or(g, zzaArg(zztasp2,1));}			}			else {				if ( (LA(1)==114) ) {					aLexclass();				}				else {					if ( (LA(1)==118) ) {						token();					}					else {						if ( (LA(1)==115) ) {							error();						}						else {							if ( (LA(1)==116) ) {								tclass();							}							else {								if ( (LA(1)==109) ) {									aPred();								}								else {									if ( (LA(1)==97) ) {										class_def();									}									else {										if ( (LA(1)==96) ) {											zzmatch(96);																						if ( class_nest_level==0 )											warn("missing class definition for trailing '}'");											class_nest_level--; zzCONSUME;										}										else break; /* MR6 code for exiting loop "for sure" */									}								}							}						}					}				}			}			zzLOOP(zztasp2);		}		zzEXIT(zztasp2);		}	}	{		zzBLOCK(zztasp2);		zzMake0;		{		while ( 1 ) {			if ( !((setwd1[LA(1)]&0x8))) break;			if ( (LA(1)==Action) ) {				zzmatch(Action);				{					UserAction *ua = newUserAction(LATEXT(1));					ua->file = action_file; ua->line = action_line;					if ( class_nest_level>0 ) list_add(&class_after_actions, ua);					else list_add(&AfterActions, ua);				} zzCONSUME;			}			else {				if ( (LA(1)==106) ) {					laction();				}				else {					if ( (LA(1)==107) ) {						lmember();					}					else {						if ( (LA(1)==108) ) {							lprefix();						}						else {							if ( (LA(1)==115) ) {								error();							}							else {								if ( (LA(1)==116) ) {									tclass();								}								else {									if ( (LA(1)==97) ) {										class_def();									}									else {										if ( (LA(1)==109) ) {											aPred();										}										else {											if ( (LA(1)==96) ) {												zzmatch(96);																								if ( class_nest_level==0 )												warn("missing class definition for trailing '}'");												class_nest_level--; zzCONSUME;											}											else break; /* MR6 code for exiting loop "for sure" */										}									}								}							}						}					}				}			}			zzLOOP(zztasp2);		}		zzEXIT(zztasp2);		}	}	zzmatch(Eof); zzCONSUME;	zzEXIT(zztasp1);	return;fail:	zzEXIT(zztasp1);	CannotContinue=TRUE;	zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);	zzresynch(setwd1, 0x10);	}}void#ifdef __USE_PROTOSclass_def(void)#elseclass_def()#endif{	zzRULE;	zzBLOCK(zztasp1);	zzMake0;	{	int go=1; char name[MaxRuleName+1];	zzmatch(97); zzCONSUME;	{		zzBLOCK(zztasp2);		zzMake0;		{		if ( (LA(1)==NonTerminal) ) {			zzmatch(NonTerminal);			if(go) strncpy(name,LATEXT(1),MaxRuleName); zzCONSUME;		}		else {			if ( (LA(1)==TokenTerm) ) {				zzmatch(TokenTerm);				if(go) strncpy(name,LATEXT(1),MaxRuleName); zzCONSUME;			}			else {zzFAIL(1,zzerr1,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText,&zzErrk); goto fail;}		}		zzEXIT(zztasp2);		}	}		if ( CurrentClassName[0]!='\0' && strcmp(CurrentClassName,name)!=0	&& GenCC ) {		err("only one grammar class allowed in this release");		go = 0;	}	else strcpy(CurrentClassName, name);	if ( !GenCC ) { err("class meta-op used without C++ option"); }	{		zzBLOCK(zztasp2);		zzMake0;		{		while ( (setwd1[LA(1)]&0x20) ) {			zzsetmatch(zzerr2);			if (ClassDeclStuff == NULL) {				/* MR10 */                   ClassDeclStuff=(char *)calloc(MaxClassDeclStuff+1,sizeof(char));				/* MR10 */              };			/* MR10 */              strncat(ClassDeclStuff," ",MaxClassDeclStuff);			/* MR10 */              strncat(ClassDeclStuff,LATEXT(1),MaxClassDeclStuff);			/* MR10 */ zzCONSUME;			zzLOOP(zztasp2);		}		zzEXIT(zztasp2);		}	}	zzmatch(100);		no_classes_found = 0;	if ( class_nest_level>=1 ) {warn("cannot have nested classes");}	else class_nest_level++; zzCONSUME;	zzEXIT(zztasp1);	return;fail:	zzEXIT(zztasp1);	CannotContinue=TRUE;	zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);	zzresynch(setwd1, 0x40);	}}void#ifdef __USE_PROTOSrule(void)#elserule()#endif{	zzRULE;	zzBLOCK(zztasp1);	zzMake0;	{		ListNode *ex_groups = NULL;	ExceptionGroup *eg;	RuleEntry *q; Junction *p; Graph r; int f, l; ECnode *e;	set toksrefd, rulesrefd;	char *pdecl=NULL, *ret=NULL, *a; CurRetDef = CurParmDef = NULL;	CurExGroups = NULL;	CurElementLabels = NULL;	/* We want a new element label hash table for each rule */	if ( Elabel!=NULL ) killHashTable(Elabel);	Elabel = newHashTable();	attribsRefdFromAction = empty;	zzmatch(NonTerminal);	q=NULL;	if ( hash_get(Rname, LATEXT(1))!=NULL ) {		err(eMsg1("duplicate rule definition: '%s'",LATEXT(1)));		CannotContinue=TRUE;	}	else	{		q = (RuleEntry *)hash_add(Rname,		LATEXT(1),		(Entry *)newRuleEntry(LATEXT(1)));		CurRule = q->str;	}	CurRuleNode = q;	f = CurFile; l = zzline;	NumRules++; zzCONSUME;	{		zzBLOCK(zztasp2);		zzMake0;		{		if ( (LA(1)==101) ) {			zzmatch(101);			if ( q!=NULL ) q->noAST = TRUE; zzCONSUME;		}		zzEXIT(zztasp2);		}	}	{		zzBLOCK(zztasp2);		zzMake0;		{		;		if ( (setwd1[LA(1)]&0x80) ) {			{				zzBLOCK(zztasp3);				zzMake0;				{				if ( (LA(1)==102) ) {					zzmatch(102); zzCONSUME;				}				zzEXIT(zztasp3);				}			}			zzmatch(PassAction);			pdecl = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));			require(pdecl!=NULL, "rule rule: cannot allocate param decl");			strcpy(pdecl, LATEXT(1));			CurParmDef = pdecl; zzCONSUME;		}		zzEXIT(zztasp2);		}	}	{		zzBLOCK(zztasp2);		zzMake0;		{		if ( (LA(1)==103) ) {			zzmatch(103); zzCONSUME;			zzmatch(PassAction);			ret = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));			require(ret!=NULL, "rule rule: cannot allocate ret type");			strcpy(ret, LATEXT(1));			CurRetDef = ret; zzCONSUME;		}		zzEXIT(zztasp2);		}	}	{		zzBLOCK(zztasp2);		zzMake0;		{		if ( (LA(1)==QuotedTerm) ) {			zzmatch(QuotedTerm);			if ( q!=NULL ) q->egroup=mystrdup(LATEXT(1)); zzCONSUME;		}		zzEXIT(zztasp2);		}	}		if ( GenEClasseForRules && q!=NULL ) {		e = newECnode;		require(e!=NULL, "cannot allocate error class node");		if ( q->egroup == NULL ) {a = q->str; a[0] = (char)toupper(a[0]);}		else a = q->egroup;		if ( Tnum( a ) == 0 )		{			e->tok = addTname( a );			list_add(&eclasses, (char *)e);			if ( q->egroup == NULL ) a[0] = (char)tolower(a[0]);			/* refers to itself */			list_add(&(e->elist), mystrdup(q->str));		}		else {			warn(eMsg1("default errclass for '%s' would conflict with token/errclass/tokclass",a));			if ( q->egroup == NULL ) a[0] = (char)tolower(a[0]);			free((char *)e);		}	}	BlkLevel++;	zzmatch(104);	inAlt=1; zzCONSUME;	block( &toksrefd, &rulesrefd );	r = makeBlk(zzaArg(zztasp1,7),0);	CurRuleBlk = (Junction *)r.left;	CurRuleBlk->blockid = CurBlockID;

⌨️ 快捷键说明

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