Wed Feb 20 22:10:48 CET 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / monoburg / sample.brg
1 /* 
2  * This header (everything before the first "%%") is copied 
3  * directly to the output.
4  */
5
6 #include <glib.h>
7 #include <stdio.h>
8 #include <string.h>
9
10 #define MBTREE_TYPE  MBTree
11
12 typedef struct _MBTree MBTree;
13 struct _MBTree {
14         guint16 op;
15         MBTree *left, *right;
16         gpointer state;
17 };
18
19 %% these are the monoburg definition
20 #
21 # This is the start of the definitions
22 # comments start with a '#' as first line character
23 #
24
25 #
26 # we must fisrt define the terminals
27 # with or without numbers
28
29 %term Assign Constant Fetch=3 Four=8 Mul=5 Plus=6
30
31 #
32 # optional start nonterminal
33 #
34 %start reg
35
36 con:  Constant                0 
37
38 con:  Four                    0 
39
40 addr: con                     0 
41
42 addr: Plus(con,reg)           
43 {
44         int ern = mono_burg_rule (tree->state, MB_NTERM_addr);
45         printf ("%s\n", mono_burg_rule_string [ern]);
46 } cost 
47 {  
48   return 1;
49 }
50
51 addr: Plus(con,Mul(Four,reg)) 2 
52 {
53         int ern = mono_burg_rule (tree->state, MB_NTERM_addr);
54         printf ("%s\n", mono_burg_rule_string [ern]);
55
56
57 reg:  Fetch(addr)             1 
58 {
59         int ern = mono_burg_rule (tree->state, MB_NTERM_reg);
60         printf ("%s\n", mono_burg_rule_string [ern]);
61 }
62
63 reg:  Assign(addr,reg)        1 
64 {
65         int ern = mono_burg_rule (tree->state, MB_NTERM_reg);
66         printf ("%s\n", mono_burg_rule_string [ern]);
67 }
68
69 %% the rest is also copied directly to the output
70 /* everything below the second "%%" is also copied directly
71  * to the output file.
72  */
73
74 static MBTree *
75 create_tree (int op, MBTree *left, MBTree *right)
76 {
77         MBTree *t = g_new0 (MBTree, 1);
78         
79         t->op = op;
80         t->left = left;
81         t->right = right;
82
83         return t;
84 }
85
86 static void
87 reduce (MBTree *tree, int goal) 
88 {
89         MBTree *kids[10];
90         int ern = mono_burg_rule (tree->state, goal);
91         guint16 *nts = mono_burg_nts [ern];
92         int i, n;
93
94         mono_burg_kids (tree, ern, kids);
95
96         // printf ("TEST %d %d %s %d\n", goal, ern, mono_burg_rule_string [ern], nts [0]);
97         
98         for (i = 0; nts [i]; i++) 
99                 reduce (kids [i], nts [i]);
100
101         n = (tree->left != NULL) + (tree->right != NULL);
102
103         if (n) { /* not a terminal */
104           // printf ("XXTE %s %d\n", mono_burg_rule_string [ern], n);
105                 if (mono_burg_func [ern])
106                         mono_burg_func [ern] (tree, NULL);
107                 else
108                         g_warning ("no code for rule %s\n", 
109                                    mono_burg_rule_string [ern]);
110         } else {
111                 if (mono_burg_func [ern])
112                         g_warning ("unused code in rule %s\n", 
113                                    mono_burg_rule_string [ern]);
114         }
115 }
116
117 int
118 main ()
119 {
120         MBTree *t, *l, *r;
121         MBState *s;
122
123         l = create_tree (MB_TERM_Constant, NULL, NULL);
124
125         r = create_tree (MB_TERM_Fetch, l, NULL);
126         l = create_tree (MB_TERM_Constant, NULL, NULL);
127
128         r = create_tree (MB_TERM_Assign, l, r);
129         l = create_tree (MB_TERM_Four, NULL, NULL);
130
131         r = create_tree (MB_TERM_Mul, l, r);
132         l = create_tree (MB_TERM_Constant, NULL, NULL);
133
134         l = create_tree (MB_TERM_Plus, l, r);
135
136         t = create_tree (MB_TERM_Fetch, l, NULL);
137
138         s = mono_burg_label (t, NULL);
139
140         g_assert (s);
141
142         reduce (t, MB_NTERM_reg);
143
144         return 0;
145 }