1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
|
/* $Id: lpgutil.c,v 1.2 1999/11/04 14:02:22 shields Exp $ */
/*
This software is subject to the terms of the IBM Jikes Compiler
License Agreement available at the following URL:
http://www.ibm.com/research/jikes.
Copyright (C) 1983, 1999, International Business Machines Corporation
and others. All Rights Reserved.
You must accept the terms of that agreement to use this software.
*/
static char hostfile[] = __FILE__;
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "common.h"
#include "header.h"
/**********************************************************************/
/* The following are global variables and constants used to manage a */
/* pool of temporary space. Externally, the user invokes the function */
/* "talloc" just as he would invoke "malloc". */
/**********************************************************************/
#ifdef DOS
#define LOG_BLKSIZE 12
#else
#define LOG_BLKSIZE 14
#endif
#define BLKSIZE (1 << LOG_BLKSIZE)
#define BASE_INCREMENT 64
typedef long cell;
static cell **temp_base = NULL;
static long temp_top = 0,
temp_size = 0,
temp_base_size = 0;
/**********************************************************************/
/* ALLOCATE_MORE_SPACE: */
/**********************************************************************/
/* This procedure obtains more TEMPORARY space. */
/**********************************************************************/
static BOOLEAN allocate_more_space(cell ***base, long *size, long *base_size)
{
int k;
/**********************************************************************/
/* The variable size always indicates the maximum number of cells */
/* that has been allocated and reserved for the storage pool. */
/* Initially, size should be set to 0 to indicate that no space has */
/* yet been allocated. The pool of cells available is divided into */
/* segments of size 2**LOG_BLKSIZE each and each segment is pointer */
/* to by a slot in the array base. */
/* */
/* By dividing "size" by the size of the segment we obtain the */
/* index for the next segment in base. If base is already full, it is */
/* reallocated. */
/* */
/**********************************************************************/
k = (*size) >> LOG_BLKSIZE; /* which segment? */
if (k == (*base_size)) /* base overflow? reallocate */
{
register int i = (*base_size);
(*base_size) += BASE_INCREMENT;
(*base) = (cell **)
((*base) == NULL ?
malloc(sizeof(cell *) * (*base_size)) :
realloc((*base), sizeof(cell *) * (*base_size)));
if ((*base) == (cell **) NULL)
return FALSE;
for (i = i; i < (*base_size); i++)
(*base)[i] = NULL;
}
/**********************************************************************/
/* If the Ast slot "k" does not already contain a segment, We try to */
/* allocate one and place its address in (*base)[k]. */
/* If the allocation was not successful, we terminate; */
/* otherwise, we adjust the address in (*base)[k] so as to allow us */
/* to index the segment directly, instead of having to perform a */
/* subtraction for each reference. Finally, we update size. */
/* */
/* Finally, we set the block to zeros. */
/**********************************************************************/
if ((*base)[k] == NULL)
{
(*base)[k] = (cell *) malloc(sizeof(cell) << LOG_BLKSIZE);
if ((*base)[k] == (cell *) NULL)
return FALSE;
(*base)[k] -= (*size);
}
memset((void *)((*base)[k] + (*size)), 0, sizeof(cell) << LOG_BLKSIZE);
(*size) += BLKSIZE;
return TRUE;
}
/**********************************************************************/
/* RESET_TEMPORARY_SPACE: */
/**********************************************************************/
/* This procedure resets the temporary space already allocated so */
/* that it can be reused before new blocks are allocated. */
/**********************************************************************/
void reset_temporary_space(void)
{
temp_top = 0; /* index of next usable elemt */
temp_size = 0;
return;
}
/**********************************************************************/
/* FREE_TEMPORARY_SPACE: */
/**********************************************************************/
/* This procedure frees all allocated temporary space. */
/**********************************************************************/
void free_temporary_space(void)
{
int k;
for (k = 0; k < temp_base_size && temp_base[k] != NULL; k++)
{
temp_base[k] += (k * BLKSIZE);
ffree(temp_base[k]);
}
if (temp_base != NULL)
{
ffree(temp_base);
temp_base = NULL;
}
temp_base_size = 0;
temp_top = 0;
temp_size = 0;
return;
}
/**********************************************************************/
/* TALLOC: */
/**********************************************************************/
/* talloc allocates an object of size "size" in temporary space and */
/* returns a pointer to it. */
/**********************************************************************/
void *talloc(long size)
{
long i;
i = temp_top;
temp_top += ((size + sizeof(cell) - 1) / sizeof(cell));
if (temp_top > temp_size)
{
i = temp_size;
temp_top = temp_size +
((size + sizeof(cell) - 1) / sizeof(cell));
if (! allocate_more_space(&temp_base, &temp_size, &temp_base_size))
{
temp_top = temp_size;
return NULL;
}
}
return ((void *) &(temp_base[i >> LOG_BLKSIZE] [i]));
}
/**********************************************************************/
/* TEMPORARY_SPACE_ALLOCATED: */
/**********************************************************************/
/* Return the total size of temporary space allocated. */
/**********************************************************************/
long temporary_space_allocated(void)
{
return ((temp_base_size * sizeof(cell **)) +
(temp_size * sizeof(cell)));
}
/**********************************************************************/
/* TEMPORARY_SPACE_USED: */
/**********************************************************************/
/* Return the total size of temporary space used. */
/**********************************************************************/
long temporary_space_used(void)
{
return (((temp_size >> LOG_BLKSIZE) * sizeof(cell **)) +
(temp_top * sizeof(cell)));
}
/**********************************************************************/
/* */
/* The following are global variables and constants used to manage a */
/* pool of global space. Externally, the user invokes one of the */
/* functions: */
/* */
/* ALLOCATE_NODE */
/* ALLOCATE_GOTO_MAP */
/* ALLOCATE_SHIFT_MAP */
/* ALLOCATE_REDUCE_MAP */
/* */
/* These functions allocate space from the global pool in the same */
/* using the function "galloc" below. */
/* */
/**********************************************************************/
static cell **global_base = NULL;
static long global_top = 0,
global_size = 0,
global_base_size = 0;
static struct node *node_pool = NULL;
/**********************************************************************/
/* PROCESS_GLOBAL_WASTE: */
/**********************************************************************/
/* This function is invoked when the space left in a segment is not */
/* enough for GALLOC to allocate a requested object. Rather than */
/* waste the space, as many NODE structures as possible are allocated */
/* in that space and stacked up in the NODE_POOL list. */
/**********************************************************************/
static void process_global_waste(long top)
{
struct node *p;
long i;
while (TRUE)
{
i = top;
top += ((sizeof(struct node) + sizeof(cell) - 1) / sizeof(cell));
if (top > global_size)
break;
p = (struct node *) &(global_base[i >> LOG_BLKSIZE] [i]);
p -> next = node_pool;
node_pool = p;
}
return;
}
/**********************************************************************/
/* GALLOC: */
/**********************************************************************/
/* galloc allocates an object of size "size" in global space and */
/* returns a pointer to it. It is analoguous to "talloc", but it */
/* is a local (static) routine that is only invoked in this file by */
/* other more specialized routines. */
/**********************************************************************/
static void *galloc(long size)
{
long i;
i = global_top;
global_top += ((size + sizeof(cell) - 1) / sizeof(cell));
if (global_top > global_size)
{
process_global_waste(i);
i = global_size;
global_top = global_size +
((size + sizeof(cell) - 1) / sizeof(cell));
if (! allocate_more_space(&global_base,
&global_size, &global_base_size))
{
global_top = global_size;
return NULL;
}
}
return ((void *) &(global_base[i >> LOG_BLKSIZE] [i]));
}
/****************************************************************************/
/* ALLOCATE_NODE: */
/****************************************************************************/
/* This function allocates a node structure and returns a pointer to it. */
/* it there are nodes in the free pool, one of them is returned. Otherwise, */
/* a new node is allocated from the global storage pool. */
/****************************************************************************/
struct node *allocate_node(char *file, long line)
{
struct node *p;
p = node_pool;
if (p != NULL) /* is free list not empty? */
node_pool = p -> next;
else
{
p = (struct node *) galloc(sizeof(struct node));
if (p == NULL)
nospace(file, line);
}
return(p);
}
/****************************************************************************/
/* FREE_NODES: */
/****************************************************************************/
/* This function frees a linked list of nodes by adding them to the free */
/* list. Head points to head of linked list and tail to the end. */
/****************************************************************************/
void free_nodes(struct node *head, struct node *tail)
{
tail -> next = node_pool;
node_pool = head;
return;
}
/****************************************************************************/
/* ALLOCATE_GOTO_MAP: */
/****************************************************************************/
/* This function allocates space for a goto map with "size" elements, */
/* initializes and returns a goto header for that map. NOTE that after the */
/* map is successfully allocated, it is offset by one element. This is */
/* to allow the array in question to be indexed from 1..size instead of */
/* 0..(size-1). */
/****************************************************************************/
struct goto_header_type allocate_goto_map(int size, char *file, long line)
{
struct goto_header_type go_to;
go_to.size = size;
go_to.map = (struct goto_type *)
galloc(size * sizeof(struct goto_type));
if (go_to.map == NULL)
nospace(file, line);
go_to.map--; /* map will be indexed in range 1..size */
return(go_to);
}
/****************************************************************************/
/* ALLOCATE_SHIFT_MAP: */
/****************************************************************************/
/* This function allocates space for a shift map with "size" elements, */
/* initializes and returns a shift header for that map. NOTE that after the */
/* map is successfully allocated, it is offset by one element. This is */
/* to allow the array in question to be indexed from 1..size instead of */
/* 0..(size-1). */
/****************************************************************************/
struct shift_header_type allocate_shift_map(int size,
char *file, long line)
{
struct shift_header_type sh;
sh.size = size;
sh.map = (struct shift_type *)
galloc(size * sizeof(struct shift_type));
if (sh.map == NULL)
nospace(file, line);
sh.map--; /* map will be indexed in range 1..size */
return(sh);
}
/****************************************************************************/
/* ALLOCATE_REDUCE_MAP: */
/****************************************************************************/
/* This function allocates space for a REDUCE map with "size"+1 elements, */
/* initializes and returns a REDUCE header for that map. The 0th element of */
/* a reduce map is used for the default reduction. */
/****************************************************************************/
struct reduce_header_type allocate_reduce_map(int size,
char *file, long line)
{
struct reduce_header_type red;
red.map = (struct reduce_type *)
galloc((size + 1) * sizeof(struct reduce_type));
if (red.map == NULL)
nospace(file, line);
red.size = size;
return(red);
}
/**********************************************************************/
/* GLOBAL_SPACE_ALLOCATED: */
/**********************************************************************/
/* Return the total size of global space allocated. */
/**********************************************************************/
long global_space_allocated(void)
{
return ((global_base_size * sizeof(cell **)) +
(global_size * sizeof(cell)));
}
/**********************************************************************/
/* GLOBAL_SPACE_USED: */
/**********************************************************************/
/* Return the total size of global space used. */
/**********************************************************************/
long global_space_used(void)
{
return (((global_size >> LOG_BLKSIZE) * sizeof(cell **)) +
(global_top * sizeof(cell)));
}
/****************************************************************************/
/* ALLOCATE_INT_ARRAY: */
/****************************************************************************/
/* This function allocates an array of size "size" of int integers. */
/****************************************************************************/
int *allocate_int_array(long size, char *file, long line)
{
int *p;
p = (int *) calloc(size, sizeof(int));
if (p == (int *) NULL)
nospace(file, line);
return(&p[0]);
}
/****************************************************************************/
/* ALLOCATE_SHORT_ARRAY: */
/****************************************************************************/
/* This function allocates an array of size "size" of short integers. */
/****************************************************************************/
short *allocate_short_array(long size, char *file, long line)
{
short *p;
p = (short *) calloc(size, sizeof(short));
if (p == (short *) NULL)
nospace(file, line);
return(&p[0]);
}
/****************************************************************************/
/* ALLOCATE_BOOLEAN_ARRAY: */
/****************************************************************************/
/* This function allocates an array of size "size" of type boolean. */
/****************************************************************************/
BOOLEAN *allocate_boolean_array(long size, char *file, long line)
{
BOOLEAN *p;
p = (BOOLEAN *) calloc(size, sizeof(BOOLEAN));
if (p == (BOOLEAN *) 0)
nospace(file, line);
return(&p[0]);
}
/*****************************************************************************/
/* FILL_IN: */
/*****************************************************************************/
/* FILL_IN is a subroutine that pads a buffer, STRING, with CHARACTER a */
/* certain AMOUNT of times. */
/*****************************************************************************/
void fill_in(char string[], int amount, char character)
{
int i;
for (i = 0; i <= amount; i++)
string[i] = character;
string[i] = '\0';
return;
}
/*****************************************************************************/
/* QCKSRT: */
/*****************************************************************************/
/* QCKSRT is a quicksort algorithm that takes as arguments an array of */
/* integers, two numbers L and H that indicate the lower and upper bound */
/* positions in ARRAY to be sorted. */
/*****************************************************************************/
static void qcksrt(short array[], int l, int h)
{
int lower,
upper,
top,
i,
j,
pivot,
lostack[14], /* A stack of size 14 can sort an array of up to */
histack[14]; /* 2 ** 15 - 1 elements */
top = 1;
lostack[top] = l;
histack[top] = h;
while (top != 0)
{
lower = lostack[top];
upper = histack[top--];
while (upper > lower)
{
i = lower;
pivot = array[lower];
for (j = lower + 1; j <= upper; j++)
{
if (array[j] < pivot)
{
array[i] = array[j];
i++;
array[j] = array[i];
}
}
array[i] = pivot;
top++;
if (i - lower < upper - i)
{
lostack[top] = i + 1;
histack[top] = upper;
upper = i - 1;
}
else
{
histack[top] = i - 1;
lostack[top] = lower;
lower = i + 1;
}
}
}
return;
}
/*****************************************************************************/
/* NUMBER_LEN: */
/*****************************************************************************/
/* NUMBER_LEN takes a state number and returns the number of digits in that */
/* number. */
/*****************************************************************************/
int number_len(int state_no)
{
int num = 0;
do
{
state_no /= 10;
num++;
} while (state_no != 0);
return num;
}
/*************************************************************************/
/* RESTORE_SYMBOL: */
/*************************************************************************/
/* This procedure takes two character strings as arguments: IN and OUT. */
/* IN identifies a grammar symbol or name that is checked as to whether */
/* or not it needs to be quoted. If so, the necessary quotes are added */
/* as IN is copied into the space identified by OUT. */
/* NOTE that it is assumed that IN and OUT do not overlap each other. */
/*************************************************************************/
void restore_symbol(char *out, char *in)
{
int len;
len = strlen(in);
if (len > 0)
{
if ((len == 1 && in[0] == ormark) ||
(in[0] == escape) ||
(in[0] == '\'') ||
(in[len - 1] == '\'') ||
(strchr(in, ' ') != NULL &&
(in[0] != '<' || in[len - 1] != '>')))
{
*(out++) = '\'';
while(*in != '\0')
{
if (*in == '\'')
*(out++) = *in;
*(out++) = *(in++);
}
*(out++) = '\'';
*out = '\0';
return;
}
}
strcpy(out, in);
if (out[0] == '\n') /* one of the special grammar symbols? */
out[0] = escape;
return;
}
/*****************************************************************************/
/* PRINT_LARGE_TOKEN: */
/*****************************************************************************/
/* PRINT_LARGE_TOKEN generates code to print a token that may exceed the */
/* limit of its field. The argument are LINE which is the symbol a varying */
/* length character string, TOKEN which is the symbol to be printed, INDENT */
/* which is a character string to be used as an initial prefix to indent the */
/* output line, and LEN which indicates the maximum number of characters that*/
/* can be printed on a given line. At the end of this process, LINE will */
/* have the value of the remaining substring that can fit on the output line.*/
/* If a TOKEN is too large to be indented in a line, but not too large for */
/* the whole line, we forget the indentation, and printed it. Otherwise, it */
/* is "chapped up" and printed in pieces that are each indented. */
/*****************************************************************************/
void print_large_token(char *line, char *token, char *indent, int len)
{
int toklen;
char temp[SYMBOL_SIZE + 1];
toklen = strlen(token);
if (toklen > len && toklen <= PRINT_LINE_SIZE-1)
{
fprintf(syslis, "\n%s", token);
ENDPAGE_CHECK;
token = "";
strcpy(line,indent);
}
else
{
for (; toklen > len; toklen = strlen(temp))
{
memcpy(temp, token, len);
temp[len] = '\0';
fprintf(syslis, "\n%s",temp);
ENDPAGE_CHECK;
strcpy(temp, token+len + 1);
token = temp;
}
strcpy(line,indent);
strcat(line,token);
}
return;
}
/*****************************************************************************/
/* PRINT_ITEM: */
/*****************************************************************************/
/* PRINT_ITEM takes as parameter an ITEM_NO which it prints. */
/*****************************************************************************/
void print_item(int item_no)
{
int rule_no,
symbol,
len,
offset,
i,
k;
char tempstr[PRINT_LINE_SIZE + 1],
line[PRINT_LINE_SIZE + 1],
tok[SYMBOL_SIZE + 1];
/*********************************************************************/
/* We first print the left hand side of the rule, leaving at least */
/* 5 spaces in the output line to accomodate the equivalence symbol */
/* "::=" surrounded by blanks on both sides. Then, we print all the */
/* terminal symbols in the right hand side up to but not including */
/* the dot symbol. */
/*********************************************************************/
rule_no = item_table[item_no].rule_number;
symbol = rules[rule_no].lhs;
restore_symbol(tok, RETRIEVE_STRING(symbol));
len = PRINT_LINE_SIZE - 5;
print_large_token(line, tok, "", len);
strcat(line, " ::= ");
i = (PRINT_LINE_SIZE / 2) - 1;
offset = MIN(strlen(line)-1, i);
len = PRINT_LINE_SIZE - (offset + 4);
i = rules[rule_no].rhs; /* symbols before dot */
k = ((rules[rule_no].rhs + item_table[item_no].dot) - 1);
for (; i <= k; i++)
{
symbol = rhs_sym[i];
restore_symbol(tok, RETRIEVE_STRING(symbol));
if (strlen(tok) + strlen(line) > PRINT_LINE_SIZE - 4)
{
fprintf(syslis,"\n%s", line);
ENDPAGE_CHECK;
fill_in(tempstr, offset, SPACE);
print_large_token(line, tok, tempstr, len);
}
else
strcat(line, tok);
strcat(line, BLANK);
}
/*********************************************************************/
/* We now add a DOT "." to the output line and print the remaining */
/* symbols in the right hand side. If ITEM_NO is a complete item, */
/* we also print the rule number. */
/*********************************************************************/
if (item_table[item_no].dot == 0 || item_table[item_no].symbol == empty)
strcpy(tok, ".");
else
strcpy(tok, " .");
strcat(line, tok);
len = PRINT_LINE_SIZE - (offset + 1);
for (i = rules[rule_no].rhs +
item_table[item_no].dot;/* symbols after dot*/
i <= rules[rule_no + 1].rhs - 1; i++)
{
symbol = rhs_sym[i];
restore_symbol(tok, RETRIEVE_STRING(symbol));
if (strlen(tok) + strlen(line) > PRINT_LINE_SIZE -1)
{
fprintf(syslis, "\n%s", line);
ENDPAGE_CHECK;
fill_in(tempstr, offset, SPACE);
print_large_token(line, tok, tempstr, len);
}
else
strcat(line, tok);
strcat(line, BLANK);
}
if (item_table[item_no].symbol == empty) /* complete item */
{
sprintf(tok, " (%d)", rule_no);
if (strlen(tok) + strlen(line) > PRINT_LINE_SIZE - 1)
{
fprintf(syslis, "\n%s", line);
ENDPAGE_CHECK;
fill_in(line,offset, SPACE);
}
strcat(line,tok);
}
fprintf(syslis, "\n%s", line);
ENDPAGE_CHECK;
return;
}
/*****************************************************************************/
/* PRINT_STATE: */
/*****************************************************************************/
/* PRINT_STATE prints all the items in a state. NOTE that when single */
/* productions are eliminated, certain items that were added in a state by */
/* CLOSURE, will no longer show up in the output. Example: If we have the */
/* item [A ::= .B] in a state, and the GOTO_REDUCE generated on B has been */
/* replaced by say the GOTO or GOTO_REDUCE of A, the item above can no longer*/
/* be retrieved, since transitions in a given state are reconstructed from */
/* the KERNEL and ADEQUATE items of the actions in the GOTO and SHIFT maps. */
/*****************************************************************************/
void print_state(int state_no)
{
struct shift_header_type sh;
struct goto_header_type go_to;
short *item_list;
int kernel_size,
i,
n,
item_no,
next_state;
BOOLEAN end_node,
*state_seen,
*item_seen;
struct node *q;
char buffer[PRINT_LINE_SIZE + 1],
line[PRINT_LINE_SIZE + 1];
/*********************************************************************/
/* ITEM_SEEN is used to construct sets of items, to help avoid */
/* adding duplicates in a list. Duplicates can occur because an */
/* item from the kernel set will either be shifted on if it is not a */
/* complete item, or it will be a member of the Complete_items set. */
/* Duplicates can also occur because of the elimination of single */
/* productions. */
/*********************************************************************/
state_seen = Allocate_boolean_array(max_la_state + 1);
item_seen = Allocate_boolean_array(num_items + 1);
item_list = Allocate_short_array(num_items + 1);
/* INITIALIZATION -----------------------------------------------------------*/
for ALL_STATES(i)
state_seen[i] = FALSE;
for ALL_ITEMS(i)
item_seen[i] = FALSE;
kernel_size = 0;
/* END OF INITIALIZATION ----------------------------------------------------*/
i = number_len(state_no) + 8; /* 8 = length("STATE") + 2 spaces + newline*/
fill_in(buffer, (PRINT_LINE_SIZE - i) ,'-');
fprintf(syslis, "\n\n\nSTATE %d %s",state_no, buffer);
output_line_no +=3;
/*********************************************************************/
/* Print the set of states that have transitions to STATE_NO. */
/*********************************************************************/
n = 0;
strcpy(line, "( ");
for (end_node = ((q = in_stat[state_no]) == NULL);
! end_node;
end_node = (q == in_stat[state_no]))
{/* copy list of IN_STAT into array */
q = q -> next;
if (! state_seen[q -> value])
{
state_seen[q -> value] = TRUE;
if (strlen(line) + number_len(q -> value) > PRINT_LINE_SIZE-2)
{
fprintf(syslis,"\n%s",line);
ENDPAGE_CHECK;
strcpy(line, " ");
}
if (q -> value != 0)
{
sprintf(buffer, "%d ", q -> value);
strcat(line, buffer);
}
}
}
strcat(line, ")");
fprintf(syslis,"\n%s\n", line);
output_line_no++;
ENDPAGE_CHECK;
/*********************************************************************/
/* Add the set of kernel items to the array ITEM_LIST, and mark all */
/* items seen to avoid duplicates. */
/*********************************************************************/
for (q = statset[state_no].kernel_items; q != NULL; q = q -> next)
{
kernel_size++;
item_no = q -> value;
item_list[kernel_size] = item_no; /* add to array */
item_seen[item_no] = TRUE; /* Mark as "seen" */
}
/*********************************************************************/
/* Add the Complete Items to the array ITEM_LIST, and mark used. */
/*********************************************************************/
n = kernel_size;
for (q = statset[state_no].complete_items; q != NULL; q = q -> next)
{
item_no = q -> value;
if (! item_seen[item_no])
{
item_seen[item_no] = TRUE; /* Mark as "seen" */
item_list[++n] = item_no;
}
}
/*********************************************************************/
/* Iterate over the shift map. Shift-Reduce actions are identified */
/* by a negative integer that indicates the rule in question , and */
/* the associated item can be retrieved by indexing the array */
/* ADEQUATE_ITEMS at the location of the rule. For shift-actions, we*/
/* simply take the predecessor-items of all the items in the kernel */
/* of the following state. */
/* If the shift-action is a look-ahead shift, we check to see if the */
/* look-ahead state contains shift actions, and retrieve the next */
/* state from one of those shift actions. */
/*********************************************************************/
sh = shift[statset[state_no].shift_number];
for (i = 1; i <= sh.size; i++)
{
next_state = SHIFT_ACTION(sh, i);
while (next_state > (int) num_states)
{
struct shift_header_type next_sh;
next_sh = shift[lastats[next_state].shift_number];
if (next_sh.size > 0)
next_state = SHIFT_ACTION(next_sh, 1);
else
next_state = 0;
}
if (next_state == 0)
q = NULL;
else if (next_state < 0)
q = adequate_item[-next_state];
else
{
q = statset[next_state].kernel_items;
if (q == NULL) /* single production state? */
q = statset[next_state].complete_items;
}
for (; q != NULL; q = q -> next)
{
item_no = q -> value - 1;
if (! item_seen[item_no])
{
item_seen[item_no] = TRUE;
item_list[++n] = item_no;
}
}
}
/*********************************************************************/
/* GOTOS and GOTO-REDUCES are analogous to SHIFTS and SHIFT-REDUCES. */
/*********************************************************************/
go_to = statset[state_no].go_to;
for (i = 1; i <= go_to.size; i++)
{
if (GOTO_ACTION(go_to, i) > 0)
{
q = statset[GOTO_ACTION(go_to, i)].kernel_items;
if (q == NULL) /* single production state? */
q = statset[GOTO_ACTION(go_to, i)].complete_items;
}
else
q = adequate_item[- GOTO_ACTION(go_to, i)];
for (; q != NULL; q = q -> next)
{
item_no = q -> value - 1;
if (! item_seen[item_no])
{
item_seen[item_no] = TRUE;
item_list[++n] = item_no;
}
}
}
/*********************************************************************/
/* Print the Kernel items. If there are any closure items, skip a */
/* line, sort then, then print them. The kernel items are in sorted */
/* order. */
/*********************************************************************/
for (item_no = 1; item_no <= kernel_size; item_no++)
print_item(item_list[item_no]);
if (kernel_size < n)
{
fprintf(syslis, "\n");
ENDPAGE_CHECK;
qcksrt(item_list, kernel_size + 1, n);
for (item_no = kernel_size + 1; item_no <= n; item_no++)
print_item(item_list[item_no]);
}
ffree(item_list);
ffree(item_seen);
ffree(state_seen);
return;
}
/*****************************************************************************/
/* NOSPACE: */
/*****************************************************************************/
/* This procedure is invoked when a call to MALLOC, CALLOC or REALLOC fails. */
/*****************************************************************************/
void nospace(char *file_name, long line_number)
{
fprintf(stderr,
"*** Cannot allocate space ... LPG terminated in "
"file %s at line %d\n", file_name, line_number);
exit(12);
}
/************************************************************************/
/* STRUPR: */
/************************************************************************/
/* StrUpr and StrLwr. */
/* These routines set all characters in a string to upper case and lower*/
/* case (respectively.) These are library routines in DOS, but */
/* are defined here for the 370 compiler. */
/************************************************************************/
char *strupr(char *string)
{
char *s;
/*********************************************************************/
/* While not at end of string, change all lower case to upper case. */
/*********************************************************************/
for (s = string; *s != '\0'; s++)
*s = (islower((int) *s) ? toupper((int) *s) : *s);
return string;
}
/************************************************************************/
/* STRLWR: */
/************************************************************************/
char *strlwr(char *string)
{
char *s;
/*********************************************************************/
/* While not at end of string, change all upper case to lower case. */
/*********************************************************************/
for (s = string; *s != '\0'; s++)
*s = (isupper((int) *s) ? tolower((int) *s) : *s);
return string;
}
|