📄 antlr.g
字号:
/* MR10 */ strncat(ClassDeclStuff,LATEXT(1),MaxClassDeclStuff);
/* MR22 */ do {
/* MR22 */ if (0 == strcmp(LATEXT(1),"public")) break;
/* MR22 */ if (0 == strcmp(LATEXT(1),"private")) break;
/* MR22 */ if (0 == strcmp(LATEXT(1),"protected")) break;
/* MR22 */ if (0 == strcmp(LATEXT(1),"virtual")) break;
/* MR22 */ if (0 == strcmp(LATEXT(1),",")) break;
/* MR22 */ if (0 == strcmp(LATEXT(1),":")) break;
/* MR22 */ if (BaseClassName != NULL) break;
/* MR22 */ BaseClassName=(char *)calloc(strlen(LATEXT(1))+1,sizeof(char));
/* MR22 */ require(BaseClassName!=NULL, "rule grammar: cannot allocate base class name");
/* MR22 */ strcpy(BaseClassName,LATEXT(1));
/* MR22 */ } while (0);
/* 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 rule */
rule : <<
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;
CurAstLabelsInActions = NULL; /* MR27 */
/* 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;
>>
}
{ "\>"
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;
>>
}
{ QuotedTerm <<if ( q!=NULL ) q->egroup=mystrdup(LATEXT(1));>> }
<<
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++;
if (BlkLevel >= MAX_BLK_LEVEL) fatal("Blocks nested too deeply");
/* MR23 */ CurBlockID_array[BlkLevel] = CurBlockID;
/* MR23 */ CurAltNum_array[BlkLevel] = CurAltNum;
>>
":" <<inAlt=1;>>
block[&toksrefd, &rulesrefd]
<<r = makeBlk($7,0, NULL /* pFirstSetSymbol */ );
CurRuleBlk = (Junction *)r.left;
CurRuleBlk->blockid = CurBlockID;
CurRuleBlk->jtype = RuleBlk;
if ( q!=NULL ) CurRuleBlk->rname = q->str;
CurRuleBlk->file = f;
CurRuleBlk->line = l;
CurRuleBlk->pdecl = pdecl;
CurRuleBlk->ret = ret;
CurRuleBlk->lock = makelocks();
CurRuleBlk->pred_lock = makelocks();
CurRuleBlk->tokrefs = toksrefd;
CurRuleBlk->rulerefs = rulesrefd;
p = newJunction(); /* add EndRule Node */
((Junction *)r.right)->p1 = (Node *)p;
r.right = (Node *) p;
p->jtype = EndRule;
p->lock = makelocks();
p->pred_lock = makelocks();
CurRuleBlk->end = p;
if ( q!=NULL ) q->rulenum = NumRules;
$7 = r;
>>
<<
/* MR23 */ CurBlockID_array[BlkLevel] = (-1);
/* MR23 */ CurAltNum_array[BlkLevel] = (-1);
--BlkLevel;
>>
<<altFixup();leFixup();egFixup();>> /* MR7 */
";" <<inAlt=0;>>
{ Action
<< a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
require(a!=NULL, "rule rule: cannot allocate error action");
strcpy(a, LATEXT(1));
CurRuleBlk->erraction = a;
>>
}
( exception_group > [eg]
<<if ( eg!=NULL ) {
list_add(&CurExGroups, (void *)eg);
if (eg->label == NULL || *eg->label=='\0' ) q->has_rule_exception = 1;
}
>>
)*
<<if ( q==NULL ) $0.left = NULL; else $0 = $7;>>
<<CurRuleBlk->exceptions = CurExGroups;>>
<<CurRuleBlk->el_labels = CurElementLabels;>>
<<CurRuleNode->ast_labels_in_actions = CurAstLabelsInActions;>> /* MR27 */
<<CurRuleNode = NULL;>> /* MR27 Moved */
;
<<CannotContinue=TRUE;>>
/*
* pragma : "{\\}#pragma" "dup\-labeled\-tokens"
* <<Pragma_DupLabeledTokens=1;>>
* ;
*/
/* rule laction */
laction : <<char *a;>>
"{\\}#lexaction"
Action
<<
a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
require(a!=NULL, "rule laction: cannot allocate action");
strcpy(a, LATEXT(1));
list_add(&LexActions, a);
>>
;
<<CannotContinue=TRUE;>>
/* MR1 */
/* MR1 11-Apr-97 Provide mechanism for inserting code into DLG class */
/* MR1 via #lexmember <<....>> & #lexprefix <<...>> */
/* MR1 */
/* rule lmember */
lmember: <<char *a;>> /* MR1 */
/* MR1 */ "{\\}#lexmember"
/* MR1 */ Action
/* MR1 */ <<
/* MR1 */ if (! GenCC) {
/* MR1 */ err("Use #lexmember only in C++ mode (to insert code in DLG class header");
/* MR1 */ } else {
/* MR1 */ a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
/* MR1 */ require(a!=NULL, "rule lmember: cannot allocate action");
/* MR1 */ strcpy(a, LATEXT(1));
/* MR1 */ list_add(&LexMemberActions, a);
/* MR1 */ };
/* MR1 */ >>
/* MR1 */ ;
/* MR1 */ <<CannotContinue=TRUE;>>
/* rule lprefix */
lprefix: <<char *a;>> /* MR1 */
/* MR1 */ "{\\}#lexprefix"
/* MR1 */ Action
/* MR1 */ <<
/* MR1 */ if (! GenCC) {
/* MR1 */ err("Use #lexprefix only in C++ mode (to insert code in DLG class header");
/* MR1 */ } else {
/* MR1 */ a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
/* MR1 */ require(a!=NULL, "rule lprefix: cannot allocate action");
/* MR1 */ strcpy(a, LATEXT(1));
/* MR1 */ list_add(&LexPrefixActions, a);
/* MR1 */ };
/* MR1 */ >>
/* MR1 */ ;
/* MR1 */ <<CannotContinue=TRUE;>>
/*
* #pred upper <<isupper()>>? predicate literal
* #pred lower <<islower()>>? predicate literal
* #pred up_or_low upper || lower predicate expression
* concealed interdependence
* #pred up_or_low_2 <<isletter()>>? A || B predicate literal equals predicate expr
* analyze using lower||upper
* generate using isLetter()
*/
/* rule aPref */
aPred: <<PredEntry *predEntry=NULL;
char *name=NULL;
Predicate *predExpr=NULL;
char *predLiteral=NULL;
int save_file;
int save_line;
int predExprPresent=0;
>>
"{\\}#pred"
<<
MR_usingPredNames=1; /* will need to use -mrhoist version of genPredTree */
>>
/* used to allow NonTerminal but it caused problems
when a rule name immediately followed a #pred statement */
TokenTerm <<name=mystrdup(LATEXT(1));>>
<<
/* don't free - referenced in predicates */
CurPredName=(char *)calloc(1,strlen(name) + 10);
strcat(CurPredName,"#pred ");
strcat(CurPredName,name);
predEntry=(PredEntry *) hash_get(Pname,name);
if (predEntry != NULL) {
warnFL(eMsg1("#pred %s previously defined - ignored",name),
FileStr[action_file],action_line);
name=NULL;
};
>>
(
Pred <<predLiteral=mystrdup(LATEXT(1));
save_line=action_line;
save_file=action_file;
>>
{
predOrExpr>[predExpr] <<predExprPresent=1;>>
}
<<if (predLiteral != NULL && name != NULL) {
/*
* predExpr may be NULL due to syntax errors
* or simply omitted by the user
*/
predEntry=newPredEntry(name);
predEntry->file=save_file;
predEntry->line=save_line;
predExpr=MR_predFlatten(predExpr);
predEntry->predLiteral=predLiteral;
if (! predExprPresent || predExpr == NULL) {
predExpr=new_pred();
predExpr->expr=predLiteral;
predExpr->source=newActionNode();
predExpr->source->action=predExpr->expr;
predExpr->source->rname=CurPredName;
predExpr->source->line=action_line;
predExpr->source->file=action_file;
predExpr->source->is_predicate=1;
predExpr->k=predicateLookaheadDepth(predExpr->source);
};
predEntry->pred=predExpr;
hash_add(Pname,name,(Entry *)predEntry);
predExpr=NULL;
};
predicate_free(predExpr);
>>
|
<<save_line=zzline; save_file=CurFile;>>
predOrExpr>[predExpr]
<<if (predExpr != NULL && name != NULL) {
predEntry=newPredEntry(name);
predEntry->file=CurFile;
predEntry->line=zzline;
predExpr=MR_predFlatten(predExpr);
predEntry->pred=predExpr;
hash_add(Pname,name,(Entry *)predEntry);
predExpr=NULL;
};
predicate_free(predExpr);
>>
)
{";"}
;
/* fail */
<<predicate_free(predExpr);
>>
/* rule predOrExpr */
predOrExpr>[Predicate *result] :
<<Predicate *ORnode;
Predicate *predExpr;
Predicate **tail=NULL;
>>
predAndExpr>[predExpr]
<<
ORnode=new_pred();
ORnode->expr=PRED_OR_LIST;
if (predExpr != NULL) {
ORnode->down=predExpr;
tail=&predExpr->right;
};
>>
( "\|\|" predAndExpr>[predExpr]
<<
if (predExpr != NULL) {
*tail=predExpr;
tail=&predExpr->right;
};
>>
)*
<<
$result=ORnode;
ORnode=NULL;
>>
;
/* fail */
<<predicate_free(ORnode);>>
/* rule predAndExpr */
predAndExpr>[Predicate *result] :
<<Predicate *ANDnode;
Predicate *predExpr;
Predicate **tail=NULL;
>>
predPrimary>[predExpr]
<<
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -