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

📄 antlr.g

📁 SRI international 发布的OAA框架软件
💻 G
📖 第 1 页 / 共 5 页
字号:
/* MR10 */                      /* element labels might exist without an elem when */
/* MR10 */                      /*  it is a forward reference (to a rule)          */
/* MR10 */
/* MR10 */						if ( GenCC && (el->elem == NULL || el->elem->ntype==nRuleRef) )
/* MR10 */							{ err(eMsg1("There are no token ptrs for rule references: '$%s'",&zzbegexpr[1])); }
/* MR10 */
/* MR10 */						if ( !GenCC && (el->elem == NULL || el->elem->ntype==nRuleRef) && GenAST) {
/* MR10 */                          err("You can no longer use attributes returned by rules when also using ASTs");
/* MR10 */                          err("   Use upward inheritance (\"rule >[Attrib a] : ... <<$a=...\>\>\")");
/* MR10 */                      };
/* MR10 */
/* MR10 */                      /* keep track of <<... $label ...>> for semantic predicates in guess mode */
/* MR10 */                      /* element labels contain pointer to the owners node                      */
/* MR10 */
/* MR10 */                      if (el->elem != NULL && el->elem->ntype == nToken) {
/* MR10 */                        list_add(&CurActionLabels,el);
/* MR10 */                      };
							}
							else
								warn(eMsg1("$%s not parameter, return value, (defined) element label",&zzbegexpr[1]));
							}
							zzmore();
							>>
#token "#0"					<< zzreplstr("(*_root)"); zzmore(); chkGTFlag(); >>
#token "#\[\]"				<< if ( GenCC ) {
                                    if (NewAST) zzreplstr("(newAST)");
                                        else zzreplstr("(new AST)");}
							   else {zzreplstr("zzastnew()");} zzmore();
							   chkGTFlag();
							>>
#token "#\(\)"				<< zzreplstr("NULL"); zzmore(); chkGTFlag(); >>
#token "#[0-9]+"			<<{
							static char buf[100];
							if ( strlen(zzbegexpr)>(size_t)85 )
								fatal("#i AST ref too big");
							if ( GenCC ) sprintf(buf,"_ast%d%s",BlkLevel-1,zzbegexpr+1);
							else sprintf(buf,"zzastArg(%s)",zzbegexpr+1);
							zzreplstr(buf);
							zzmore();
							set_orel(atoi(zzbegexpr+1), &AST_nodes_refd_in_actions);
							chkGTFlag();
 							}
							>>

/* MR14 Arpad Beszedes  26-May-98
        Add support for #line directives when antlr source is pre-processed
        #lexclass ACTIONS
*/

#token "#line[\ \t]* [0-9]+ {[\ \t]* \"~[\"]+\" ([\ \t]* [0-9]*)* } (\n|\r|\r\n)"
        <<
        zzline = atoi(zzbegexpr+5) - 1; zzline++; zzmore();
        getFileNameFromTheLineInfo(FileStr[CurFile], zzbegexpr);
        >>

#token "#line ~[\n\r]* (\n|\r|\r\n)"
        <<
        zzline++; zzmore();
        >>

/* MR14 end of a block to support #line in antlr source code */

#token "#[_a-zA-Z][_a-zA-Z0-9]*"
							<<
							if ( !(strcmp(zzbegexpr, "#ifdef")==0 ||
								 strcmp(zzbegexpr, "#if")==0 ||
								 strcmp(zzbegexpr, "#else")==0 ||
								 strcmp(zzbegexpr, "#endif")==0 ||
								 strcmp(zzbegexpr, "#ifndef")==0 ||
								 strcmp(zzbegexpr, "#define")==0 ||
								 strcmp(zzbegexpr, "#pragma")==0 ||
								 strcmp(zzbegexpr, "#undef")==0 ||
								 strcmp(zzbegexpr, "#import")==0 ||
								 strcmp(zzbegexpr, "#line")==0 ||
								 strcmp(zzbegexpr, "#include")==0 ||
								 strcmp(zzbegexpr, "#error")==0) )
							{
								static char buf[100];
								sprintf(buf, "%s_ast", zzbegexpr+1);
/* MR27 */						list_add(&CurAstLabelsInActions, mystrdup(zzbegexpr+1));
								zzreplstr(buf);
								chkGTFlag();
							}
							zzmore();
							>>
