📄 antlr.g
字号:
set_orel(e_num, $rulesrefd);
}
}
}
>>
)*
<<if ( n == 0 ) g = emptyAlt();
$0 = g;
/* We want to reduce number of LT(i) calls and the number of
* local attribute variables in C++ mode (for moment, later we'll
* do for C also). However, if trees are being built, they
* require most of the attrib variables to create the tree nodes
* with; therefore, we gen a token ptr for each token ref in C++
*/
if ( GenCC && !GenAST )
{
/* This now free's the temp set -ATG 5/6/95 */
set temp;
temp = set_and(elems, attribsRefdFromAction);
set_orin($toksrefd, temp);
set_free(temp);
}
else set_orin($toksrefd, elems);
if ( GenCC ) {
dif = set_dif(attribsRefdFromAction, elems);
if ( set_deg(dif)>0 )
err("one or more $i in action(s) refer to non-token elements");
set_free(dif);
}
set_free(elems);
set_free(attribsRefdFromAction);
inAlt = 0;
>>
;
<<CannotContinue=TRUE;>>
/* rule element_label */
element_label > [LabelEntry *label]
: <<TermEntry *t=NULL; LabelEntry *l=NULL; RuleEntry *r=NULL; char *lab;>>
LABEL <<lab = mystrdup(LATEXT(1));>>
<<
UsedNewStyleLabel = 1;
if ( UsedOldStyleAttrib ) err("cannot mix with new-style labels with old-style $i");
t = (TermEntry *) hash_get(Tname, lab);
if ( t==NULL ) t = (TermEntry *) hash_get(Texpr, lab);
if ( t==NULL ) r = (RuleEntry *) hash_get(Rname, lab);
if ( t!=NULL ) {
err(eMsg1("label definition clashes with token/tokclass definition: '%s'", lab));
$label = NULL;
}
else if ( r!=NULL ) {
err(eMsg1("label definition clashes with rule definition: '%s'", lab));
$label = NULL;
}
else {
/* we don't clash with anybody else */
l = (LabelEntry *) hash_get(Elabel, lab);
if ( l==NULL ) { /* ok to add new element label */
l = (LabelEntry *)hash_add(Elabel,
lab,
(Entry *)newLabelEntry(lab));
/* add to list of element labels for this rule */
list_add(&CurElementLabels, (void *)lab);
/* MR7 */ leAdd(l); /* list of labels waiting for exception group definitions */
$label = l;
}
else {
err(eMsg1("label definitions must be unique per rule: '%s'", lab));
$label = NULL;
}
}
>>
":"
;
/* rule element */
element[int old_not, int first_on_line, int use_def_MT_handler] > [Node *node]
: <<
Attrib blk;
Predicate *pred = NULL;
int local_use_def_MT_handler=0;
ActionNode *act;
RuleRefNode *rr;
set toksrefd, rulesrefd;
TermEntry *term;
TokNode *p=NULL; RuleRefNode *q; int approx=0;
LabelEntry *label=NULL;
int predMsgDone=0;
int semDepth=0;
int ampersandStyle;
int height; /* MR11 */
int equal_height; /* MR11 */
char* pFirstSetSymbol = NULL; /* MR21 */
$node = NULL;
>>
{element_label>[label]}
( TokenTerm
<<
term = (TermEntry *) hash_get(Tname, LATEXT(1));
if ( term==NULL && UserDefdTokens ) {
err("implicit token definition not allowed with #tokdefs");
$$.left = $$.right = NULL;
}
else {
$$ = buildToken(LATEXT(1));
p=((TokNode *)((Junction *)$$.left)->p1);
term = (TermEntry *) hash_get(Tname, LATEXT(1));
require( term!= NULL, "hash table mechanism is broken");
p->tclass = term->tclass;
p->complement = $old_not;
if ( label!=NULL ) {
p->el_label = label->str;
label->elem = (Node *)p;
}
}
>>
{ ".."
( QuotedTerm
<<if ( p!=NULL ) setUpperRange(p, LATEXT(1));>>
| TokenTerm
<<if ( p!=NULL ) setUpperRange(p, LATEXT(1));>>
)
}
<<
if ( p!=NULL && (p->upper_range!=0 || p->tclass || $old_not) )
list_add(&MetaTokenNodes, (void *)p);
>>
( "^" <<if ( p!=NULL ) p->astnode=ASTroot;>>
| <<if ( p!=NULL ) p->astnode=ASTchild;>>
| "!" <<if ( p!=NULL ) p->astnode=ASTexclude;>>
)
{ "\@" <<local_use_def_MT_handler = 1;>> }
<<
if ( p!=NULL && $first_on_line ) {
CurAltStart = (Junction *)$$.left;
altAdd(CurAltStart); /* MR7 */
p->altstart = CurAltStart;
}
if ( p!=NULL )
p->use_def_MT_handler = $use_def_MT_handler || local_use_def_MT_handler;
$node = (Node *)p;
>>
| QuotedTerm
<<
term = (TermEntry *) hash_get(Texpr, LATEXT(1));
if ( term==NULL && UserDefdTokens ) {
err("implicit token definition not allowed with #tokdefs");
$$.left = $$.right = NULL;
}
else {
$$ = buildToken(LATEXT(1)); p=((TokNode *)((Junction *)$$.left)->p1);
p->complement = $old_not;
if ( label!=NULL ) {
p->el_label = label->str;
label->elem = (Node *)p;
}
}
>>
{ ".."
( QuotedTerm
<<if ( p!=NULL ) setUpperRange(p, LATEXT(1));>>
| TokenTerm
<<if ( p!=NULL ) setUpperRange(p, LATEXT(1));>>
)
}
( "^" <<if ( p!=NULL ) p->astnode=ASTroot;>>
| <<if ( p!=NULL ) p->astnode=ASTchild;>>
| "!" <<if ( p!=NULL ) p->astnode=ASTexclude;>>
)
{ "\@" <<local_use_def_MT_handler = 1;>> }
<<
if ( p!=NULL && (p->upper_range!=0 || p->tclass || $old_not) )
list_add(&MetaTokenNodes, (void *)p);
>>
<<
if ( $first_on_line ) {
CurAltStart = (Junction *)$$.left;
altAdd(CurAltStart); /* MR7 */
p->altstart = CurAltStart;
}
if ( p!=NULL )
p->use_def_MT_handler = $use_def_MT_handler || local_use_def_MT_handler;
$node = (Node *)p;
>>
| <<if ( $old_not ) warn("~ WILDCARD is an undefined operation (implies 'nothing')");>>
"."
<<$$ = buildWildCard(LATEXT(1)); p=((TokNode *)((Junction *)$$.left)->p1);>>
( "^" <<p->astnode=ASTroot;>>
| <<p->astnode=ASTchild;>>
| "!" <<p->astnode=ASTexclude;>>
)
<<list_add(&MetaTokenNodes, (void *)p);>>
<<
if ( $first_on_line ) {
CurAltStart = (Junction *)$$.left;
altAdd(CurAltStart); /* MR7 */
p->altstart = CurAltStart;
if ( label!=NULL ) {
p->el_label = label->str;
label->elem = (Node *)p;
}
}
$node = (Node *)p;
>>
| <<if ( $old_not ) warn("~ NONTERMINAL is an undefined operation");>>
NonTerminal
<<$$ = buildRuleRef(LATEXT(1));>>
{ "!" <<q = (RuleRefNode *) ((Junction *)$$.left)->p1;
q->astnode=ASTexclude;>>
}
{ {"\<"}
PassAction <<addParm(((Junction *)$$.left)->p1, LATEXT(1));>>
}
<<rr=(RuleRefNode *) ((Junction *)$$.left)->p1;>>
{ <<char *a;>>
"\>"
PassAction
<<
a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
require(a!=NULL, "rule element: cannot allocate assignment");
strcpy(a, LATEXT(1));
rr->assign = a;
>>
}
<<
if ( label!=NULL ) {
rr->el_label = label->str;
label->elem = (Node *)rr;
}
if ( $first_on_line ) {
CurAltStart = (Junction *)$$.left;
altAdd(CurAltStart); /* MR7 */
((RuleRefNode *)((Junction *)$$.left)->p1)->altstart = CurAltStart;
}
$node = (Node *)rr;
>>
)
| <<if ( $old_not ) warn("~ ACTION is an undefined operation");>>
Action <<$0 = buildAction(LATEXT(1),action_file,action_line, 0);>>
<<if ( $first_on_line ) { /* MR7 */
CurAltStart = (Junction *)$0.left; /* MR7 */
altAdd(CurAltStart); /* MR7 */
};>> /* MR7 */
<<$node = (Node *) ((Junction *)$0.left)->p1;>>
| <<if ( $old_not ) warn("~ SEMANTIC-PREDICATE is an undefined operation");>>
Pred <<$0 = buildAction(LATEXT(1),action_file,action_line, 1);>>
<<act = (ActionNode *) ((Junction *)$0.left)->p1;>>
<<if (numericActionLabel) { /* MR10 */
list_add(&NumericPredLabels,act); /* MR10 */
numericActionLabel=0; /* MR10 */
}; /* MR10 */
>>
{ <<char *a;>>
PassAction
<<
a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
require(a!=NULL, "rule element: cannot allocate predicate fail action");
strcpy(a, LATEXT(1));
act->pred_fail = a;
>>
}
<<if ( $first_on_line ) { /* MR7 */
CurAltStart = (Junction *)$0.left; /* MR7 */
altAdd(CurAltStart); /* MR7 */
};>> /* MR7 */
<<$node = (Node *)act;>>
| <<if ( $old_not ) warn("~ BLOCK is an undefined operation");>>
<<BlkLevel++;
if (BlkLevel >= MAX_BLK_LEVEL) fatal("Blocks nested too deeply");
/* MR23 */ CurBlockID_array[BlkLevel] = CurBlockID;
/* MR23 */ CurAltNum_array[BlkLevel] = CurAltNum;
>>
{ Pragma
( "approx" <<approx=LL_k;>>
| "LL\(1\)" <<approx = 1;>> /* MR20 */
| "LL\(2\)" <<approx = 2;>> /* MR20 */
)
}
/* MR21 */ { FirstSetSymbol
/* MR21 */ "\("
/* MR21 */ ( NonTerminal
/* MR21 */ <<
/* MR21 */ pFirstSetSymbol = (char *) calloc(strlen(LATEXT(1))+1,
/* MR21 */ sizeof(char));
/* MR21 */ require(pFirstSetSymbol!=NULL,
/* MR21 */ "cannot allocate first set name");
/* MR21 */ strcpy(pFirstSetSymbol, LATEXT(1));
/* MR21 */ >>
/* MR21 */ | TokenTerm
/* MR21 */ <<
/* MR21 */ pFirstSetSymbol = (char *) calloc(strlen(LATEXT(1))+1,
/* MR21 */ sizeof(char));
/* MR21 */ require(pFirstSetSymbol!=NULL,
/* MR21 */ "cannot allocate first set name");
/* MR21 */ strcpy(pFirstSetSymbol, LATEXT(1));
/* MR21 */ >>
/* MR21 */ )
/* MR21 */ "\)"
/* MR21 */ }
(
"\(" block[&toksrefd,&rulesrefd] "\)"
<<blk = $$ = $2;
/* MR23 */ CurBlockID_array[BlkLevel] = (-1);
/* MR23 */ CurAltNum_array[BlkLevel] = (-1);
--BlkLevel;
>>
( "\*" <<$$ = makeLoop($$,approx,pFirstSetSymbol);>>
| "\+" <<$$ = makePlus($$,approx,pFirstSetSymbol);>>
| "?"
(
( "=>" <<ampersandStyle=0;>>
| "&&" <<ampersandStyle=1;>> /* MR10 (g)? && <<p>>? */
)
Pred /* generalized predicate */
/* first make into a predicate */
<<$$ = buildAction(LATEXT(1),action_file,action_line,1);>>
<<act = (ActionNode *) ((Junction *)$$.left)->p1;>>
<<semDepth=predicateLookaheadDepth(act);>> /* MR10 */
<<if (numericActionLabel) { /* MR10 */
list_add(&NumericPredLabels,act); /* MR10 */
numericActionLabel=0; /* MR10 */
}; /* MR10 */
>>
{ <<char *a;>>
PassAction
<<
a = (char *)calloc(strlen(LATEXT(1))+1, sizeof(char));
require(a!=NULL, "rule element: cannot allocate predicate fail action");
strcpy(a, LATEXT(1));
act->pred_fail = a;
>>
}
<<if ($first_on_line) { /* MR7 */
CurAltStart=(Junction *)$$.left; /* MR7 */
altAdd(CurAltStart); /* MR7 */
};>>
<<$node = (Node *)act;>>
/* for now, just snag context */
<<
pred = computePredFromContextGuard(blk,&predMsgDone); /* MR10 */
if ( pred==NULL) { /* MR10 */
if ( !predMsgDone) err("invalid or missing context guard"); /* MR10 */
predMsgDone=1; /* MR10 */
} else { /* MR10 */
act->guardNodes=(Junction *)blk.left; /* MR11 */
pred->expr = act->action;
pred->source = act;
/* MR10 */ pred->ampersandStyle = ampersandStyle; /* 0 means (g)? => ... 1 means (g)? && ... */
/* MR13 */ if (pred->tcontext != NULL) {
/* MR13 */ height=MR_max_height_of_tree(pred->tcontext);
/* MR13 */ equal_height=MR_all_leaves_same_height(pred->tcontext,height);
/* MR13 */ if (! equal_height) {
/* MR13 */ errFL("in guarded predicates all tokens in the guard must be at the same height",
/* MR13 */ FileStr[act->file],act->line);
/* MR13 */ };
/* MR13 */ }
/* MR10 */ if (ampersandStyle) {
/* MR10 */ act->ampersandPred = pred;
/* MR11 */ if (! HoistPredicateContext) {
/* MR11 */ errFL("without \"-prc on\" (guard)? && <<pred>>? ... doesn't make sense",
/* MR11 */ FileStr[act->file],act->line);
/* MR11 */ };
/* MR10 */ } else {
/* MR10 */ act->guardpred = pred;
/* MR10 */ };
/* MR10 */ if (pred->k != semDepth) {
/* MR10 */ warn(eMsgd2("length of guard (%d) does not match the length of semantic predicate (%d)",
/* MR10 */ pred->k,semDepth));
/* MR10 */ };
}
>>
| <<$$ = makeBlk($$,approx,pFirstSetSymbol);
FoundGuessBlk = 1;
((Junction *) ((Junction
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -