* src/lalr.c (matrix_print): New.

(transpose): Use it.
Use arrays instead of pointers.
This commit is contained in:
Akim Demaille
2001-12-05 09:18:25 +00:00
parent 6a1e01f060
commit 963c0cf62f
2 changed files with 62 additions and 27 deletions

View File

@@ -1,3 +1,9 @@
2001-12-05 Akim Demaille <akim@epita.fr>
* src/lalr.c (matrix_print): New.
(transpose): Use it.
Use arrays instead of pointers.
2001-12-05 Akim Demaille <akim@epita.fr> 2001-12-05 Akim Demaille <akim@epita.fr>
* src/lalr.c (maxrhs): Move to... * src/lalr.c (maxrhs): Move to...

View File

@@ -31,6 +31,7 @@
#include "lalr.h" #include "lalr.h"
#include "nullable.h" #include "nullable.h"
#include "derives.h" #include "derives.h"
#include "getargs.h"
/* All the decorated states, indexed by the state number. Warning: /* All the decorated states, indexed by the state number. Warning:
there is a state_TABLE in LR0.c, but it is different and static. there is a state_TABLE in LR0.c, but it is different and static.
@@ -404,59 +405,87 @@ add_lookback_edge (int stateno, int ruleno, int gotono)
} }
static void
matrix_print (FILE *out, short **matrix, int n)
{
int i, j;
for (i = 0; i < n; ++i)
{
fprintf (out, "%3d: ", i);
if (matrix[i])
for (j = 0; matrix[i][j] != -1; ++j)
fprintf (out, "%3d ", matrix[i][j]);
fputc ('\n', out);
}
fputc ('\n', out);
}
/*-------------------------------------------------------------------. /*-------------------------------------------------------------------.
| Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is | | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
| replaced with the result. | | replaced with the result. |
| |
| R_ARG[I] is NULL or a -1 terminated list of numbers. |
| |
| RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
| is in R_ARG[I]. |
`-------------------------------------------------------------------*/ `-------------------------------------------------------------------*/
static short ** static short **
transpose (short **R_arg, int n) transpose (short **R_arg, int n)
{ {
short **new_R; /* The result. */
short **temp_R; short **new_R = XCALLOC (short *, n);
short *nedges; /* END_R[I] -- next entry of NEW_R[I]. */
int i; short **end_R = XCALLOC (short *, n);
/* NEDGES[I] -- total size of NEW_R[I]. */
short *nedges = XCALLOC (short, n);
int i, j;
nedges = XCALLOC (short, n); if (trace_flag)
for (i = 0; i < n; i++)
{ {
short *sp = R_arg[i]; fputs ("transpose: input\n", stderr);
if (sp) matrix_print (stderr, R_arg, n);
{
while (*sp >= 0)
nedges[*sp++]++;
}
} }
new_R = XCALLOC (short *, n); /* Count. */
temp_R = XCALLOC (short *, n); for (i = 0; i < n; i++)
if (R_arg[i])
for (j = 0; R_arg[i][j] >= 0; ++j)
++nedges[R_arg[i][j]];
/* Allocate. */
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
if (nedges[i] > 0) if (nedges[i] > 0)
{ {
short *sp = XCALLOC (short, nedges[i] + 1); short *sp = XCALLOC (short, nedges[i] + 1);
new_R[i] = sp;
temp_R[i] = sp;
sp[nedges[i]] = -1; sp[nedges[i]] = -1;
new_R[i] = sp;
end_R[i] = sp;
} }
XFREE (nedges); /* Store. */
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ if (R_arg[i])
short *sp = R_arg[i]; for (j = 0; R_arg[i][j] >= 0; ++j)
if (sp) {
while (*sp >= 0) *end_R[R_arg[i][j]] = i;
*temp_R[*sp++]++ = i; ++end_R[R_arg[i][j]];
} }
XFREE (temp_R); free (nedges);
free (end_R);
/* Free the input: it is replaced with the result. */ /* Free the input: it is replaced with the result. */
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
XFREE (R_arg[i]); XFREE (R_arg[i]);
XFREE (R_arg); free (R_arg);
if (trace_flag)
{
fputs ("transpose: output\n", stderr);
matrix_print (stderr, new_R, n);
}
return new_R; return new_R;
} }