#token "#\["				<<
							pushint(']');
							if ( GenCC ) {
                                if (NewAST) zzreplstr("(newAST(");
                                    else zzreplstr("(new AST("); }
							else zzreplstr("zzmk_ast(zzastnew(),");
							zzmore();
							chkGTFlag();
							>>
#token "#\("				<<
							pushint('}');
							if ( GenCC ) {
								if (tmakeInParser) {
									zzreplstr("tmake(");
								}
								else {
									zzreplstr("ASTBase::tmake(");
								}
							}
							else {
								zzreplstr("zztmake(");
							}
							zzmore();
							chkGTFlag();
							>>
#token "#"					<< zzmore(); >>
#token "\)"					<<
							if ( istackempty() )
								zzmore();
							else if ( topint()==')' ) {
								popint();
							}
							else if ( topint()=='}' ) {
								popint();
								/* terminate #(..) */
								zzreplstr(", NULL)");
							}
							zzmore();
							>>
#token "\["					<<
							pushint('|');	/* look for '|' to terminate simple [...] */
							zzmore();
							>>
#token "\("					<<
							pushint(')');
							zzmore();
							>>

#token "\\\]"				<< zzreplstr("]");  zzmore(); >>
#token "\\\)"				<< zzreplstr(")");  zzmore(); >>

/* MR1	10-Apr-97  MR1  Previously unable to put right shift operator	*/
/* MR1					in DLG action			*/

#token "\\>"	<< if (! tokenActionActive) zzreplstr(">");	 /* MR1 */
		   zzmore();				         /* MR1 */
		>>					         /* MR1	*/


#token "'"					<< zzmode(ACTION_CHARS); zzmore();>>
#token "\""					<< zzmode(ACTION_STRINGS); zzmore();>>
#token "\\$"				<< zzreplstr("$");  zzmore(); >>
#token "\\#"				<< zzreplstr("#");  zzmore(); >>
#token "\\(\n|\r|\r\n)"		<< zzline++; zzmore(); >>
#token "\\~[\]\)>$#]"		<< zzmore(); >> /* escaped char, always ignore */
#token "/"					<< zzmore(); >>
#token "/\*"				<< zzmode(ACTION_COMMENTS); zzmore(); >>
#token "\*/"				<< warn("Missing /*; found dangling */ in action"); zzmore(); >>
#token "//"					<< zzmode(ACTION_CPP_COMMENTS); zzmore(); >>
#token "~[\n\r\)\(\\$#\>\]\[\"'/]+" << zzmore(); >>

#lexclass START
#token "[\t\ ]+"			<< zzskip(); >>				/* Ignore White */
#token "\n|\r|\r\n"	   		<< zzline++; zzskip(); >>	/* Track Line # */
#token "\["                 << zzmode(ACTIONS); zzmore();
                               istackreset();
                               pushint(']'); >>
#token "\<\<"               << action_file=CurFile; action_line=zzline;
                               zzmode(ACTIONS); zzmore();
                               list_free(&CurActionLabels,0);       /* MR10 */
                               numericActionLabel=0;                /* MR10 */
                               istackreset();
                               pushint('>'); >>
#token "\""					<< zzmode(STRINGS); zzmore(); >>
#token "/\*"				<< zzmode(COMMENTS); zzskip(); >>
#token "\*/"				<< warn("Missing /*; found dangling */"); zzskip(); >>
#token "//"					<< zzmode(CPP_COMMENTS); zzskip(); >>

/* MR14 Arpad Beszedes  26-May-98
        Add support for #line directives when antlr source is pre-processed
        #lexclass START
*/

#token "#line[\ \t]* [0-9]+ {[\ \t]* \"~[\"]+\" ([\ \t]* [0-9]*)* } (\n|\r|\r\n)"
        <<
        zzline = atoi(zzbegexpr+5) - 1; zzline++; zzmore();
        getFileNameFromTheLineInfo(FileStr[CurFile], zzbegexpr);
        >>

#token "#line ~[\n\r]* (\n|\r|\r\n)"
        <<
        zzline++; zzmore();
        >>

/* MR14 end of a block to support #line in antlr source code */

/*                                                                          */
/*  8-Apr-97	Regularize escape sequence for ">>"                         */
/*			appearing in string literals                                    */
/*                                                                          */

#token "\>\>"			<< warn("Missing <<; found dangling \>\>"); zzskip(); >> /* MR1 */
#token WildCard "."
#token "\@"			<<FoundException = 1;		/* MR6 */
    				  FoundAtOperator = 1;>>	/* MR6 */
#token Eof					"@"
							<<	/* L o o k  F o r  A n o t h e r  F i l e */
							{
							FILE *new_input;
							new_input = NextFile();
							if ( new_input == NULL ) { NLA=Eof; return; }
							fclose( input );
							input = new_input;
							zzrdstream( input );
							zzskip();	/* Skip the Eof (@) char i.e continue */
							}
							>>

#token LABEL

#errclass "grammar-element" { element }
#errclass "meta-symbol"		{ "\}" "!" ";" "\|" "\~" "^" "\)" }

#token Pragma			"{\\}#pragma"			/* MR21 */
#token FirstSetSymbol	"{\\}#FirstSetSymbol"	/* MR21 */
/*
 * Get a grammar -- Build a list of rules like:
 *
 *	o-->Rule1--o
 *	|
 *	o-->Rule2--o
 *	|
 *	...
 *	|
 *	o-->RuleN--o
 */

/* rule grammar */

grammar :	<<Graph g;>>
			(	"{\\}#header" Action    /* MR13 */
				<<
				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");
				>>
            |   "{\\}#first" 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");
                    };
				>>

			|	"{\\}#parser" 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");
				}
				>>
			|	"{\\}#tokdefs" 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);
				}>>
			)*
			(	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);
				}>>
			|	laction
			|	lmember				/* MR1 */
			|	lprefix				/* MR1 */
			|	aLexclass
			|	token
			|	error
			|	tclass
            |   aPred               /* MR11 */
			|	default_exception_handler
			|	class_def
			|	"\}"
				<<
				if ( class_nest_level==0 )
					warn("missing class definition for trailing '}'");
				class_nest_level--;
				>>
			)*
            
			rule		<<g=$3; SynDiag = (Junction *) $3.left;>>
			(	rule

                	<<if ( $1.left!=NULL ) {
                        g.right = NULL;

/* MR21a */             /*  Avoid use of a malformed graph when CannotContinue */
/* MR21a */             /*  is already set                                     */
/* MR21a */
/* MR21a */             if (! (CannotContinue && g.left == NULL)) {
/* MR21a */               g = Or(g, $1);
/* MR21a */             }
/* MR21a */		      }
                    >>

			|	aLexclass
			|	token
			|	error
			|	tclass
            |   aPred               /* MR11 */
			|	class_def
			|	"\}"
				<<
				if ( class_nest_level==0 )
					warn("missing class definition for trailing '}'");
				class_nest_level--;
				>>
			)*
			(	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);
				}>>
			|	laction
			|	lmember				/* MR1 */
			|	lprefix				/* MR1 */
			|	error
			|	tclass
			|	class_def
            |   aPred               /* MR11 */
			|	"\}"
				<<
				if ( class_nest_level==0 )
					warn("missing class definition for trailing '}'");
				class_nest_level--;
				>>
			)*
			Eof
		;
		<<CannotContinue=TRUE;>>

/* rule class_def */

class_def
	:	<<int go=1; char name[MaxRuleName+1];>>
		"class"
		(	NonTerminal		<<if(go) strncpy(name,LATEXT(1),MaxRuleName);>>
		|	TokenTerm		<<if(go) strncpy(name,LATEXT(1),MaxRuleName);>>
		)
		<<
		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"); }>>

/* MR10 */  (~ "\{"
/* MR10 */            <<if (ClassDeclStuff == NULL) {
/* MR10 */                   ClassDeclStuff=(char *)calloc(MaxClassDeclStuff+1,sizeof(char));
/* MR10 */              };
/* MR10 */              strncat(ClassDeclStuff," ",MaxClassDeclStuff);

⌨️ 快捷键说明

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