Commit be4e00aa authored by Michaël Cadilhac's avatar Michaël Cadilhac
Browse files

from Michael Cadilhac <michael.cadilhac@lrde.epita.fr>

	* parser.y: Add parsing of optional `name:' before a rule member.
	* monoburg.h,
	* monoburg.c: Add optional rule naming.
parent 84cc72a5
from Michael Cadilhac <michael.cadilhac@lrde.epita.fr>
* parser.y: Add parsing of optional `name:' before a rule member.
* monoburg.h,
* monoburg.c: Add optional rule naming.
2005-06-14 Roland Levillain <roland@lrde.epita.fr>
* configure.ac: Bump to 1.0.1a.
......
......@@ -101,8 +101,30 @@ create_rule (char *id, Tree *tree, char *code, char *cost, char *cfunc)
rule_add (rule, code, cost, cfunc);
}
void
check_varname (char *varname, Tree *t)
{
if (t->varname && !strcmp (varname, t->varname))
yyerror ("variable name `%s' redefined");
if (t->left)
check_varname (varname, t->left);
if (t->right)
check_varname (varname, t->right);
}
void
check_varnames (Tree *t_source, Tree *t_on)
{
if (t_source->varname)
check_varname (t_source->varname, t_on);
if (t_source->left)
check_varnames (t_source->left, t_on);
if (t_source->right)
check_varnames (t_source->right, t_on);
}
Tree *
create_tree (char *id, Tree *left, Tree *right)
create_tree (char *id, char *varname, Tree *left, Tree *right)
{
int arity = (left != NULL) + (right != NULL);
Term *term = NULL;
......@@ -139,6 +161,19 @@ create_tree (char *id, Tree *left, Tree *right)
tree->nonterm = nonterm (id);
}
if (varname) {
if (tree->left)
check_varname (varname, tree->left);
if (tree->right)
check_varname (varname, tree->right);
tree->varname = g_strdup (varname);
} else {
tree->varname = NULL;
}
if (tree->left && tree->right)
check_varnames (tree->left, tree->right);
return tree;
}
......@@ -804,6 +839,50 @@ emit_kids ()
}
static void
emit_tree_variables (char *prefix, int depth, Tree *t)
{
char *tn;
int i;
if (t->varname) {
if (with_references) {
output ("MBTREE_TYPE &%s = *(%s(&tree))",
t->varname, prefix);
} else {
output ("MBTREE_TYPE *%s = (%s(tree))",
t->varname, prefix);
}
for (i = 0; i < depth; ++i)
output (")");
output (";\n");
}
if (t->left) {
tn = g_strconcat ("MBTREE_LEFT(", prefix, NULL);
emit_tree_variables (tn, depth + 1, t->left);
g_free (tn);
}
if (t->right) {
tn = g_strconcat ("MBTREE_RIGHT(", prefix, NULL);
emit_tree_variables (tn, depth + 1, t->right);
g_free (tn);
}
}
static void
emit_rule_variables (Rule *rule)
{
Tree *t = rule->tree;
if (t) {
emit_tree_variables ("", 0, t);
}
}
static void
emit_emitter_func ()
{
......@@ -836,7 +915,9 @@ emit_emitter_func ()
output ("(void) tree; (void) s;");
if (dag_mode)
output (" (void) state;");
output ("\n%s\n", rule->code);
output ("\n");
emit_rule_variables (rule);
output ("%s\n", rule->code);
output ("}\n\n");
g_hash_table_insert (cache, rule->code, GINT_TO_POINTER (i));
}
......@@ -1217,7 +1298,9 @@ main (int argc, char *argv [])
} else if (argv [i][1] == '-') {
/* Long options */
if (strcmp (argv [i] + 2, "without-glib") == 0) {
if (strcmp (argv [i] + 2, "help") == 0) {
usage (argv [0]);
} else if (strcmp (argv [i] + 2, "without-glib") == 0) {
with_glib = FALSE;
} else if (strcmp(argv [i] + 2, "version") == 0) {
version ();
......
......@@ -45,6 +45,7 @@ struct _Tree {
Term *op;
Tree *left;
Tree *right;
char *varname;
NonTerm *nonterm; /* used by chain rules */
};
......@@ -68,7 +69,7 @@ struct _File {
extern File inputs[MAX_FDS];
Tree *create_tree (char *id, Tree *left, Tree *right);
Tree *create_tree (char *id, char *varname, Tree *left, Tree *right);
Term *create_term (char *id, int num);
......
......@@ -43,6 +43,7 @@
%type <text> optcost
%type <text> optcfunc
%type <text> optcode
%type <text> optvarname
%type <rule> rule
%type <rule_list> rule_list
......@@ -89,9 +90,15 @@ tlist : /* empty */
| tlist IDENT '=' INTEGER { create_term ($2, $4); }
;
tree : IDENT { $$ = create_tree ($1, NULL, NULL); }
| IDENT '(' tree ')' { $$ = create_tree ($1, $3, NULL); }
| IDENT '(' tree ',' tree ')' { $$ = create_tree ($1, $3, $5); }
tree : IDENT optvarname {
$$ = create_tree ($2 ? $2 : $1, $2 ? $1 : NULL, NULL, NULL);
}
| IDENT optvarname '(' tree ')' {
$$ = create_tree ($2 ? $2 : $1, $2 ? $1 : NULL, $4, NULL);
}
| IDENT optvarname '(' tree ',' tree ')' {
$$ = create_tree ($2 ? $2 : $1, $2 ? $1 : NULL, $4, $6);
}
;
optcost : /* empty */ {$$ = NULL; }
......@@ -102,6 +109,10 @@ optcost : /* empty */ {$$ = NULL; }
optcfunc : /*empty */ { $$ = NULL; }
| COST CODE { $$ = $2; }
;
optvarname: /* empty */ { $$ = NULL; }
| ':' IDENT { $$ = $2; }
%%
static char input[2048];
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment