Răsfoiți Sursa

2001-09-08 Dietmar Maurer <[email protected]>

	* monoburg.c (emit_header): added some convenient macros

	* monoburg.y (optcfunc): allow arbitrary cost functions

svn path=/trunk/mono/; revision=763
Dietmar Maurer 24 ani în urmă
părinte
comite
b358ec1260

+ 6 - 0
mono/monoburg/ChangeLog

@@ -1,3 +1,9 @@
+2001-09-08  Dietmar Maurer  <[email protected]>
+
+	* monoburg.c (emit_header): added some convenient macros
+
+	* monoburg.y (optcfunc): allow arbitrary cost functions
+
 2001-09-06  Dietmar Maurer  <[email protected]>
 
 	* monoburg.c (emit_header): use macros to access the tree (like in iburg)

+ 45 - 1
mono/monoburg/monoburg.c

@@ -33,16 +33,28 @@ static void output (char *fmt, ...)
 }
 
 void     
-create_rule (char *id, Tree *tree, char *code, char *cost)
+create_rule (char *id, Tree *tree, char *code, char *cost, char *cfunc)
 {
 	Rule *rule = g_new0 (Rule, 1);
 
+	if (!cfunc && !cost)
+		cost = "0";
+
 	rule->lhs = nonterm (id);
 	rule->tree = tree;
 	rule_list = g_list_append (rule_list, rule);
 	rule->cost = cost;
+	rule->cfunc = cfunc;
 	rule->code = code;
 
+	if (cfunc) {
+		if (cost)
+			yyerror ("duplicated costs (constant costs and cost function)");
+		else 
+			rule->cost = g_strdup_printf ("mono_burg_cost_%d (tree)",
+						      g_list_length (rule_list));
+	}
+
 	rule->lhs->rules = g_list_append (rule->lhs->rules, rule);
 
 	if (tree->op)
@@ -211,6 +223,14 @@ emit_header ()
 	output ("#ifndef MBTREE_RIGHT\n#define MBTREE_RIGHT(t) ((t)->right)\n#endif\n");
 	output ("#ifndef MBTREE_STATE\n#define MBTREE_STATE(t) ((t)->state)\n#endif\n");
 
+	output ("\n");
+	output ("#define MBMAXCOST 32768\n");
+
+	output ("\n");
+	output ("#define MBCOND(x) if (!x) return MBMAXCOST;\n");
+
+	output ("\n");
+
 	for (l = term_list; l; l = l->next) {
 		Term *t = (Term *)l->data;
 		if (t->number == -1)
@@ -582,6 +602,29 @@ emit_emitter_func ()
 	output ("};\n\n");
 }
 
+static void
+emit_cost_func ()
+{
+	GList *l;
+	int i;
+
+	for (l =  rule_list, i = 0; l; l = l->next) {
+		Rule *rule = (Rule *)l->data;
+		
+		if (rule->cfunc) {
+			output ("static guint16\n");
+
+			emit_rule_string (rule, "");
+
+			output ("mono_burg_cost_%d (MBTREE_TYPE *tree)\n", i + 1);
+			output ("{\n");
+			output ("%s\n", rule->cfunc);
+			output ("}\n\n");
+		}
+		i++;
+	}
+}
+
 static void
 emit_closure ()
 {
@@ -834,6 +877,7 @@ main (int argc, char *argv [])
 	}
 	
 	emit_vardefs ();
+	emit_cost_func ();
 	emit_emitter_func ();
 	emit_decoders ();
 

+ 3 - 1
mono/monoburg/monoburg.h

@@ -43,6 +43,7 @@ struct _Rule {
 	Tree *tree;
 	char *code;
 	char *cost;
+	char *cfunc;
 };
 
 
@@ -54,7 +55,8 @@ NonTerm *nonterm        (char *id);
 
 void     start_nonterm  (char *id);
 
-void     create_rule    (char *id, Tree *tree, char *code, char *cost);
+void     create_rule    (char *id, Tree *tree, char *code, char *cost, 
+			 char *cfunc);
 
 void     yyparsetail    (void);
 

+ 11 - 6
mono/monoburg/monoburg.y

@@ -32,11 +32,13 @@ static int yylinepos = 0;
 %token <text> CODE
 %token <text> STRING
 %token  START
+%token  COST
 %token  TERM
 %token <ivalue> INTEGER
 
 %type   <tree>          tree
 %type   <text>          optcost
+%type   <text>          optcfunc
 %type   <text>          optcode
 
 %%
@@ -44,7 +46,7 @@ static int yylinepos = 0;
 decls   : /* empty */ 
 	| START IDENT { start_nonterm ($2); } decls
 	| TERM  tlist decls
-	| IDENT ':' tree optcost optcode { create_rule ($1, $3, $5, $4); } decls 
+	| IDENT ':' tree optcost optcode optcfunc { create_rule ($1, $3, $5, $4, $6); } decls 
 	;
 
 optcode : /* empty */ { $$ = NULL }
@@ -61,11 +63,14 @@ tree	: IDENT { $$ = create_tree ($1, NULL, NULL); }
 	| IDENT '(' tree ',' tree ')' { $$ = create_tree ($1, $3, $5); }
 	;
 
-optcost : /* empty */ {$$ = "0"; }
+optcost : /* empty */ {$$ = NULL; }
 	| STRING
 	| INTEGER { $$ = g_strdup_printf ("%d", $1); }
 	;
 
+optcfunc : /*empty */ { $$ = NULL; }
+	 | COST CODE { $$ = $2; }
+	 ;
 %%
 
 static char *
@@ -189,10 +194,10 @@ yylex (void)
       char *n = next;
       int l;
 
-      //if (!strncmp (next - 1, "cost", 4) && isspace (next[3])) {
-      //next += 4;
-      //return COST;
-      //}
+      if (!strncmp (next - 1, "cost", 4) && isspace (next[3])) {
+	next += 4;
+	return COST;
+      }
 
       while (isalpha (*n) || isdigit (*n) || (*n == '_')) 
 	      n++;

+ 5 - 2
mono/monoburg/sample.brg

@@ -38,11 +38,14 @@ con:  Four                    0
 
 addr: con                     0 
 
-addr: Plus(con,reg)           0 
+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 
 {