2004-07-15 Dick Porter <dick@ximian.com>
[mono.git] / mono / monoburg / sample.brg
index f17a1066ad59dba2053185160bc16cc67196ca30..619b6fc821dd756886f403bee3faaba5da611871 100644 (file)
@@ -3,7 +3,18 @@
  * directly to the output.
  */
 
+#include <glib.h>
 #include <stdio.h>
+#include <string.h>
+
+#define MBTREE_TYPE  MBTree
+
+typedef struct _MBTree MBTree;
+struct _MBTree {
+       guint16 op;
+       MBTree *left, *right;
+       gpointer state;
+};
 
 %% these are the monoburg definition
 #
@@ -15,7 +26,7 @@
 # we must fisrt define the terminals
 # with or without numbers
 # 
-%term Assign Constant Fetch=3 Four=8 Mul=5 Plus=6
+%term Assign Constant Fetch=3 Four=8 Mul=5 Plus=6 AltFetch=7
 
 #
 # optional start nonterminal
@@ -28,11 +39,14 @@ con:  Four                    0
 
 addr: con                     0 
 
-addr: Plus(con,reg)           
+addr: Plus(con,reg)           
 {
        int ern = mono_burg_rule (tree->state, MB_NTERM_addr);
        printf ("%s\n", mono_burg_rule_string [ern]);
-} 
+} cost 
+{  
+  return 1;
+}
 
 addr: Plus(con,Mul(Four,reg)) 2 
 {
@@ -40,6 +54,7 @@ addr: Plus(con,Mul(Four,reg)) 2
        printf ("%s\n", mono_burg_rule_string [ern]);
 } 
 
+reg:  AltFetch(addr),
 reg:  Fetch(addr)             1 
 {
        int ern = mono_burg_rule (tree->state, MB_NTERM_reg);
@@ -75,17 +90,29 @@ reduce (MBTree *tree, int goal)
        MBTree *kids[10];
        int ern = mono_burg_rule (tree->state, goal);
        guint16 *nts = mono_burg_nts [ern];
-       int i;
+       int i, n;
 
        mono_burg_kids (tree, ern, kids);
 
-       //printf ("TEST %d %d %s\n", goal, ern, mono_burg_rule_string [ern]);
-
-       for (i = 0; nts [i]; i++)
+       // printf ("TEST %d %d %s %d\n", goal, ern, mono_burg_rule_string [ern], nts [0]);
+       
+       for (i = 0; nts [i]; i++) 
                reduce (kids [i], nts [i]);
 
-       if (mono_burg_func [ern])
-               mono_burg_func [ern] (tree);
+       n = (tree->left != NULL) + (tree->right != NULL);
+
+       if (n) { /* not a terminal */
+         // printf ("XXTE %s %d\n", mono_burg_rule_string [ern], n);
+               if (mono_burg_func [ern])
+                       mono_burg_func [ern] (tree, NULL);
+               else
+                       g_warning ("no code for rule %s\n", 
+                                  mono_burg_rule_string [ern]);
+       } else {
+               if (mono_burg_func [ern])
+                       g_warning ("unused code in rule %s\n", 
+                                  mono_burg_rule_string [ern]);
+       }
 }
 
 int
@@ -109,7 +136,7 @@ main ()
 
        t = create_tree (MB_TERM_Fetch, l, NULL);
 
-       s = mono_burg_label (t);
+       s = mono_burg_label (t, NULL);
 
        g_assert (s);