📄 antlr.g
字号:
ANDnode=new_pred();
ANDnode->expr=PRED_AND_LIST;
if (predExpr != NULL) {
ANDnode->down=predExpr;
tail=&predExpr->right;
};
>>
( "&&" predPrimary>[predExpr]
<<
if (predExpr != NULL) {
*tail=predExpr;
tail=&predExpr->right;
};
>>
)*
<<
$result=ANDnode;
ANDnode=NULL;
>>
;
/* fail */
<<predicate_free(ANDnode);>>
/* rule predPrimary */
predPrimary>[Predicate *result] :
<<
char *name=NULL;
PredEntry *predEntry=NULL;
Predicate *predExpr=NULL;
>>
TokenTerm <<name=mystrdup(LATEXT(1));>>
<<
predEntry=(PredEntry *) hash_get(Pname,name);
if (predEntry == NULL) {
warnFL(eMsg1("no previously defined #pred with name \"%s\"",name),
FileStr[CurFile],zzline);
name=NULL;
$result=NULL;
} else {
predExpr=predicate_dup(predEntry->pred);
predExpr->predEntry=predEntry;
$result=predExpr;
};
>>
| "\(" predOrExpr>[predExpr] "\)"
<<
$result=predExpr;
>>
| "!" predPrimary>[predExpr]
<<
predExpr->inverted=!predExpr->inverted;
$result=predExpr;
>>
;
/* fail */ <<
predicate_free(predExpr);
>>
/* rule aLexclass */
aLexclass: "{\\}#lexclass" TokenTerm <<lexclass(mystrdup(LATEXT(1)));>>
;
<<CannotContinue=TRUE;>>
/* rule error */
error : <<char *t=NULL; ECnode *e; int go=1; TermEntry *p;>>
"{\\}#errclass"
(<<;>> TokenTerm <<t=mystrdup(LATEXT(1));>>
| QuotedTerm <<t=mystrdup(LATEXT(1));>>
)
<<e = newECnode;
require(e!=NULL, "cannot allocate error class node");
e->lexclass = CurrentLexClass;
if ( Tnum( (t=StripQuotes(t)) ) == 0 )
{
if ( hash_get(Texpr, t) != NULL )
warn(eMsg1("errclass name conflicts with regular expression '%s'",t));
e->tok = addTname( t );
set_orel(e->tok, &imag_tokens);
require((p=(TermEntry *)hash_get(Tname, t)) != NULL,
"hash table mechanism is broken");
p->classname = 1; /* entry is errclass name, not token */
list_add(&eclasses, (char *)e);
}
else
{
warn(eMsg1("redefinition of errclass or conflict w/token or tokclass '%s'; ignored",t));
free( (char *)e );
go=0;
}
>>
"\{"
( NonTerminal <<if ( go ) t=mystrdup(LATEXT(1));>>
| TokenTerm <<if ( go ) t=mystrdup(LATEXT(1));>>
| QuotedTerm <<if ( go ) t=mystrdup(LATEXT(1));>>
)
<<if ( go ) list_add(&(e->elist), t);>>
(
( NonTerminal <<if ( go ) t=mystrdup(LATEXT(1));>>
| TokenTerm <<if ( go ) t=mystrdup(LATEXT(1));>>
| QuotedTerm <<if ( go ) t=mystrdup(LATEXT(1));>>
)
<<if ( go ) list_add(&(e->elist), t);>>
)*
"\}"
;
<<CannotContinue=TRUE;>>
/* rule tclass */
tclass : <<char *t=NULL; TCnode *e; int go=1,tok,totok; TermEntry *p, *term, *toterm;>>
<<char *akaString=NULL; int save_file; int save_line;>>
<<char *totext=NULL; >>
"{\\}#tokclass" TokenTerm <<t=mystrdup(LATEXT(1));>>
<<e = newTCnode;
require(e!=NULL, "cannot allocate token class node");
e->lexclass = CurrentLexClass;
if ( Tnum( t ) == 0 )
{
e->tok = addTname( t );
set_orel(e->tok, &imag_tokens);
set_orel(e->tok, &tokclasses);
require((p=(TermEntry *)hash_get(Tname, t)) != NULL,
"hash table mechanism is broken");
p->classname = 1; /* entry is class name, not token */
p->tclass = e; /* save ptr to this tclass def */
list_add(&tclasses, (char *)e);
}
else
{
warn(eMsg1("redefinition of tokclass or conflict w/token '%s'; ignored",t));
free( (char *)e );
go=0;
}
>>
/* MR23 */ {
/* MR23 */ "\("
/* MR23 */ QuotedTerm
/* MR23 */ <<akaString=mystrdup(StripQuotes(LATEXT(1)));
/* MR11 */ save_file=CurFile;save_line=zzline;
/* MR23 */ >>
/* MR23 */ "\)"
/* MR23 */ }
/* MR23 */
/* MR23 */
/* MR23 */ <<
/* MR23 */ if (p!= NULL && akaString != NULL) {
/* MR23 */ if (p->akaString != NULL) {
/* MR23 */ if (strcmp(p->akaString,akaString) != 0) {
/* MR23 */ warnFL(eMsg2("this #tokclass statment conflicts with a previous #tokclass %s(\"%s\") statement",
/* MR23 */ t,p->akaString),
/* MR23 */ FileStr[save_file],save_line);
/* MR23 */ };
/* MR23 */ } else {
/* MR23 */ p->akaString=akaString;
/* MR23 */ };
/* MR23 */ };
/* MR23 */ >>
"\{"
(
( TokenTerm
<<if ( go ) {
term = (TermEntry *) hash_get(Tname, LATEXT(1));
if ( term==NULL && UserDefdTokens ) {
err("implicit token definition not allowed with #tokdefs");
go = 0;
}
else {t=mystrdup(LATEXT(1)); tok=addTname(LATEXT(1));}
}>>
{
".."
TokenTerm
<<if ( go ) {
toterm = (TermEntry *) hash_get(Tname, LATEXT(1));
if ( toterm==NULL && UserDefdTokens ) {
err("implicit token definition not allowed with #tokdefs");
go = 0;
} else {
totext=mystrdup(LATEXT(1)); totok=addTname(LATEXT(1));
}
}
>>
}
| QuotedTerm
<<if ( go ) {
term = (TermEntry *) hash_get(Texpr, LATEXT(1));
if ( term==NULL && UserDefdTokens ) {
err("implicit token definition not allowed with #tokdefs");
go = 0;
}
else {t=mystrdup(LATEXT(1)); tok=addTexpr(LATEXT(1));}
}>>
)
<<if ( go ) {
if (totext == NULL) {
list_add(&(e->tlist), t);
} else {
list_add(&(e->tlist),"..");
list_add(&(e->tlist),t);
list_add(&(e->tlist),totext);
}
totext=NULL;
}
>>
)+ // MR15 Manfred Kogler - forbid empty #tokclass sets (was "+")
"\}"
;
<<CannotContinue=TRUE;>>
/* rule token */
token : <<char *t=NULL, *e=NULL, *a=NULL; int tnum=0;>>
<<char *akaString=NULL; TermEntry *te;int save_file=0,save_line=0;>> /* MR11 */
"{\\}#token"
/* MR1 10-Apr-97 MR1 Allow shift right operator in DLG actions */
/* MR1 Danger when parser feedback to lexer */
/* MR1 */
<<tokenActionActive=1;>> /* MR1 */
{ TokenTerm <<t=mystrdup(LATEXT(1));>>
/* MR11 */ {
/* MR11 */ "\("
/* MR11 */ QuotedTerm
/* MR11 */ <<akaString=mystrdup(StripQuotes(LATEXT(1)));
/* MR11 */ save_file=CurFile;save_line=zzline;
/* MR11 */ >>
/* MR11 */ "\)"
/* MR11 */ }
{ "=" "[0-9]+" /* define the token type number */
<<tnum = atoi(LATEXT(1));>>
}
}
{ QuotedTerm <<e=mystrdup(LATEXT(1));>> }
{ Action
<<
a = (char *) calloc(strlen(LATEXT(1))+1, sizeof(char));
require(a!=NULL, "rule token: cannot allocate action");
strcpy(a, LATEXT(1));
>>
}
{ ";" } /* MR11 */
<<chkToken(t, e, a, tnum);>>
<<if (t != NULL) {
te=(TermEntry *)hash_get(Tname,t);
if (te != NULL && akaString != NULL) {
if (te->akaString != NULL) {
if (strcmp(te->akaString,akaString) != 0) {
warnFL(eMsg2("this #token statment conflicts with a previous #token %s(\"%s\") statement",
t,te->akaString),
FileStr[save_file],save_line);
};
} else {
te->akaString=akaString;
};
};
};
>>
;
<<CannotContinue=TRUE;>>
/* rule block */
block[set *toksrefd, set *rulesrefd]
: <<
Graph g, b;
set saveblah;
int saveinalt = inAlt;
ExceptionGroup *eg;
*$toksrefd = empty;
*$rulesrefd = empty;
set_clr(AST_nodes_refd_in_actions);
CurBlockID++;
/* MR23 */ CurBlockID_array[BlkLevel] = CurBlockID;
CurAltNum = 1;
/* MR23 */ CurAltNum_array[BlkLevel] = CurAltNum;
saveblah = attribsRefdFromAction;
attribsRefdFromAction = empty;
>>
alt[toksrefd,rulesrefd] <<b = g = $1;>>
<<
if ( ((Junction *)g.left)->p1->ntype == nAction )
{
ActionNode *actionNode=(ActionNode *)
( ( (Junction *)g.left) ->p1);
if (!actionNode->is_predicate )
{
actionNode->init_action = TRUE;
/* MR12c */ if (actionNode->noHoist) {
/* MR12c */ errFL("<<nohoist>> appears as init-action - use <<>> <<nohoist>>",
/* MR12c */ FileStr[actionNode->file],actionNode->line);
/* MR12c */ };
}
}
((Junction *)g.left)->blockid = CurBlockID;
>>
( exception_group > [eg]
<<
if ( eg!=NULL ) {
/* MR7 ***** eg->altID = makeAltID(CurBlockID,CurAltNum); *****/
/* MR7 ***** CurAltStart->exception_label = eg->altID; *****/
list_add(&CurExGroups, (void *)eg);
}
>>
)*
<<CurAltNum++;
/* MR23 */ CurAltNum_array[BlkLevel] = CurAltNum;
>>
( "\|" <<inAlt=1;>>
alt[toksrefd,rulesrefd] <<g = Or(g, $2);>>
<<
((Junction *)g.left)->blockid = CurBlockID;
>>
( exception_group > [eg]
<<
if ( eg!=NULL ) {
/* MR7 ***** eg->altID = makeAltID(CurBlockID,CurAltNum); *****/
/* MR7 ***** CurAltStart->exception_label = eg->altID; *****/
list_add(&CurExGroups, (void *)eg);
}
>>
)*
<<CurAltNum++;
/* MR23 */ CurAltNum_array[BlkLevel] = CurAltNum;
>>
)*
<<$0 = b;>>
<<attribsRefdFromAction = saveblah; inAlt = saveinalt;>>
;
<<CannotContinue=TRUE;>>
/* rule alt */
alt[set *toksrefd, set *rulesrefd]
: <<int n=0; Graph g; int e_num=0, old_not=0; Node *node; set elems, dif;
int first_on_line = 1, use_def_MT_handler = 0;
g.left=NULL; g.right=NULL;
CurAltStart = NULL;
elems = empty;
inAlt = 1;
>>
{ "\@" /* handle MismatchedToken signals with default handler */
<<use_def_MT_handler = 1;>>
}
( <<;>> /* MR9 Removed unreferenced variable "tok" */
{ <<old_not=0;>> "\~" <<old_not=1;>> }
element[old_not, first_on_line, use_def_MT_handler] > [node]
<<if ( node!=NULL && node->ntype!=nAction ) first_on_line = 0;>>
<<
if ( $2.left!=NULL ) {
g = Cat(g, $2);
n++;
if ( node!=NULL ) {
if ( node->ntype!=nAction ) e_num++;
/* record record number of all rule and token refs */
if ( node->ntype==nToken ) {
TokNode *tk = (TokNode *)((Junction *)$2.left)->p1;
tk->elnum = e_num;
set_orel(e_num, &elems);
}
else if ( node->ntype==nRuleRef ) {
RuleRefNode *rn = (RuleRefNode *)((Junction *)$2.left)->p1;
rn->elnum = e_num;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -