summaryrefslogtreecommitdiff
path: root/utils/Burg/sample.gr
blob: e1f7283b6d6a8fafe98fd921f15ab781c325e2d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
%{
#include <stdio.h>

typedef struct node *NODEPTR_TYPE;

struct node {
	int op, state_label;
	NODEPTR_TYPE left, right;
};

#define OP_LABEL(p)	((p)->op)
#define STATE_LABEL(p)	((p)->state_label)
#define LEFT_CHILD(p)	((p)->left)
#define RIGHT_CHILD(p)	((p)->right)
#define PANIC		printf
%}

%start reg
%term Assign=1 Constant=2 Fetch=3 Four=4 Mul=5 Plus=6
%%
con:  Constant                = 1 (0);
con:  Four                    = 2 (0);
addr: con                     = 3 (0);
addr: Plus(con,reg)           = 4 (0);
addr: Plus(con,Mul(Four,reg)) = 5 (0);
reg:  Fetch(addr)             = 6 (1);
reg:  Assign(addr,reg)        = 7 (1);

%%

#define Assign 1
#define Constant 2
#define Fetch 3
#define Four 4
#define Mul 5
#define Plus 6

#ifdef __STDC__
#define ARGS(x) x
#else
#define ARGS(x) ()
#endif

NODEPTR_TYPE buildtree ARGS((int, NODEPTR_TYPE, NODEPTR_TYPE));
void printcover ARGS((NODEPTR_TYPE, int, int));
void printtree ARGS((NODEPTR_TYPE));
int treecost ARGS((NODEPTR_TYPE, int, int));
void printMatches ARGS((NODEPTR_TYPE));
int main ARGS((void));

NODEPTR_TYPE buildtree(op, left, right) int op; NODEPTR_TYPE left; NODEPTR_TYPE right; {
	NODEPTR_TYPE p;
	extern void *malloc ARGS((unsigned));

	p = (NODEPTR_TYPE) malloc(sizeof *p);
	p->op = op;
	p->left = left;
	p->right = right;
	return p;
}

void printcover(p, goalnt, indent) NODEPTR_TYPE p; int goalnt; int indent; {
	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(p) NODEPTR_TYPE p; {
	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(p, goalnt, costindex) NODEPTR_TYPE p; int goalnt; int costindex; {
	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;
}

void printMatches(p) NODEPTR_TYPE p; {
	int nt;
	int eruleno;

	printf("Node 0x%lx= ", (unsigned long)p);
	printtree(p);
	printf(" matched rules:\n");
	for (nt = 1; burm_ntname[nt] != (char*)NULL; nt++)
		if ((eruleno = burm_rule(STATE_LABEL(p), nt)) != 0)
			printf("\t%s\n", burm_string[eruleno]);
}

main() {
	NODEPTR_TYPE p;

	p = buildtree(Assign,
		buildtree(Constant, 0, 0),
		buildtree(Fetch,
			buildtree(Plus,
				buildtree(Constant, 0, 0),
				buildtree(Mul,
					buildtree(Four, 0, 0),
					buildtree(Fetch, buildtree(Constant, 0, 0), 0)
				)
			),
			0
		)
	);
	printtree(p);
	printf("\n\n");
	burm_label(p);
	printcover(p, 1, 0);
	printf("\nCover cost == %d\n\n", treecost(p, 1, 0));
	printMatches(p);
	return 0;
}