31 |
#include "lalr.h" |
#include "lalr.h" |
32 |
#include "nullable.h" |
#include "nullable.h" |
33 |
#include "derives.h" |
#include "derives.h" |
34 |
|
#include "getargs.h" |
35 |
|
|
36 |
/* All the decorated states, indexed by the state number. Warning: |
/* All the decorated states, indexed by the state number. Warning: |
37 |
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. |
405 |
} |
} |
406 |
|
|
407 |
|
|
408 |
|
static void |
409 |
|
matrix_print (FILE *out, short **matrix, int n) |
410 |
|
{ |
411 |
|
int i, j; |
412 |
|
|
413 |
|
for (i = 0; i < n; ++i) |
414 |
|
{ |
415 |
|
fprintf (out, "%3d: ", i); |
416 |
|
if (matrix[i]) |
417 |
|
for (j = 0; matrix[i][j] != -1; ++j) |
418 |
|
fprintf (out, "%3d ", matrix[i][j]); |
419 |
|
fputc ('\n', out); |
420 |
|
} |
421 |
|
fputc ('\n', out); |
422 |
|
} |
423 |
|
|
424 |
/*-------------------------------------------------------------------. |
/*-------------------------------------------------------------------. |
425 |
| 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 | |
426 |
| replaced with the result. | |
| replaced with the result. | |
427 |
|
| | |
428 |
|
| R_ARG[I] is NULL or a -1 terminated list of numbers. | |
429 |
|
| | |
430 |
|
| RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM | |
431 |
|
| is in R_ARG[I]. | |
432 |
`-------------------------------------------------------------------*/ |
`-------------------------------------------------------------------*/ |
433 |
|
|
434 |
static short ** |
static short ** |
435 |
transpose (short **R_arg, int n) |
transpose (short **R_arg, int n) |
436 |
{ |
{ |
437 |
short **new_R; |
/* The result. */ |
438 |
short **temp_R; |
short **new_R = XCALLOC (short *, n); |
439 |
short *nedges; |
/* END_R[I] -- next entry of NEW_R[I]. */ |
440 |
int i; |
short **end_R = XCALLOC (short *, n); |
441 |
|
/* NEDGES[I] -- total size of NEW_R[I]. */ |
442 |
nedges = XCALLOC (short, n); |
short *nedges = XCALLOC (short, n); |
443 |
|
int i, j; |
444 |
|
|
445 |
for (i = 0; i < n; i++) |
if (trace_flag) |
446 |
{ |
{ |
447 |
short *sp = R_arg[i]; |
fputs ("transpose: input\n", stderr); |
448 |
if (sp) |
matrix_print (stderr, R_arg, n); |
|
{ |
|
|
while (*sp >= 0) |
|
|
nedges[*sp++]++; |
|
|
} |
|
449 |
} |
} |
450 |
|
|
451 |
new_R = XCALLOC (short *, n); |
/* Count. */ |
452 |
temp_R = XCALLOC (short *, n); |
for (i = 0; i < n; i++) |
453 |
|
if (R_arg[i]) |
454 |
|
for (j = 0; R_arg[i][j] >= 0; ++j) |
455 |
|
++nedges[R_arg[i][j]]; |
456 |
|
|
457 |
|
/* Allocate. */ |
458 |
for (i = 0; i < n; i++) |
for (i = 0; i < n; i++) |
459 |
if (nedges[i] > 0) |
if (nedges[i] > 0) |
460 |
{ |
{ |
461 |
short *sp = XCALLOC (short, nedges[i] + 1); |
short *sp = XCALLOC (short, nedges[i] + 1); |
|
new_R[i] = sp; |
|
|
temp_R[i] = sp; |
|
462 |
sp[nedges[i]] = -1; |
sp[nedges[i]] = -1; |
463 |
|
new_R[i] = sp; |
464 |
|
end_R[i] = sp; |
465 |
} |
} |
466 |
|
|
467 |
XFREE (nedges); |
/* Store. */ |
|
|
|
468 |
for (i = 0; i < n; i++) |
for (i = 0; i < n; i++) |
469 |
{ |
if (R_arg[i]) |
470 |
short *sp = R_arg[i]; |
for (j = 0; R_arg[i][j] >= 0; ++j) |
471 |
if (sp) |
{ |
472 |
while (*sp >= 0) |
*end_R[R_arg[i][j]] = i; |
473 |
*temp_R[*sp++]++ = i; |
++end_R[R_arg[i][j]]; |
474 |
} |
} |
475 |
|
|
476 |
XFREE (temp_R); |
free (nedges); |
477 |
|
free (end_R); |
478 |
|
|
479 |
/* Free the input: it is replaced with the result. */ |
/* Free the input: it is replaced with the result. */ |
480 |
for (i = 0; i < n; i++) |
for (i = 0; i < n; i++) |
481 |
XFREE (R_arg[i]); |
XFREE (R_arg[i]); |
482 |
XFREE (R_arg); |
free (R_arg); |
483 |
|
|
484 |
|
if (trace_flag) |
485 |
|
{ |
486 |
|
fputs ("transpose: output\n", stderr); |
487 |
|
matrix_print (stderr, new_R, n); |
488 |
|
} |
489 |
|
|
490 |
return new_R; |
return new_R; |
491 |
} |
} |