4 typedef struct node *NODEPTR_TYPE;
8 NODEPTR_TYPE left, right;
11 #define OP_LABEL(p) ((p)->op)
12 #define STATE_LABEL(p) ((p)->state_label)
13 #define LEFT_CHILD(p) ((p)->left)
14 #define RIGHT_CHILD(p) ((p)->right)
19 %term Assign=1 Constant=2 Fetch=3 Four=4 Mul=5 Plus=6
21 con: Constant = 1 (0);
24 addr: Plus(con,reg) = 4 (0);
25 addr: Plus(con,Mul(Four,reg)) = 5 (0);
26 reg: Fetch(addr) = 6 (1);
27 reg: Assign(addr,reg) = 7 (1);
44 NODEPTR_TYPE buildtree ARGS((int, NODEPTR_TYPE, NODEPTR_TYPE));
45 void printcover ARGS((NODEPTR_TYPE, int, int));
46 void printtree ARGS((NODEPTR_TYPE));
47 int treecost ARGS((NODEPTR_TYPE, int, int));
48 void printMatches ARGS((NODEPTR_TYPE));
49 int main ARGS((void));
51 NODEPTR_TYPE buildtree(op, left, right) int op; NODEPTR_TYPE left; NODEPTR_TYPE right; {
53 extern void *malloc ARGS((unsigned));
55 p = (NODEPTR_TYPE) malloc(sizeof *p);
62 void printcover(p, goalnt, indent) NODEPTR_TYPE p; int goalnt; int indent; {
63 int eruleno = burm_rule(STATE_LABEL(p), goalnt);
64 short *nts = burm_nts[eruleno];
65 NODEPTR_TYPE kids[10];
72 for (i = 0; i < indent; i++)
74 printf("%s\n", burm_string[eruleno]);
75 burm_kids(p, eruleno, kids);
76 for (i = 0; nts[i]; i++)
77 printcover(kids[i], nts[i], indent+1);
80 void printtree(p) NODEPTR_TYPE p; {
81 int op = burm_op_label(p);
83 printf("%s", burm_opname[op]);
84 switch (burm_arity[op]) {
89 printtree(burm_child(p, 0));
94 printtree(burm_child(p, 0));
96 printtree(burm_child(p, 1));
102 int treecost(p, goalnt, costindex) NODEPTR_TYPE p; int goalnt; int costindex; {
103 int eruleno = burm_rule(STATE_LABEL(p), goalnt);
104 int cost = burm_cost[eruleno][costindex], i;
105 short *nts = burm_nts[eruleno];
106 NODEPTR_TYPE kids[10];
108 burm_kids(p, eruleno, kids);
109 for (i = 0; nts[i]; i++)
110 cost += treecost(kids[i], nts[i], costindex);
114 void printMatches(p) NODEPTR_TYPE p; {
118 printf("Node 0x%lx= ", (unsigned long)p);
120 printf(" matched rules:\n");
121 for (nt = 1; burm_ntname[nt] != (char*)NULL; nt++)
122 if ((eruleno = burm_rule(STATE_LABEL(p), nt)) != 0)
123 printf("\t%s\n", burm_string[eruleno]);
129 p = buildtree(Assign,
130 buildtree(Constant, 0, 0),
133 buildtree(Constant, 0, 0),
135 buildtree(Four, 0, 0),
136 buildtree(Fetch, buildtree(Constant, 0, 0), 0)
146 printf("\nCover cost == %d\n\n", treecost(p, 1, 0));