|
|
1.1 root 1: /*
2: * pretty print routines
3: */
4:
5: #include <stdarg.h>
6: #include "yacc.h"
7:
8: static char ctab[] = " |";
9: listgram()
10: {
11: register i, k, j;
12: register struct sym *sp;
13: struct prod *pp;
14:
15: if( !verbose )
16: return;
17: fprintf(listout,"Input grammar:\n\n");
18: for(i=0; i<nnonterm; i++) {
19: sp = ntrmptr[i];
20: fprintf(listout,"%s:\n\n", sp->s_name);
21: for(j=0; j<sp->s_nprods; j++ ) {
22: pp = sp->s_prods[j];
23: fprintf(listout, "%d:\t%c ", pp->p_prodno, ctab[j!=0]);
24: for(k = 0 ; PROD_RIGHT (pp) [k] >= 0 ; k ++)
25: fprintf (listout, "%s ",
26: ptosym (PROD_RIGHT (pp) [k]));
27: fprintf(listout, "\n");
28: }
29: fprintf(listout,"\t;\n\n");
30: }
31: }
32:
33: prstate(i, f)
34: FILE *f;
35: {
36: register j;
37: register struct sitem *itp;
38:
39: itp = items[i];
40: fprintf(f, "State %d:\n\n", i);
41: for(j=0; j<itp->i_nitems; j++)
42: pritem(itp->i_items[j],f);
43: fprintf(f, "\n");
44: }
45:
46: pritem(ip,f)
47: register int *ip;
48: FILE *f;
49: {
50: register int *pp;
51:
52: pp = ip;
53: do ; while( *--pp >= 0 );
54: fprintf(f,"\t");
55: fprintf(f, "%s <- ", ptosym(-*pp++));
56: while( pp!=ip ) {
57: fprintf(f, "%s ", ptosym(*pp++));
58: }
59: fprintf(f, "_ ");
60: while( *pp!=-1 )
61: fprintf(f, "%s ", ptosym(*pp++));
62: fprintf(f,"\n");
63: }
64:
65: char *
66: ptosym(n)
67: register n;
68: {
69: if (n >= NTBASE)
70: return ntrmptr [n - NTBASE]->s_name;
71: else
72: return trmptr [n]->s_name;
73: }
74:
75: char *
76: prsym(c)
77: register c;
78: {
79: static char s[10];
80:
81: if( c<040 || c>= 0177 ) {
82: switch( c ) {
83: case '\n':
84: sprintf(s, "'\\n'");
85: break;
86:
87: case '\r':
88: sprintf(s, "'\\r'");
89: break;
90:
91: case '\t':
92: sprintf(s, "'\\t'");
93: break;
94:
95: case '\b':
96: sprintf(s,"'\\b'");
97: break;
98:
99: case '\f':
100: sprintf(s, "'\\f'");
101: break;
102:
103: case '\0':
104: sprintf(s, "'\\0'");
105: break;
106:
107: default:
108: sprintf(s, "'\\%3o'", c); /* MWC DSC */
109: }
110: } else
111: sprintf(s, "'%c'", c);
112: return( s );
113: }
114:
115: options(argc, argv)
116: int argc;
117: char *argv[];
118: {
119: register i;
120: register char *ap;
121:
122: for(i=1; i<argc; i++) {
123: ap = argv[i];
124: if( ap[0]!='-' || strcmp(ap,"-")==0 )
125: gramy = argv[i];
126: else
127: if( strcmp(ap,"-v")==0 )
128: verbose++;
129: else
130: if( strcmp(ap,"-l")==0 ) {
131: verbose++;
132: youtput = nextarg(argv[++i]);
133: }
134: else
135: if( strcmp(ap, "-st")==0 ) {
136: pstat++;
137: }
138: else
139: if( strcmp(ap,"-hdr")==0 ) {
140: ytabh = nextarg(argv[++i]);
141: }
142: else
143: if( strcmp(ap,"-d")==0 ) {
144: yydebug++;
145: verbose++;
146: }
147: else
148: if( strcmp(ap, "-items")==0 )
149: maxitem = getnum(argv[++i]);
150: else
151: if( strcmp(ap, "-sprod")==0 )
152: maxprodl = getnum(argv[++i]);
153: else
154: if( strcmp(ap,"-prods")==0 )
155: maxprod = getnum(argv[++i]);
156: else
157: if( strcmp(ap,"-reds")==0 )
158: maxreds = getnum(argv[++i]);
159: else
160: if( strcmp(ap,"-terms")==0 )
161: maxterm = getnum(argv[++i]);
162: else
163: if( strcmp(ap,"-nterms")==0 )
164: maxnterm = getnum(argv[++i]);
165: else
166: if( strcmp(ap,"-states")==0 )
167: maxstates = getnum(argv[++i]);
168: else
169: if( strcmp(ap,"-types")==0 )
170: maxtype = getnum(argv[++i]);
171: else
172: usage();
173: if (maxterm >= LSETSIZE * CHAR_BIT)
174: yyerror (NLNO|FATAL,
175: "The maximum -terms value is %d\n",
176: LSETSIZE * CHAR_BIT);
177: }
178: /*
179: * if( (maxterm+maxnterm+maxtype) >= MAXSYM )
180: * yyerror(NLNO|FATAL, "increase MAXSYM");
181: *
182: * MWC DSC - make it all flexible with maxsym - it will also mean less memory
183: * allocated, in most cases.
184: */
185: prdptr = (struct prod **) yalloc (maxprod, sizeof (* prdptr));
186: symtab = (struct sym **)
187: yalloc(maxsym+1, sizeof *symtab); /* MWC DSC */
188: ntrmptr = (struct sym **) yalloc (maxnterm, sizeof *ntrmptr);
189: trmptr = (struct sym **) yalloc (maxterm, sizeof *trmptr);
190: typeptr = (struct sym **) yalloc (maxtype, sizeof *typeptr);
191:
192: nitprod = (struct prod *) yalloc (1, PROD_TOTAL_SIZE (maxprodl));
193: PROD_EXTRA_INIT (nitprod);
194:
195: nititem = (struct sitem *) yalloc (1, SITEM_TOTAL_SIZE (maxitem));
196: SITEM_EXTRA_INIT (nititem);
197:
198: states = (struct state *) yalloc(maxstates, sizeof *states);
199: items = (struct sitem **) yalloc(maxstates, sizeof (* items));
200: }
201:
202: yyerror(type, format /* , ... */)
203: char * format;
204: {
205: va_list argp;
206:
207: if( type&FATAL )
208: fprintf(stderr, "fatal error: ");
209: if( (type&NLNO)==0 )
210: fprintf(stderr, "line %d: ", yyline);
211:
212: /* Look ma! No more '%r'! */
213: va_start (argp, format);
214: vfprintf (stderr, format, argp);
215: va_end (argp);
216:
217: fprintf(stderr, "\n");
218: if( type&FATAL )
219: cleanup(2);
220: if( (type&WARNING)==0 )
221: nerrors++;
222: if( type&SKIP )
223: while( llgetc()!='\n' ) /* MWC DSC */
224: ;
225: }
226:
227: getnum(s)
228: char *s;
229: {
230: if( s==NULL )
231: yyerror(NLNO|FATAL, "missing argument");
232: return( atoi(s) );
233: }
234:
235: char * /* MWC DSC */
236: nextarg(s)
237: char *s;
238: {
239: if( s==NULL )
240: yyerror(NLNO|FATAL, "missing file name");
241: return( s );
242: }
243:
244: usage()
245: {
246: fprintf(stderr, "Usage: yacc [options] [parameters] grammar\n");
247: fprintf(stderr, "Options: -v -d -l file -hdr file -st\n");
248: fprintf(stderr,"Parameters: -prods # -terms # - nterms # -states # -types #\n");
249: exit(1);
250: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.