📄 antlr.g
字号:
/* * antlr.g -- PCCTS Version 1.xx ANTLR * * Parse an antlr input grammar and build a syntax-diagram. * * Written in itself (needs at least 1.06 to work) * * 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-1995 *//* MR1 *//* MR1 10-Apr-97 MR1 Replace #if logic with #include "pcctscfg.h" *//* MR1 */#header << #include "set.h" #include <ctype.h> #include "syn.h" #include "hash.h" #include "generic.h" #define zzcr_attr(attr,tok,t) >><<#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;>>#lexaction <</* maintained, but not used for now */set AST_nodes_refd_in_actions = set_init;int inAlt = 0;set attribsRefdFromAction;int UsedOldStyleAttrib = 0;int UsedNewStyleLabel = 0;#ifdef __USE_PROTOSchar *inline_set(char *);#elsechar *inline_set();#endif/* MR1 10-Apr-97 MR1 Previously unable to put right shift operator *//* MR1 in DLG action */int tokenActionActive=0; /* MR1 */>>#lexclass STRINGS#token QuotedTerm "\"" << zzmode(START); >>#token "\n|\r|\r\n" << zzline++; warn("eoln found in string"); zzskip(); >>#token "\\(\n|\r|\r\n)" << zzline++; zzmore(); >>#token "\\~[]" << zzmore(); >>#token "~[\n\r\"\\]+" << zzmore(); >>#lexclass ACTION_STRINGS#token "\"" << zzmode(ACTIONS); zzmore(); >>#token "\n|\r|\r\n" << zzline++; warn("eoln found in string (in user action)"); zzskip(); >>#token "\\(\n|\r|\r\n)" << zzline++; zzmore(); >>#token "\\~[]" << zzmore(); >>#token "~[\n\r\"\\]+" << zzmore(); >>#lexclass ACTION_CHARS#token "'" << zzmode(ACTIONS); zzmore(); >>#token "\n|\r|\r\n" << zzline++; warn("eoln found in char literal (in user action)"); zzskip(); >>#token "\\~[]" << zzmore(); >>#token "~[\n\r'\\]+" << zzmore(); >>#lexclass ACTION_COMMENTS#token "\*/" << zzmode(ACTIONS); zzmore(); >>#token "\*" << zzmore(); >>#token "\n|\r|\r\n" << zzline++; zzmore(); DAWDLE; >>#token "~[\n\r\*]+" << zzmore(); >>#lexclass TOK_DEF_COMMENTS#token "\*/" << zzmode(PARSE_ENUM_FILE); zzmore(); >>#token "\*" << zzmore(); >>#token "\n|\r|\r\n" << zzline++; zzmore(); DAWDLE; >>#token "~[\n\r\*]+" << zzmore(); >>#lexclass TOK_DEF_CPP_COMMENTS#token "\n|\r|\r\n" << zzline++; zzmode(PARSE_ENUM_FILE); zzskip(); DAWDLE; >>#token "~[\n\r]+" << zzskip(); >>#lexclass ACTION_CPP_COMMENTS#token "\n|\r|\r\n" << zzline++; zzmode(ACTIONS); zzmore(); DAWDLE; >>#token "~[\n\r]+" << zzmore(); >>#lexclass CPP_COMMENTS#token "\n|\r|\r\n" << zzline++; zzmode(START); zzskip(); DAWDLE; >>#token "~[\n\r]+" << zzskip(); >>#lexclass COMMENTS#token "\*/" << zzmode(START); zzskip(); >>#token "\*" << zzskip(); >>#token "\n|\r|\r\n" << zzline++; zzskip(); DAWDLE; >>#token "~[\n\r\*]+" << zzskip(); >>/* * This lexical class accepts actions of type [..] and <<..>> * * It translates the following special items for C: * * $j --> "zzaArg(current zztasp, j)" * $i.j --> "zzaArg(zztaspi, j)" * $i.nondigit> "zzaArg(current zztasp, i).nondigit" * $$ --> "zzaRet" * $alnum --> "alnum" (used to ref parameters) * $rule --> "zzaRet" * $retval --> "_retv.retval" if > 1 return values else "_retv" * $[token, text] --> "zzconstr_attr(token, text)" * $[] --> "zzempty_attr()" * * It translates the following special items for C++: * (attributes are now stored with 'Token' and $i's are only * pointers to the Tokens. Rules don't have attributes now.) * * $j --> "_tbj" where b is the block level * $i.j --> "_tij" * $j->nondigit> "_tbj->nondigit" * $$ --> "$$" * $alnum --> "alnum" (used to ref parameters) * $rule --> "$rule" * $retval --> "_retv.retval" if > 1 return values else "_retv" * $[token, text] --> invalid * $[] --> invalid * * And, for trees: * * #0 --> "(*_root)" * #i --> "zzastArg(i)" * #[args] --> "zzmk_ast(zzastnew(), args)" * #[] --> "zzastnew()" * #( root, child1, ..., childn ) * --> "zztmake(root, child1, ...., childn, NULL)" * #() --> "NULL" * * For C++, ... * * #0 --> "(*_root)" * #i --> "_astbi" where b is the block level * #alnum --> "alnum_ast" (used to ref #label) * #[args] --> "new AST(args)" * #[] --> "new AST" * #( root, child1, ..., childn ) * --> "AST::tmake(root, child1, ...., childn, NULL)" * #() --> "NULL" * * To escape, * * \] --> ] * \) --> ) * \$ --> $ * \# --> # * * A stack is used to nest action terminators because they can be nested * like crazy: << #[$[..],..] >> */#lexclass ACTIONS#token Action "\>\>" << /* these do not nest */ zzmode(START); NLATEXT[0] = ' '; NLATEXT[1] = ' '; zzbegexpr[0] = ' '; zzbegexpr[1] = ' '; if ( zzbufovf ) { err( eMsgd("action buffer overflow; size %d",ZZLEXBUFSIZE)); }/* MR1 10-Apr-97 MR1 Previously unable to put right shift operator *//* MR1 in DLG action *//* MR1 Doesn't matter what kind of action it is - reset*/ tokenActionActive=0; /* MR1 */ >>#token Pred "\>\>?" << /* these do not nest */ zzmode(START); NLATEXT[0] = ' '; NLATEXT[1] = ' '; zzbegexpr[0] = '\0'; if ( zzbufovf ) { err( eMsgd("predicate buffer overflow; size %d",ZZLEXBUFSIZE)); };#ifdef __cplusplus__/* MR10 */ list_apply(CurActionLabels, (void (*)(void *))mark_label_used_in_sem_pred);#else#ifdef __STDC__/* MR10 */ list_apply(CurActionLabels, (void (*)(void *))mark_label_used_in_sem_pred);#else/* MR10 */ list_apply(CurActionLabels,mark_label_used_in_sem_pred);#endif#endif >>#token PassAction "\]" << if ( topint() == ']' ) { popint(); if ( istackempty() ) /* terminate action */ { zzmode(START); NLATEXT[0] = ' '; zzbegexpr[0] = ' '; if ( zzbufovf ) { err( eMsgd("parameter buffer overflow; size %d",ZZLEXBUFSIZE)); } } else { /* terminate $[..] and #[..] */ if ( GenCC ) zzreplstr("))"); else zzreplstr(")"); zzmore(); } } else if ( topint() == '|' ) { /* end of simple [...] */ popint(); zzmore(); } else zzmore(); >>#token "consumeUntil\( [\ \t]* \{~[\}]+\} [\ \t]* \)" << zzmore(); zzreplstr(inline_set(zzbegexpr+ strlen("consumeUntil("))); >>#token "consumeUntil\( ~[\)]+ \)" << zzmore(); >>#token "\n|\r|\r\n" << zzline++; zzmore(); DAWDLE; >>#token "\>" << zzmore(); >>#token "$" << zzmore(); >>#token "$$" << if ( !GenCC ) {zzreplstr("zzaRet"); zzmore();} else err("$$ use invalid in C++ mode"); >>#token "$\[\]" << if ( !GenCC ) {zzreplstr("zzempty_attr"); zzmore();} else err("$[] use invalid in C++ mode"); >>#token "$\[" << pushint(']'); if ( !GenCC ) zzreplstr("zzconstr_attr("); else err("$[..] use invalid in C++ mode"); zzmore(); >>#token "$[0-9]+" <<{ static char buf[100]; numericActionLabel=1; /* MR10 */ if ( strlen(zzbegexpr)>(size_t)85 ) fatal("$i attrib ref too big"); set_orel(atoi(zzbegexpr+1), &attribsRefdFromAction); if ( !GenCC ) sprintf(buf,"zzaArg(zztasp%d,%s)", BlkLevel-1,zzbegexpr+1); else sprintf(buf,"_t%d%s", BlkLevel-1,zzbegexpr+1); zzreplstr(buf); zzmore(); UsedOldStyleAttrib = 1; if ( UsedNewStyleLabel ) err("cannot mix old-style $i with new-style labels"); } >>#token "$[0-9]+." <<{ static char buf[100]; numericActionLabel=1; /* MR10 */ if ( strlen(zzbegexpr)>(size_t)85 ) fatal("$i.field attrib ref too big"); zzbegexpr[strlen(zzbegexpr)-1] = ' '; set_orel(atoi(zzbegexpr+1), &attribsRefdFromAction); if ( !GenCC ) sprintf(buf,"zzaArg(zztasp%d,%s).", BlkLevel-1,zzbegexpr+1); else sprintf(buf,"_t%d%s.", BlkLevel-1,zzbegexpr+1); zzreplstr(buf); zzmore(); UsedOldStyleAttrib = 1; if ( UsedNewStyleLabel ) err("cannot mix old-style $i with new-style labels"); } >>#token "$[0-9]+.[0-9]+" <<{ static char buf[100]; static char i[20], j[20]; char *p,*q; numericActionLabel=1; /* MR10 */ if (strlen(zzbegexpr)>(size_t)85) fatal("$i.j attrib ref too big"); for (p=zzbegexpr+1,q= &i[0]; *p!='.'; p++) { if ( q == &i[20] ) fatalFL("i of $i.j attrib ref too big", FileStr[CurFile], zzline ); *q++ = *p; } *q = '\0'; for (p++, q= &j[0]; *p!='\0'; p++) { if ( q == &j[20] ) fatalFL("j of $i.j attrib ref too big", FileStr[CurFile], zzline ); *q++ = *p; } *q = '\0'; if ( !GenCC ) sprintf(buf,"zzaArg(zztasp%s,%s)",i,j); else sprintf(buf,"_t%s%s",i,j); zzreplstr(buf); zzmore(); UsedOldStyleAttrib = 1; if ( UsedNewStyleLabel ) err("cannot mix old-style $i with new-style labels"); } >>#token "$[_a-zA-Z][_a-zA-Z0-9]*" <<{ static char buf[300]; LabelEntry *el; zzbegexpr[0] = ' '; if ( CurRule != NULL && strcmp(CurRule, &zzbegexpr[1])==0 ) { if ( !GenCC ) zzreplstr("zzaRet"); } else if ( CurRetDef != NULL && strmember(CurRetDef, &zzbegexpr[1])) { if ( HasComma( CurRetDef ) ) { require (strlen(zzbegexpr)<=(size_t)285, "$retval attrib ref too big"); sprintf(buf,"_retv.%s",&zzbegexpr[1]); zzreplstr(buf); } else zzreplstr("_retv"); } else if ( CurParmDef != NULL && strmember(CurParmDef, &zzbegexpr[1])) { ; } else if ( Elabel==NULL ) { { err("$-variables in actions outside of rules are not allowed"); } } else if ( (el=(LabelEntry *)hash_get(Elabel, &zzbegexpr[1]))!=NULL ) {/* MR10 *//* 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(); >>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -