📄 antlr.g
字号:
#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); 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 ) 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" { "\}" "!" ";" "\|" "\~" "^" "\)" }/* * Get a grammar -- Build a list of rules like: * * o-->Rule1--o * | * o-->Rule2--o * | * ... * | * o-->RuleN--o */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; g = Or(g, $1);}>> | 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;>>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);/* MR10 */ strncat(ClassDeclStuff,LATEXT(1),MaxClassDeclStuff);/* MR10 */ >>/* MR10 */ )* "\{" << no_classes_found = 0; if ( class_nest_level>=1 ) {warn("cannot have nested classes");} else class_nest_level++; >> ; <<CannotContinue=TRUE;>>/* * Build -o-->o-R-o-->o- where -o-R-o- is the block from rule 'block'. * Construct the RuleBlk front and EndRule node on the end of the * block. This is used to add FOLLOW pointers to the rule end. Add the * new rule name to the Rname hash table and sets its rulenum. * Store the parameter definitions if any are found. * * Note that locks are required on the RuleBlk and EndRule nodes to thwart * infinite recursion. * * Return the left graph pointer == NULL to indicate error/dupl rule def. */rule : << 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; >> 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++; >> { "!" <<if ( q!=NULL ) q->noAST = TRUE;>> } { <<;>> {"\<"} 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; >> }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -