Compilation Methods SS 2013 - File utils.c
/* shortcut functions to simplify construction of trees */
#define iconst maketree(1, 0, 0)
#define var maketree(2, 0, 0)
#define iadd(a, b) maketree(3, a, b)
#define deref(a) maketree(4, a, 0)
#define assign(a, b) maketree(5, a, b)
NODEPTR_TYPE maketree (int, NODEPTR_TYPE, NODEPTR_TYPE);
void printcover (NODEPTR_TYPE, int, int);
void printtree (NODEPTR_TYPE);
int treecost (NODEPTR_TYPE, int, int);
void printMatches (NODEPTR_TYPE);
int main (void);
NODEPTR_TYPE maketree(int op, NODEPTR_TYPE left, NODEPTR_TYPE right)
/* create a tree node and connect it to its children */
{
NODEPTR_TYPE p;
p = (NODEPTR_TYPE) malloc(sizeof *p);
p->op = op;
p->left = left;
p->right = right;
return p;
}
void printcover(NODEPTR_TYPE p, int goalnt, int indent)
/* textual output of resulting tree cover with patterns */
{
int eruleno = burm_rule(STATE_LABEL(p), goalnt);
short *nts = burm_nts[eruleno];
NODEPTR_TYPE kids[10];
int i;
if (eruleno == 0) {
printf("no cover\n");
return;
}
for (i = 0; i < indent; i++)
printf(".");
printf("%s\n", burm_string[eruleno]);
burm_kids(p, eruleno, kids);
for (i = 0; nts[i]; i++)
printcover(kids[i], nts[i], indent+1);
}
void printtree(NODEPTR_TYPE p)
/* textual output of tree */
{
int op = burm_op_label(p);
printf("%s", burm_opname[op]);
switch (burm_arity[op]) {
case 0:
break;
case 1:
printf("(");
printtree(burm_child(p, 0));
printf(")");
break;
case 2:
printf("(");
printtree(burm_child(p, 0));
printf(", ");
printtree(burm_child(p, 1));
printf(")");
break;
}
}
int treecost(NODEPTR_TYPE p, int goalnt, int costindex)
/* compute total cost of tree cover */
{
int eruleno = burm_rule(STATE_LABEL(p), goalnt);
int cost = burm_cost[eruleno][costindex], i;
short *nts = burm_nts[eruleno];
NODEPTR_TYPE kids[10];
burm_kids(p, eruleno, kids);
for (i = 0; nts[i]; i++)
cost += treecost(kids[i], nts[i], costindex);
return cost;
}
Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 13.06.2017


