GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/yacc/output.c Lines: 604 688 87.8 %
Date: 2017-11-07 Branches: 352 516 68.2 %

Line Branch Exec Source
1
/*	$OpenBSD: output.c,v 1.26 2016/09/21 16:26:30 otto Exp $	*/
2
/*	$NetBSD: output.c,v 1.4 1996/03/19 03:21:41 jtc Exp $	*/
3
4
/*
5
 * Copyright (c) 1989 The Regents of the University of California.
6
 * All rights reserved.
7
 *
8
 * This code is derived from software contributed to Berkeley by
9
 * Robert Paul Corbett.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
19
 * 3. Neither the name of the University nor the names of its contributors
20
 *    may be used to endorse or promote products derived from this software
21
 *    without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33
 * SUCH DAMAGE.
34
 */
35
36
#include "defs.h"
37
38
static int nvectors;
39
static int nentries;
40
static short **froms;
41
static short **tos;
42
static short *tally;
43
static short *width;
44
static short *state_count;
45
static short *order;
46
static short *base;
47
static short *pos;
48
static int maxtable;
49
static short *table;
50
static short *check;
51
static int lowzero;
52
static int high;
53
54
void output_prefix(void);
55
void output_rule_data(void);
56
void output_yydefred(void);
57
void output_actions(void);
58
void token_actions(void);
59
void goto_actions(void);
60
int default_goto(int);
61
void save_column(int, int);
62
void sort_actions(void);
63
void pack_table(void);
64
int matching_vector(int);
65
int pack_vector(int);
66
void output_base(void);
67
void output_table(void);
68
void output_check(void);
69
int is_C_identifier(char *);
70
void output_defines(void);
71
void output_stored_text(void);
72
void output_debug(void);
73
void output_stype(void);
74
void output_trailing_text(void);
75
void output_semantic_actions(void);
76
void free_itemsets(void);
77
void free_shifts(void);
78
void free_reductions(void);
79
80
void
81
output(void)
82
{
83
30
	free_itemsets();
84
15
	free_shifts();
85
15
	free_reductions();
86
15
	output_prefix();
87
15
	output_stored_text();
88
15
	output_defines();
89
15
	output_rule_data();
90
15
	output_yydefred();
91
15
	output_actions();
92
15
	free_parser();
93
15
	output_debug();
94
15
	output_stype();
95
15
	if (rflag)
96
		write_section(tables);
97
15
	write_section(header);
98
15
	output_trailing_text();
99
15
	write_section(body);
100
15
	output_semantic_actions();
101
15
	write_section(trailer);
102
15
}
103
104
105
void
106
output_prefix(void)
107
{
108
30
	if (symbol_prefix == NULL)
109
9
		symbol_prefix = "yy";
110
	else {
111
6
		++outline;
112
6
		fprintf(code_file, "#define yyparse %sparse\n", symbol_prefix);
113
6
		++outline;
114
6
		fprintf(code_file, "#define yylex %slex\n", symbol_prefix);
115
6
		++outline;
116
6
		fprintf(code_file, "#define yyerror %serror\n", symbol_prefix);
117
6
		++outline;
118
6
		fprintf(code_file, "#define yychar %schar\n", symbol_prefix);
119
6
		++outline;
120
6
		fprintf(code_file, "#define yyval %sval\n", symbol_prefix);
121
6
		++outline;
122
6
		fprintf(code_file, "#define yylval %slval\n", symbol_prefix);
123
6
		++outline;
124
6
		fprintf(code_file, "#define yydebug %sdebug\n", symbol_prefix);
125
6
		++outline;
126
6
		fprintf(code_file, "#define yynerrs %snerrs\n", symbol_prefix);
127
6
		++outline;
128
6
		fprintf(code_file, "#define yyerrflag %serrflag\n", symbol_prefix);
129
6
		++outline;
130
6
		fprintf(code_file, "#define yyss %sss\n", symbol_prefix);
131
6
		++outline;
132
6
		fprintf(code_file, "#define yysslim %ssslim\n", symbol_prefix);
133
6
		++outline;
134
6
		fprintf(code_file, "#define yyssp %sssp\n", symbol_prefix);
135
6
		++outline;
136
6
		fprintf(code_file, "#define yyvs %svs\n", symbol_prefix);
137
6
		++outline;
138
6
		fprintf(code_file, "#define yyvsp %svsp\n", symbol_prefix);
139
6
		++outline;
140
6
		fprintf(code_file, "#define yystacksize %sstacksize\n", symbol_prefix);
141
6
		++outline;
142
6
		fprintf(code_file, "#define yylhs %slhs\n", symbol_prefix);
143
6
		++outline;
144
6
		fprintf(code_file, "#define yylen %slen\n", symbol_prefix);
145
6
		++outline;
146
6
		fprintf(code_file, "#define yydefred %sdefred\n", symbol_prefix);
147
6
		++outline;
148
6
		fprintf(code_file, "#define yydgoto %sdgoto\n", symbol_prefix);
149
6
		++outline;
150
6
		fprintf(code_file, "#define yysindex %ssindex\n", symbol_prefix);
151
6
		++outline;
152
6
		fprintf(code_file, "#define yyrindex %srindex\n", symbol_prefix);
153
6
		++outline;
154
6
		fprintf(code_file, "#define yygindex %sgindex\n", symbol_prefix);
155
6
		++outline;
156
6
		fprintf(code_file, "#define yytable %stable\n", symbol_prefix);
157
6
		++outline;
158
6
		fprintf(code_file, "#define yycheck %scheck\n", symbol_prefix);
159
6
		++outline;
160
6
		fprintf(code_file, "#define yyname %sname\n", symbol_prefix);
161
6
		++outline;
162
6
		fprintf(code_file, "#define yyrule %srule\n", symbol_prefix);
163
	}
164
15
	++outline;
165
15
	fprintf(code_file, "#define YYPREFIX \"%s\"\n", symbol_prefix);
166
15
}
167
168
169
void
170
output_rule_data(void)
171
{
172
	int i;
173
	int j;
174
175
45
	fprintf(output_file,
176
	    "const short %slhs[] =\n"
177
15
	    "\t{%42d,", symbol_prefix, symbol_value[start_symbol]);
178
179
	j = 10;
180
3730
	for (i = 3; i < nrules; i++) {
181
1850
		if (j >= 10) {
182
192
			if (!rflag)
183
192
				++outline;
184
384
			putc('\n', output_file);
185
			j = 1;
186
192
		} else
187
1658
			++j;
188
1850
		fprintf(output_file, "%5d,", symbol_value[rlhs[i]]);
189
	}
190
15
	if (!rflag)
191
15
		outline += 2;
192
15
	fprintf(output_file, "\n};\n");
193
194
30
	fprintf(output_file,
195
	    "const short %slen[] =\n"
196
15
	    "\t{%42d,", symbol_prefix, 2);
197
198
	j = 10;
199
3730
	for (i = 3; i < nrules; i++) {
200
1850
		if (j >= 10) {
201
192
			if (!rflag)
202
192
				++outline;
203
384
			putc('\n', output_file);
204
			j = 1;
205
192
		} else
206
1658
			j++;
207
1850
		fprintf(output_file, "%5d,", rrhs[i + 1] - rrhs[i] - 1);
208
	}
209
15
	if (!rflag)
210
15
		outline += 2;
211
15
	fprintf(output_file, "\n};\n");
212
15
}
213
214
215
void
216
output_yydefred(void)
217
{
218
	int i, j;
219
220
45
	fprintf(output_file,
221
	    "const short %sdefred[] =\n"
222
	    "\t{%39d,",
223
40
	    symbol_prefix, (defred[0] ? defred[0] - 2 : 0));
224
225
	j = 10;
226
6850
	for (i = 1; i < nstates; i++) {
227
3410
		if (j < 10)
228
3065
			++j;
229
		else {
230
345
			if (!rflag)
231
345
				++outline;
232
690
			putc('\n', output_file);
233
			j = 1;
234
		}
235
8297
		fprintf(output_file, "%5d,", (defred[i] ? defred[i] - 2 : 0));
236
	}
237
238
15
	if (!rflag)
239
15
		outline += 2;
240
15
	fprintf(output_file, "\n};\n");
241
15
}
242
243
244
void
245
output_actions(void)
246
{
247
30
	nvectors = 2 * nstates + nvars;
248
249
15
	froms = NEW2(nvectors, short *);
250
15
	tos = NEW2(nvectors, short *);
251
15
	tally = NEW2(nvectors, short);
252
15
	width = NEW2(nvectors, short);
253
254
15
	token_actions();
255
15
	free(lookaheads);
256
15
	free(LA);
257
15
	free(LAruleno);
258
15
	free(accessing_symbol);
259
260
15
	goto_actions();
261
15
	free(goto_map + ntokens);
262
15
	free(from_state);
263
15
	free(to_state);
264
265
15
	sort_actions();
266
15
	pack_table();
267
15
	output_base();
268
15
	output_table();
269
15
	output_check();
270
15
}
271
272
273
void
274
token_actions(void)
275
{
276
	int i, j;
277
	int shiftcount, reducecount;
278
	int max, min;
279
	short *actionrow, *r, *s;
280
	action *p;
281
282
30
	actionrow = NEW2(2*ntokens, short);
283
6880
	for (i = 0; i < nstates; ++i) {
284
3425
		if (parser[i]) {
285
1129220
			for (j = 0; j < 2 * ntokens; ++j)
286
561192
				actionrow[j] = 0;
287
			shiftcount = 0;
288
			reducecount = 0;
289
92600
			for (p = parser[i]; p; p = p->next) {
290
42882
				if (p->suppressed == 0) {
291
41191
					if (p->action_code == SHIFT) {
292
8585
						++shiftcount;
293
8585
						actionrow[p->symbol] = p->number;
294

73797
					} else if (p->action_code == REDUCE &&
295
32606
					    p->number != defred[i]) {
296
6319
						++reducecount;
297
6319
						actionrow[p->symbol + ntokens] = p->number;
298
6319
					}
299
				}
300
			}
301
302
3418
			tally[i] = shiftcount;
303
3418
			tally[nstates+i] = reducecount;
304
3418
			width[i] = 0;
305
3418
			width[nstates+i] = 0;
306
3418
			if (shiftcount > 0) {
307
1913
				froms[i] = r = NEW2(shiftcount, short);
308
1913
				tos[i] = s = NEW2(shiftcount, short);
309
				min = MAXSHORT;
310
				max = 0;
311
308492
				for (j = 0; j < ntokens; ++j) {
312
152333
					if (actionrow[j]) {
313
8585
						if (min > symbol_value[j])
314
2185
							min = symbol_value[j];
315
8585
						if (max < symbol_value[j])
316
8153
							max = symbol_value[j];
317
8585
						*r++ = symbol_value[j];
318
8585
						*s++ = actionrow[j];
319
8585
					}
320
				}
321
1913
				width[i] = max - min + 1;
322
1913
			}
323
3418
			if (reducecount > 0) {
324
576
				froms[nstates+i] = r = NEW2(reducecount, short);
325
576
				tos[nstates+i] = s = NEW2(reducecount, short);
326
				min = MAXSHORT;
327
				max = 0;
328
91000
				for (j = 0; j < ntokens; ++j) {
329
44924
					if (actionrow[ntokens+j]) {
330
6319
						if (min > symbol_value[j])
331
789
							min = symbol_value[j];
332
6319
						if (max < symbol_value[j])
333
5628
							max = symbol_value[j];
334
6319
						*r++ = symbol_value[j];
335
6319
						*s++ = actionrow[ntokens+j] - 2;
336
6319
					}
337
				}
338
576
				width[nstates+i] = max - min + 1;
339
576
			}
340
		}
341
	}
342
15
	free(actionrow);
343
15
}
344
345
void
346
goto_actions(void)
347
{
348
	int i, j, k;
349
350
30
	state_count = NEW2(nstates, short);
351
352
15
	k = default_goto(start_symbol + 1);
353
30
	fprintf(output_file, "const short %sdgoto[] =\n"
354
15
	    "\t{%40d,", symbol_prefix, k);
355
15
	save_column(start_symbol + 1, k);
356
357
	j = 10;
358
1190
	for (i = start_symbol + 2; i < nsyms; i++) {
359
580
		if (j >= 10) {
360
66
			if (!rflag)
361
66
				++outline;
362
132
			putc('\n', output_file);
363
			j = 1;
364
66
		} else
365
514
			++j;
366
367
580
		k = default_goto(i);
368
580
		fprintf(output_file, "%5d,", k);
369
580
		save_column(i, k);
370
	}
371
372
15
	if (!rflag)
373
15
		outline += 2;
374
15
	fprintf(output_file, "\n};\n");
375
15
	free(state_count);
376
15
}
377
378
int
379
default_goto(int symbol)
380
{
381
	int i;
382
	int m;
383
	int n;
384
	int default_state;
385
	int max;
386
387
1190
	m = goto_map[symbol];
388
595
	n = goto_map[symbol + 1];
389
390
595
	if (m == n)
391
		return (0);
392
393
595
	memset(state_count, 0, nstates * sizeof(short));
394
395
6402
	for (i = m; i < n; i++)
396
2606
		state_count[to_state[i]]++;
397
398
	max = 0;
399
	default_state = 0;
400
434398
	for (i = 0; i < nstates; i++) {
401
216604
		if (state_count[i] > max) {
402
			max = state_count[i];
403
			default_state = i;
404
625
		}
405
	}
406
407
595
	return (default_state);
408
595
}
409
410
411
412
void
413
save_column(int symbol, int default_state)
414
{
415
	int i;
416
	int m;
417
	int n;
418
	short *sp;
419
	short *sp1;
420
	short *sp2;
421
	int count;
422
	int symno;
423
424
1190
	m = goto_map[symbol];
425
595
	n = goto_map[symbol + 1];
426
427
	count = 0;
428
6402
	for (i = m; i < n; i++) {
429
2606
		if (to_state[i] != default_state)
430
1168
			++count;
431
	}
432
595
	if (count == 0)
433
406
		return;
434
435
189
	symno = symbol_value[symbol] + 2*nstates;
436
437
189
	froms[symno] = sp1 = sp = NEW2(count, short);
438
189
	tos[symno] = sp2 = NEW2(count, short);
439
440
4328
	for (i = m; i < n; i++) {
441
1975
		if (to_state[i] != default_state) {
442
1168
			*sp1++ = from_state[i];
443
1168
			*sp2++ = to_state[i];
444
1168
		}
445
	}
446
447
189
	tally[symno] = count;
448
189
	width[symno] = sp1[-1] - sp[0] + 1;
449
784
}
450
451
void
452
sort_actions(void)
453
{
454
	int i;
455
	int j;
456
	int k;
457
	int t;
458
	int w;
459
460
30
	order = NEW2(nvectors, short);
461
15
	nentries = 0;
462
463
14950
	for (i = 0; i < nvectors; i++) {
464
7460
		if (tally[i] > 0) {
465
			t = tally[i];
466
2678
			w = width[i];
467
2678
			j = nentries - 1;
468
469

531307
			while (j >= 0 && (width[order[j]] < w))
470
174439
				j--;
471
472

15891
			while (j >= 0 && (width[order[j]] == w) &&
473
4351
			    (tally[order[j]] < t))
474
2078
				j--;
475
476
358390
			for (k = nentries - 1; k > j; k--)
477
176517
				order[k + 1] = order[k];
478
479
2678
			order[j + 1] = i;
480
2678
			nentries++;
481
2678
		}
482
	}
483
15
}
484
485
486
void
487
pack_table(void)
488
{
489
	int i;
490
	int place;
491
	int state;
492
493
30
	base = NEW2(nvectors, short);
494
15
	pos = NEW2(nentries, short);
495
496
15
	maxtable = 1000;
497
15
	table = NEW2(maxtable, short);
498
15
	check = NEW2(maxtable, short);
499
500
15
	lowzero = 0;
501
15
	high = 0;
502
503
30030
	for (i = 0; i < maxtable; i++)
504
15000
		check[i] = -1;
505
506
5386
	for (i = 0; i < nentries; i++) {
507
2678
		state = matching_vector(i);
508
509
2678
		if (state < 0)
510
1738
			place = pack_vector(i);
511
		else
512
940
			place = base[state];
513
514
2678
		pos[i] = place;
515
2678
		base[order[i]] = place;
516
	}
517
518
14950
	for (i = 0; i < nvectors; i++) {
519
7460
		free(froms[i]);
520
7460
		free(tos[i]);
521
	}
522
523
15
	free(froms);
524
15
	free(tos);
525
15
	free(pos);
526
15
}
527
528
529
/*  The function matching_vector determines if the vector specified by	*/
530
/*  the input parameter matches a previously considered	vector.  The	*/
531
/*  test at the start of the function checks if the vector represents	*/
532
/*  a row of shifts over terminal symbols or a row of reductions, or a	*/
533
/*  column of shifts over a nonterminal symbol.  Berkeley Yacc does not	*/
534
/*  check if a column of shifts over a nonterminal symbols matches a	*/
535
/*  previously considered vector.  Because of the nature of LR parsing	*/
536
/*  tables, no two columns can match.  Therefore, the only possible	*/
537
/*  match would be between a row and a column.  Such matches are	*/
538
/*  unlikely.  Therefore, to save time, no attempt is made to see if a	*/
539
/*  column matches a previously considered vector.			*/
540
/*									*/
541
/*  Matching_vector is poorly designed.  The test could easily be made	*/
542
/*  faster.  Also, it depends on the vectors being in a specific	*/
543
/*  order.								*/
544
545
int
546
matching_vector(int vector)
547
{
548
	int i, j, k, t, w, match, prev;
549
550
5356
	i = order[vector];
551
2678
	if (i >= 2*nstates)
552
189
		return (-1);
553
554
2489
	t = tally[i];
555
2489
	w = width[i];
556
557
91060
	for (prev = vector - 1; prev >= 0; prev--) {
558
45509
		j = order[prev];
559

89748
		if (width[j] != w || tally[j] != t)
560
1528
			return (-1);
561
562
		match = 1;
563

242661
		for (k = 0; match && k < t; k++) {
564

59483
			if (tos[j][k] != tos[i][k] ||
565
8230
			    froms[j][k] != froms[i][k])
566
43041
				match = 0;
567
		}
568
569
43981
		if (match)
570
940
			return (j);
571
	}
572
573
21
	return (-1);
574
2678
}
575
576
577
578
int
579
pack_vector(int vector)
580
{
581
	int i, j, k, l;
582
	int t, loc, ok;
583
	short *from, *to;
584
	int newmax;
585
586
3476
	i = order[vector];
587
1738
	t = tally[i];
588
1738
	assert(t);
589
590
1738
	from = froms[i];
591
1738
	to = tos[i];
592
593
1738
	j = lowzero - from[0];
594
19924
	for (k = 1; k < t; ++k)
595
8224
		if (lowzero - from[k] > j)
596
256
			j = lowzero - from[k];
597
410493
	for (;; ++j) {
598
412231
		if (j == 0)
599
			continue;
600
		ok = 1;
601

3561812
		for (k = 0; ok && k < t; k++) {
602
911698
			loc = j + from[k];
603
911698
			if (loc >= maxtable) {
604
30
				if (loc >= MAXTABLE)
605
					fatal("maximum table size exceeded");
606
607
				newmax = maxtable;
608
30
				do {
609
30
					newmax += 200;
610
30
				} while (newmax <= loc);
611
30
				table = realloc(table, newmax * sizeof(short));
612
30
				if (table == NULL)
613
					no_space();
614
30
				check = realloc(check, newmax * sizeof(short));
615
30
				if (check == NULL)
616
					no_space();
617
12060
				for (l  = maxtable; l < newmax; ++l) {
618
6000
					table[l] = 0;
619
6000
					check[l] = -1;
620
				}
621
30
				maxtable = newmax;
622
30
			}
623
624
911698
			if (check[loc] != -1)
625
408316
				ok = 0;
626
		}
627

2101867
		for (k = 0; ok && k < vector; k++) {
628
425591
			if (pos[k] == j)
629
1624
				ok = 0;
630
		}
631
411678
		if (ok) {
632
23400
			for (k = 0; k < t; k++) {
633
9962
				loc = j + from[k];
634
9962
				table[loc] = to[k];
635
9962
				check[loc] = from[k];
636
9962
				if (loc > high)
637
4888
					high = loc;
638
			}
639
640

15584
			while (lowzero < maxtable && check[lowzero] != -1)
641
4036
				++lowzero;
642
643
1738
			return (j);
644
		}
645
	}
646
}
647
648
649
650
void
651
output_base(void)
652
{
653
	int i, j;
654
655
45
	fprintf(output_file, "const short %ssindex[] =\n"
656
15
	    "\t{%39d,", symbol_prefix, base[0]);
657
658
	j = 10;
659
6850
	for (i = 1; i < nstates; i++) {
660
3410
		if (j >= 10) {
661
345
			if (!rflag)
662
345
				++outline;
663
690
			putc('\n', output_file);
664
			j = 1;
665
345
		} else
666
3065
			++j;
667
3410
		fprintf(output_file, "%5d,", base[i]);
668
	}
669
670
15
	if (!rflag)
671
15
		outline += 2;
672
30
	fprintf(output_file, "};\n"
673
	    "const short %srindex[] =\n"
674
15
	    "\t{%39d,", symbol_prefix, base[nstates]);
675
676
	j = 10;
677
6850
	for (i = nstates + 1; i < 2*nstates; i++) {
678
3410
		if (j >= 10) {
679
345
			if (!rflag)
680
345
				++outline;
681
690
			putc('\n', output_file);
682
			j = 1;
683
345
		} else
684
3065
			++j;
685
3410
		fprintf(output_file, "%5d,", base[i]);
686
	}
687
688
15
	if (!rflag)
689
15
		outline += 2;
690
30
	fprintf(output_file, "};\n"
691
	    "const short %sgindex[] =\n"
692
15
	    "\t{%39d,", symbol_prefix, base[2*nstates]);
693
694
	j = 10;
695
1190
	for (i = 2*nstates + 1; i < nvectors - 1; i++) {
696
580
		if (j >= 10) {
697
66
			if (!rflag)
698
66
				++outline;
699
132
			putc('\n', output_file);
700
			j = 1;
701
66
		} else
702
514
			++j;
703
580
		fprintf(output_file, "%5d,", base[i]);
704
	}
705
706
15
	if (!rflag)
707
15
		outline += 2;
708
15
	fprintf(output_file, "\n};\n");
709
15
	free(base);
710
15
}
711
712
713
void
714
output_table(void)
715
{
716
	int i, j;
717
718
30
	++outline;
719
15
	fprintf(code_file, "#define YYTABLESIZE %d\n", high);
720
30
	fprintf(output_file, "const short %stable[] =\n"
721
15
	    "\t{%40d,", symbol_prefix, table[0]);
722
723
	j = 10;
724
31196
	for (i = 1; i <= high; i++) {
725
15583
		if (j >= 10) {
726
1566
			if (!rflag)
727
1566
				++outline;
728
3132
			putc('\n', output_file);
729
			j = 1;
730
1566
		} else
731
14017
			++j;
732
15583
		fprintf(output_file, "%5d,", table[i]);
733
	}
734
735
15
	if (!rflag)
736
15
		outline += 2;
737
15
	fprintf(output_file, "\n};\n");
738
15
	free(table);
739
15
}
740
741
742
void
743
output_check(void)
744
{
745
	int i, j;
746
747
45
	fprintf(output_file, "const short %scheck[] =\n"
748
15
	    "\t{%40d,", symbol_prefix, check[0]);
749
750
	j = 10;
751
31196
	for (i = 1; i <= high; i++) {
752
15583
		if (j >= 10) {
753
1566
			if (!rflag)
754
1566
				++outline;
755
3132
			putc('\n', output_file);
756
			j = 1;
757
1566
		} else
758
14017
			++j;
759
15583
		fprintf(output_file, "%5d,", check[i]);
760
	}
761
762
15
	if (!rflag)
763
15
		outline += 2;
764
15
	fprintf(output_file, "\n};\n");
765
15
	free(check);
766
15
}
767
768
769
int
770
is_C_identifier(char *name)
771
{
772
	char *s;
773
	int c;
774
775
	s = name;
776
1706
	c = (unsigned char)*s;
777
853
	if (c == '"') {
778
		c = (unsigned char)*++s;
779
		if (!isalpha(c) && c != '_' && c != '$')
780
			return (0);
781
		while ((c = (unsigned char)*++s) != '"') {
782
			if (!isalnum(c) && c != '_' && c != '$')
783
				return (0);
784
		}
785
		return (1);
786
	}
787
788
853
	if (!isalpha(c) && c != '_' && c != '$')
789
50
		return (0);
790
5210
	while ((c = (unsigned char)*++s)) {
791
4407
		if (!isalnum(c) && c != '_' && c != '$')
792
			return (0);
793
	}
794
803
	return (1);
795
853
}
796
797
798
void
799
output_defines(void)
800
{
801
	int c, i;
802
	char *s;
803
804
1751
	for (i = 2; i < ntokens; ++i) {
805
853
		s = symbol_name[i];
806
853
		if (is_C_identifier(s)) {
807
803
			fprintf(code_file, "#define ");
808
803
			if (dflag)
809
582
				fprintf(defines_file, "#define ");
810
803
			c = (unsigned char)*s;
811
803
			if (c == '"') {
812
				while ((c = (unsigned char)*++s) != '"') {
813
					putc(c, code_file);
814
					if (dflag)
815
						putc(c, defines_file);
816
				}
817
			} else {
818
				do {
819
10420
					putc(c, code_file);
820
5210
					if (dflag)
821
7898
						putc(c, defines_file);
822
5210
				} while ((c = (unsigned char)*++s));
823
			}
824
803
			++outline;
825
803
			fprintf(code_file, " %d\n", symbol_value[i]);
826
803
			if (dflag)
827
582
				fprintf(defines_file, " %d\n", symbol_value[i]);
828
		}
829
	}
830
831
15
	++outline;
832
15
	fprintf(code_file, "#define YYERRCODE %d\n", symbol_value[1]);
833
834

26
	if (dflag && unionized) {
835
11
		fclose(union_file);
836
11
		union_file = fopen(union_file_name, "r");
837
11
		if (union_file == NULL)
838
			open_error(union_file_name);
839

6756
		while ((c = getc(union_file)) != EOF)
840
3356
			putc(c, defines_file);
841
11
		fprintf(defines_file, " YYSTYPE;\n");
842
11
		fprintf(defines_file, "#endif /* YYSTYPE_DEFINED */\n");
843
22
		fprintf(defines_file, "extern YYSTYPE %slval;\n",
844
11
		    symbol_prefix);
845
11
	}
846
15
}
847
848
849
void
850
output_stored_text(void)
851
{
852
	int c;
853
	FILE *in, *out;
854
855
30
	fclose(text_file);
856
15
	text_file = fopen(text_file_name, "r");
857
15
	if (text_file == NULL)
858
		open_error(text_file_name);
859
	in = text_file;
860

60
	if ((c = getc(in)) == EOF)
861
		return;
862
15
	out = code_file;
863
15
	if (c ==  '\n')
864
		++outline;
865
30
	putc(c, out);
866

143736
	while ((c = getc(in)) != EOF) {
867
35919
		if (c == '\n')
868
1466
			++outline;
869
71838
		putc(c, out);
870
	}
871
15
	if (!lflag)
872
15
		fprintf(out, line_format, ++outline + 1, code_file_name);
873
30
}
874
875
876
void
877
output_debug(void)
878
{
879
	int i, j, k, max;
880
	char **symnam, *s;
881
882
30
	++outline;
883
15
	fprintf(code_file, "#define YYFINAL %d\n", final_state);
884
15
	outline += 3;
885
30
	fprintf(code_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
886
15
		tflag);
887
15
	if (rflag)
888
		fprintf(output_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
889
		    tflag);
890
891
	max = 0;
892
1736
	for (i = 2; i < ntokens; ++i)
893
853
		if (symbol_value[i] > max)
894
803
			max = symbol_value[i];
895
15
	++outline;
896
15
	fprintf(code_file, "#define YYMAXTOKEN %d\n", max);
897
898
15
	symnam = calloc(max+1, sizeof(char *));
899
15
	if (symnam == NULL)
900
		no_space();
901
902
1736
	for (i = ntokens - 1; i >= 2; --i)
903
853
		symnam[symbol_value[i]] = symbol_name[i];
904
15
	symnam[0] = "end-of-file";
905
906
15
	if (!rflag)
907
15
		++outline;
908
30
	fprintf(output_file,
909
	    "#if YYDEBUG\n"
910
	    "const char * const %sname[] =\n"
911
15
	    "\t{", symbol_prefix);
912
	j = 80;
913
9346
	for (i = 0; i <= max; ++i) {
914
4658
		if ((s = symnam[i]) != '\0') {
915
868
			if (s[0] == '"') {
916
				k = 7;
917
				while (*++s != '"') {
918
					++k;
919
					if (*s == '\\') {
920
						k += 2;
921
						if (*++s == '\\')
922
							++k;
923
					}
924
				}
925
				j += k;
926
				if (j > 80) {
927
					if (!rflag)
928
						++outline;
929
					putc('\n', output_file);
930
					j = k;
931
				}
932
				fprintf(output_file, "\"\\\"");
933
				s = symnam[i];
934
				while (*++s != '"') {
935
					if (*s == '\\') {
936
						fprintf(output_file, "\\\\");
937
						if (*++s == '\\')
938
							fprintf(output_file, "\\\\");
939
						else
940
							putc(*s, output_file);
941
					} else
942
						putc(*s, output_file);
943
				}
944
				fprintf(output_file, "\\\"\",");
945
868
			} else if (s[0] == '\'') {
946
50
				if (s[1] == '"') {
947
					j += 7;
948
					if (j > 80) {
949
						if (!rflag)
950
							++outline;
951
						putc('\n', output_file);
952
						j = 7;
953
					}
954
					fprintf(output_file, "\"'\\\"'\",");
955
				} else {
956
					k = 5;
957
150
					while (*++s != '\'') {
958
50
						++k;
959
50
						if (*s == '\\') {
960
1
							k += 2;
961
1
							if (*++s == '\\')
962
								++k;
963
						}
964
					}
965
50
					j += k;
966
50
					if (j > 80) {
967
4
						if (!rflag)
968
4
							++outline;
969
8
						putc('\n', output_file);
970
						j = k;
971
4
					}
972
50
					fprintf(output_file, "\"'");
973
50
					s = symnam[i];
974
150
					while (*++s != '\'') {
975
50
						if (*s == '\\') {
976
1
							fprintf(output_file, "\\\\");
977
1
							if (*++s == '\\')
978
								fprintf(output_file, "\\\\");
979
							else
980
2
								putc(*s, output_file);
981
						} else
982
98
							putc(*s, output_file);
983
					}
984
50
					fprintf(output_file, "'\",");
985
				}
986
			} else {
987
818
				k = strlen(s) + 3;
988
818
				j += k;
989
818
				if (j > 80) {
990
118
					if (!rflag)
991
118
						++outline;
992
236
					putc('\n', output_file);
993
					j = k;
994
118
				}
995
1636
				putc('"', output_file);
996
				do {
997
10750
					putc(*s, output_file);
998
5375
				} while (*++s);
999
818
				fprintf(output_file, "\",");
1000
			}
1001
		} else {
1002
3790
			j += 2;
1003
3790
			if (j > 80) {
1004
89
				if (!rflag)
1005
89
					++outline;
1006
178
				putc('\n', output_file);
1007
				j = 2;
1008
89
			}
1009
3790
			fprintf(output_file, "0,");
1010
		}
1011
	}
1012
15
	if (!rflag)
1013
15
		outline += 2;
1014
15
	fprintf(output_file, "\n};\n");
1015
15
	free(symnam);
1016
1017
15
	if (!rflag)
1018
15
		++outline;
1019
30
	fprintf(output_file,
1020
	    "const char * const %srule[] =\n"
1021
15
	    "\t{", symbol_prefix);
1022
3760
	for (i = 2; i < nrules; ++i) {
1023
1865
		fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
1024
12022
		for (j = rrhs[i]; ritem[j] > 0; ++j) {
1025
4146
			s = symbol_name[ritem[j]];
1026
4146
			if (s[0] == '"') {
1027
				fprintf(output_file, " \\\"");
1028
				while (*++s != '"') {
1029
					if (*s == '\\') {
1030
						if (s[1] == '\\')
1031
							fprintf(output_file, "\\\\\\\\");
1032
						else
1033
							fprintf(output_file, "\\\\%c", s[1]);
1034
						++s;
1035
					} else
1036
						putc(*s, output_file);
1037
				}
1038
				fprintf(output_file, "\\\"");
1039
4146
			} else if (s[0] == '\'') {
1040
202
				if (s[1] == '"')
1041
					fprintf(output_file, " '\\\"'");
1042
202
				else if (s[1] == '\\') {
1043
22
					if (s[2] == '\\')
1044
						fprintf(output_file, " '\\\\\\\\");
1045
					else
1046
22
						fprintf(output_file, " '\\\\%c", s[2]);
1047
					s += 2;
1048
44
					while (*++s != '\'')
1049
						putc(*s, output_file);
1050
44
					putc('\'', output_file);
1051
				} else
1052
180
					fprintf(output_file, " '%c'", s[1]);
1053
			} else
1054
3944
				fprintf(output_file, " %s", s);
1055
		}
1056
1865
		if (!rflag)
1057
1865
			++outline;
1058
1865
		fprintf(output_file, "\",\n");
1059
	}
1060
1061
15
	if (!rflag)
1062
15
		outline += 2;
1063
15
	fprintf(output_file, "};\n#endif\n");
1064
15
}
1065
1066
1067
void
1068
output_stype(void)
1069
{
1070
30
	if (!unionized && ntags == 0) {
1071
		outline += 3;
1072
		fprintf(code_file, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
1073
	}
1074
15
}
1075
1076
1077
void
1078
output_trailing_text(void)
1079
{
1080
	int c, last;
1081
	FILE *in, *out;
1082
1083
30
	if (line == 0)
1084
		return;
1085
1086
15
	in = input_file;
1087
15
	out = code_file;
1088
15
	c = (unsigned char)*cptr;
1089
15
	if (c == '\n') {
1090
15
		++lineno;
1091

60
		if ((c = getc(in)) == EOF)
1092
2
			return;
1093
13
		if (!lflag) {
1094
13
			++outline;
1095
13
			fprintf(out, line_format, lineno, input_file_name);
1096
13
		}
1097
13
		if (c == '\n')
1098
12
			++outline;
1099
26
		putc(c, out);
1100
		last = c;
1101
13
	} else {
1102
		if (!lflag) {
1103
			++outline;
1104
			fprintf(out, line_format, lineno, input_file_name);
1105
		}
1106
		do {
1107
			putc(c, out);
1108
		} while ((c = (unsigned char)*++cptr) != '\n');
1109
		++outline;
1110
		putc('\n', out);
1111
		last = '\n';
1112
	}
1113
1114

652452
	while ((c = getc(in)) != EOF) {
1115
130480
		if (c == '\n')
1116
5930
			++outline;
1117
260960
		putc(c, out);
1118
		last = c;
1119
	}
1120
1121
13
	if (last != '\n') {
1122
		++outline;
1123
		putc('\n', out);
1124
	}
1125
13
	if (!lflag)
1126
13
		fprintf(out, line_format, ++outline + 1, code_file_name);
1127
28
}
1128
1129
1130
void
1131
output_semantic_actions(void)
1132
{
1133
	int c, last;
1134
	FILE *out;
1135
1136
30
	fclose(action_file);
1137
15
	action_file = fopen(action_file_name, "r");
1138
15
	if (action_file == NULL)
1139
		open_error(action_file_name);
1140
1141

60
	if ((c = getc(action_file)) == EOF)
1142
		return;
1143
1144
15
	out = code_file;
1145
	last = c;
1146
15
	if (c == '\n')
1147
		++outline;
1148
30
	putc(c, out);
1149

1611320
	while ((c = getc(action_file)) != EOF) {
1150
322252
		if (c == '\n')
1151
13188
			++outline;
1152
644504
		putc(c, out);
1153
		last = c;
1154
	}
1155
1156
15
	if (last != '\n') {
1157
		++outline;
1158
		putc('\n', out);
1159
	}
1160
1161
15
	if (!lflag)
1162
15
		fprintf(out, line_format, ++outline + 1, code_file_name);
1163
30
}
1164
1165
1166
void
1167
free_itemsets(void)
1168
{
1169
	core *cp, *next;
1170
1171
30
	free(state_table);
1172
6880
	for (cp = first_state; cp; cp = next) {
1173
3425
		next = cp->next;
1174
3425
		free(cp);
1175
	}
1176
15
}
1177
1178
1179
void
1180
free_shifts(void)
1181
{
1182
	shifts *sp, *next;
1183
1184
30
	free(shift_table);
1185
4214
	for (sp = first_shift; sp; sp = next) {
1186
2092
		next = sp->next;
1187
2092
		free(sp);
1188
	}
1189
15
}
1190
1191
1192
1193
void
1194
free_reductions(void)
1195
{
1196
	reductions *rp, *next;
1197
1198
30
	free(reduction_table);
1199
4156
	for (rp = first_reduction; rp; rp = next) {
1200
2063
		next = rp->next;
1201
2063
		free(rp);
1202
	}
1203
15
}