GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/yacc/output.c Lines: 584 690 84.6 %
Date: 2017-11-13 Branches: 335 516 64.9 %

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
18
	free_itemsets();
84
9
	free_shifts();
85
9
	free_reductions();
86
9
	output_prefix();
87
9
	output_stored_text();
88
9
	output_defines();
89
9
	output_rule_data();
90
9
	output_yydefred();
91
9
	output_actions();
92
9
	free_parser();
93
9
	output_debug();
94
9
	output_stype();
95
9
	if (rflag)
96
		write_section(tables);
97
9
	write_section(header);
98
9
	output_trailing_text();
99
9
	write_section(body);
100
9
	output_semantic_actions();
101
9
	write_section(trailer);
102
9
}
103
104
105
void
106
output_prefix(void)
107
{
108
18
	if (symbol_prefix == NULL)
109
5
		symbol_prefix = "yy";
110
	else {
111
4
		++outline;
112
4
		fprintf(code_file, "#define yyparse %sparse\n", symbol_prefix);
113
4
		++outline;
114
4
		fprintf(code_file, "#define yylex %slex\n", symbol_prefix);
115
4
		++outline;
116
4
		fprintf(code_file, "#define yyerror %serror\n", symbol_prefix);
117
4
		++outline;
118
4
		fprintf(code_file, "#define yychar %schar\n", symbol_prefix);
119
4
		++outline;
120
4
		fprintf(code_file, "#define yyval %sval\n", symbol_prefix);
121
4
		++outline;
122
4
		fprintf(code_file, "#define yylval %slval\n", symbol_prefix);
123
4
		++outline;
124
4
		fprintf(code_file, "#define yydebug %sdebug\n", symbol_prefix);
125
4
		++outline;
126
4
		fprintf(code_file, "#define yynerrs %snerrs\n", symbol_prefix);
127
4
		++outline;
128
4
		fprintf(code_file, "#define yyerrflag %serrflag\n", symbol_prefix);
129
4
		++outline;
130
4
		fprintf(code_file, "#define yyss %sss\n", symbol_prefix);
131
4
		++outline;
132
4
		fprintf(code_file, "#define yysslim %ssslim\n", symbol_prefix);
133
4
		++outline;
134
4
		fprintf(code_file, "#define yyssp %sssp\n", symbol_prefix);
135
4
		++outline;
136
4
		fprintf(code_file, "#define yyvs %svs\n", symbol_prefix);
137
4
		++outline;
138
4
		fprintf(code_file, "#define yyvsp %svsp\n", symbol_prefix);
139
4
		++outline;
140
4
		fprintf(code_file, "#define yystacksize %sstacksize\n", symbol_prefix);
141
4
		++outline;
142
4
		fprintf(code_file, "#define yylhs %slhs\n", symbol_prefix);
143
4
		++outline;
144
4
		fprintf(code_file, "#define yylen %slen\n", symbol_prefix);
145
4
		++outline;
146
4
		fprintf(code_file, "#define yydefred %sdefred\n", symbol_prefix);
147
4
		++outline;
148
4
		fprintf(code_file, "#define yydgoto %sdgoto\n", symbol_prefix);
149
4
		++outline;
150
4
		fprintf(code_file, "#define yysindex %ssindex\n", symbol_prefix);
151
4
		++outline;
152
4
		fprintf(code_file, "#define yyrindex %srindex\n", symbol_prefix);
153
4
		++outline;
154
4
		fprintf(code_file, "#define yygindex %sgindex\n", symbol_prefix);
155
4
		++outline;
156
4
		fprintf(code_file, "#define yytable %stable\n", symbol_prefix);
157
4
		++outline;
158
4
		fprintf(code_file, "#define yycheck %scheck\n", symbol_prefix);
159
4
		++outline;
160
4
		fprintf(code_file, "#define yyname %sname\n", symbol_prefix);
161
4
		++outline;
162
4
		fprintf(code_file, "#define yyrule %srule\n", symbol_prefix);
163
	}
164
9
	++outline;
165
9
	fprintf(code_file, "#define YYPREFIX \"%s\"\n", symbol_prefix);
166
9
}
167
168
169
void
170
output_rule_data(void)
171
{
172
	int i;
173
	int j;
174
175
27
	fprintf(output_file,
176
	    "const short %slhs[] =\n"
177
9
	    "\t{%42d,", symbol_prefix, symbol_value[start_symbol]);
178
179
	j = 10;
180
1806
	for (i = 3; i < nrules; i++) {
181
894
		if (j >= 10) {
182
93
			if (!rflag)
183
93
				++outline;
184
186
			putc('\n', output_file);
185
			j = 1;
186
93
		} else
187
801
			++j;
188
894
		fprintf(output_file, "%5d,", symbol_value[rlhs[i]]);
189
	}
190
9
	if (!rflag)
191
9
		outline += 2;
192
9
	fprintf(output_file, "\n};\n");
193
194
18
	fprintf(output_file,
195
	    "const short %slen[] =\n"
196
9
	    "\t{%42d,", symbol_prefix, 2);
197
198
	j = 10;
199
1806
	for (i = 3; i < nrules; i++) {
200
894
		if (j >= 10) {
201
93
			if (!rflag)
202
93
				++outline;
203
186
			putc('\n', output_file);
204
			j = 1;
205
93
		} else
206
801
			j++;
207
894
		fprintf(output_file, "%5d,", rrhs[i + 1] - rrhs[i] - 1);
208
	}
209
9
	if (!rflag)
210
9
		outline += 2;
211
9
	fprintf(output_file, "\n};\n");
212
9
}
213
214
215
void
216
output_yydefred(void)
217
{
218
	int i, j;
219
220
27
	fprintf(output_file,
221
	    "const short %sdefred[] =\n"
222
	    "\t{%39d,",
223
25
	    symbol_prefix, (defred[0] ? defred[0] - 2 : 0));
224
225
	j = 10;
226
4272
	for (i = 1; i < nstates; i++) {
227
2127
		if (j < 10)
228
1913
			++j;
229
		else {
230
214
			if (!rflag)
231
214
				++outline;
232
428
			putc('\n', output_file);
233
			j = 1;
234
		}
235
5146
		fprintf(output_file, "%5d,", (defred[i] ? defred[i] - 2 : 0));
236
	}
237
238
9
	if (!rflag)
239
9
		outline += 2;
240
9
	fprintf(output_file, "\n};\n");
241
9
}
242
243
244
void
245
output_actions(void)
246
{
247
18
	nvectors = 2 * nstates + nvars;
248
249
9
	froms = NEW2(nvectors, short *);
250
9
	tos = NEW2(nvectors, short *);
251
9
	tally = NEW2(nvectors, short);
252
9
	width = NEW2(nvectors, short);
253
254
9
	token_actions();
255
9
	free(lookaheads);
256
9
	free(LA);
257
9
	free(LAruleno);
258
9
	free(accessing_symbol);
259
260
9
	goto_actions();
261
9
	free(goto_map + ntokens);
262
9
	free(from_state);
263
9
	free(to_state);
264
265
9
	sort_actions();
266
9
	pack_table();
267
9
	output_base();
268
9
	output_table();
269
9
	output_check();
270
9
}
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
18
	actionrow = NEW2(2*ntokens, short);
283
4290
	for (i = 0; i < nstates; ++i) {
284
2136
		if (parser[i]) {
285
591640
			for (j = 0; j < 2 * ntokens; ++j)
286
293688
				actionrow[j] = 0;
287
			shiftcount = 0;
288
			reducecount = 0;
289
43828
			for (p = parser[i]; p; p = p->next) {
290
19782
				if (p->suppressed == 0) {
291
19470
					if (p->action_code == SHIFT) {
292
3701
						++shiftcount;
293
3701
						actionrow[p->symbol] = p->number;
294

35239
					} else if (p->action_code == REDUCE &&
295
15769
					    p->number != defred[i]) {
296
1208
						++reducecount;
297
1208
						actionrow[p->symbol + ntokens] = p->number;
298
1208
					}
299
				}
300
			}
301
302
2132
			tally[i] = shiftcount;
303
2132
			tally[nstates+i] = reducecount;
304
2132
			width[i] = 0;
305
2132
			width[nstates+i] = 0;
306
2132
			if (shiftcount > 0) {
307
1229
				froms[i] = r = NEW2(shiftcount, short);
308
1229
				tos[i] = s = NEW2(shiftcount, short);
309
				min = MAXSHORT;
310
				max = 0;
311
168848
				for (j = 0; j < ntokens; ++j) {
312
83195
					if (actionrow[j]) {
313
3701
						if (min > symbol_value[j])
314
1345
							min = symbol_value[j];
315
3701
						if (max < symbol_value[j])
316
3479
							max = symbol_value[j];
317
3701
						*r++ = symbol_value[j];
318
3701
						*s++ = actionrow[j];
319
3701
					}
320
				}
321
1229
				width[i] = max - min + 1;
322
1229
			}
323
2132
			if (reducecount > 0) {
324
172
				froms[nstates+i] = r = NEW2(reducecount, short);
325
172
				tos[nstates+i] = s = NEW2(reducecount, short);
326
				min = MAXSHORT;
327
				max = 0;
328
21824
				for (j = 0; j < ntokens; ++j) {
329
10740
					if (actionrow[ntokens+j]) {
330
1208
						if (min > symbol_value[j])
331
190
							min = symbol_value[j];
332
1208
						if (max < symbol_value[j])
333
958
							max = symbol_value[j];
334
1208
						*r++ = symbol_value[j];
335
1208
						*s++ = actionrow[ntokens+j] - 2;
336
1208
					}
337
				}
338
172
				width[nstates+i] = max - min + 1;
339
172
			}
340
		}
341
	}
342
9
	free(actionrow);
343
9
}
344
345
void
346
goto_actions(void)
347
{
348
	int i, j, k;
349
350
18
	state_count = NEW2(nstates, short);
351
352
9
	k = default_goto(start_symbol + 1);
353
18
	fprintf(output_file, "const short %sdgoto[] =\n"
354
9
	    "\t{%40d,", symbol_prefix, k);
355
9
	save_column(start_symbol + 1, k);
356
357
	j = 10;
358
462
	for (i = start_symbol + 2; i < nsyms; i++) {
359
222
		if (j >= 10) {
360
26
			if (!rflag)
361
26
				++outline;
362
52
			putc('\n', output_file);
363
			j = 1;
364
26
		} else
365
196
			++j;
366
367
222
		k = default_goto(i);
368
222
		fprintf(output_file, "%5d,", k);
369
222
		save_column(i, k);
370
	}
371
372
9
	if (!rflag)
373
9
		outline += 2;
374
9
	fprintf(output_file, "\n};\n");
375
9
	free(state_count);
376
9
}
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
462
	m = goto_map[symbol];
388
231
	n = goto_map[symbol + 1];
389
390
231
	if (m == n)
391
		return (0);
392
393
231
	memset(state_count, 0, nstates * sizeof(short));
394
395
2808
	for (i = m; i < n; i++)
396
1173
		state_count[to_state[i]]++;
397
398
	max = 0;
399
	default_state = 0;
400
103606
	for (i = 0; i < nstates; i++) {
401
51572
		if (state_count[i] > max) {
402
			max = state_count[i];
403
			default_state = i;
404
243
		}
405
	}
406
407
231
	return (default_state);
408
231
}
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
462
	m = goto_map[symbol];
425
231
	n = goto_map[symbol + 1];
426
427
	count = 0;
428
2808
	for (i = m; i < n; i++) {
429
1173
		if (to_state[i] != default_state)
430
522
			++count;
431
	}
432
231
	if (count == 0)
433
151
		return;
434
435
80
	symno = symbol_value[symbol] + 2*nstates;
436
437
80
	froms[symno] = sp1 = sp = NEW2(count, short);
438
80
	tos[symno] = sp2 = NEW2(count, short);
439
440
1856
	for (i = m; i < n; i++) {
441
848
		if (to_state[i] != default_state) {
442
522
			*sp1++ = from_state[i];
443
522
			*sp2++ = to_state[i];
444
522
		}
445
	}
446
447
80
	tally[symno] = count;
448
80
	width[symno] = sp1[-1] - sp[0] + 1;
449
311
}
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
18
	order = NEW2(nvectors, short);
461
9
	nentries = 0;
462
463
9042
	for (i = 0; i < nvectors; i++) {
464
4512
		if (tally[i] > 0) {
465
			t = tally[i];
466
1481
			w = width[i];
467
1481
			j = nentries - 1;
468
469

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

6839
			while (j >= 0 && (width[order[j]] == w) &&
473
1581
			    (tally[order[j]] < t))
474
282
				j--;
475
476
53824
			for (k = nentries - 1; k > j; k--)
477
25431
				order[k + 1] = order[k];
478
479
1481
			order[j + 1] = i;
480
1481
			nentries++;
481
1481
		}
482
	}
483
9
}
484
485
486
void
487
pack_table(void)
488
{
489
	int i;
490
	int place;
491
	int state;
492
493
18
	base = NEW2(nvectors, short);
494
9
	pos = NEW2(nentries, short);
495
496
9
	maxtable = 1000;
497
9
	table = NEW2(maxtable, short);
498
9
	check = NEW2(maxtable, short);
499
500
9
	lowzero = 0;
501
9
	high = 0;
502
503
18018
	for (i = 0; i < maxtable; i++)
504
9000
		check[i] = -1;
505
506
2980
	for (i = 0; i < nentries; i++) {
507
1481
		state = matching_vector(i);
508
509
1481
		if (state < 0)
510
1183
			place = pack_vector(i);
511
		else
512
298
			place = base[state];
513
514
1481
		pos[i] = place;
515
1481
		base[order[i]] = place;
516
	}
517
518
9042
	for (i = 0; i < nvectors; i++) {
519
4512
		free(froms[i]);
520
4512
		free(tos[i]);
521
	}
522
523
9
	free(froms);
524
9
	free(tos);
525
9
	free(pos);
526
9
}
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
2962
	i = order[vector];
551
1481
	if (i >= 2*nstates)
552
80
		return (-1);
553
554
1401
	t = tally[i];
555
1401
	w = width[i];
556
557
119470
	for (prev = vector - 1; prev >= 0; prev--) {
558
59731
		j = order[prev];
559

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

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

62361
			if (tos[j][k] != tos[i][k] ||
565
2017
			    froms[j][k] != froms[i][k])
566
58334
				match = 0;
567
		}
568
569
58632
		if (match)
570
298
			return (j);
571
	}
572
573
4
	return (-1);
574
1481
}
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
2366
	i = order[vector];
587
1183
	t = tally[i];
588
1183
	assert(t);
589
590
1183
	from = froms[i];
591
1183
	to = tos[i];
592
593
1183
	j = lowzero - from[0];
594
6842
	for (k = 1; k < t; ++k)
595
2238
		if (lowzero - from[k] > j)
596
56
			j = lowzero - from[k];
597
77365
	for (;; ++j) {
598
77365
		if (j == 0)
599
			continue;
600
		ok = 1;
601

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

2641891
		for (k = 0; ok && k < vector; k++) {
628
828760
			if (pos[k] == j)
629
6712
				ok = 0;
630
		}
631
77214
		if (ok) {
632
9208
			for (k = 0; k < t; k++) {
633
3421
				loc = j + from[k];
634
3421
				table[loc] = to[k];
635
3421
				check[loc] = from[k];
636
3421
				if (loc > high)
637
1099
					high = loc;
638
			}
639
640

9342
			while (lowzero < maxtable && check[lowzero] != -1)
641
1931
				++lowzero;
642
643
1183
			return (j);
644
		}
645
	}
646
}
647
648
649
650
void
651
output_base(void)
652
{
653
	int i, j;
654
655
27
	fprintf(output_file, "const short %ssindex[] =\n"
656
9
	    "\t{%39d,", symbol_prefix, base[0]);
657
658
	j = 10;
659
4272
	for (i = 1; i < nstates; i++) {
660
2127
		if (j >= 10) {
661
214
			if (!rflag)
662
214
				++outline;
663
428
			putc('\n', output_file);
664
			j = 1;
665
214
		} else
666
1913
			++j;
667
2127
		fprintf(output_file, "%5d,", base[i]);
668
	}
669
670
9
	if (!rflag)
671
9
		outline += 2;
672
18
	fprintf(output_file, "};\n"
673
	    "const short %srindex[] =\n"
674
9
	    "\t{%39d,", symbol_prefix, base[nstates]);
675
676
	j = 10;
677
4272
	for (i = nstates + 1; i < 2*nstates; i++) {
678
2127
		if (j >= 10) {
679
214
			if (!rflag)
680
214
				++outline;
681
428
			putc('\n', output_file);
682
			j = 1;
683
214
		} else
684
1913
			++j;
685
2127
		fprintf(output_file, "%5d,", base[i]);
686
	}
687
688
9
	if (!rflag)
689
9
		outline += 2;
690
18
	fprintf(output_file, "};\n"
691
	    "const short %sgindex[] =\n"
692
9
	    "\t{%39d,", symbol_prefix, base[2*nstates]);
693
694
	j = 10;
695
462
	for (i = 2*nstates + 1; i < nvectors - 1; i++) {
696
222
		if (j >= 10) {
697
26
			if (!rflag)
698
26
				++outline;
699
52
			putc('\n', output_file);
700
			j = 1;
701
26
		} else
702
196
			++j;
703
222
		fprintf(output_file, "%5d,", base[i]);
704
	}
705
706
9
	if (!rflag)
707
9
		outline += 2;
708
9
	fprintf(output_file, "\n};\n");
709
9
	free(base);
710
9
}
711
712
713
void
714
output_table(void)
715
{
716
	int i, j;
717
718
18
	++outline;
719
9
	fprintf(code_file, "#define YYTABLESIZE %d\n", high);
720
18
	fprintf(output_file, "const short %stable[] =\n"
721
9
	    "\t{%40d,", symbol_prefix, table[0]);
722
723
	j = 10;
724
8922
	for (i = 1; i <= high; i++) {
725
4452
		if (j >= 10) {
726
451
			if (!rflag)
727
451
				++outline;
728
902
			putc('\n', output_file);
729
			j = 1;
730
451
		} else
731
4001
			++j;
732
4452
		fprintf(output_file, "%5d,", table[i]);
733
	}
734
735
9
	if (!rflag)
736
9
		outline += 2;
737
9
	fprintf(output_file, "\n};\n");
738
9
	free(table);
739
9
}
740
741
742
void
743
output_check(void)
744
{
745
	int i, j;
746
747
27
	fprintf(output_file, "const short %scheck[] =\n"
748
9
	    "\t{%40d,", symbol_prefix, check[0]);
749
750
	j = 10;
751
8922
	for (i = 1; i <= high; i++) {
752
4452
		if (j >= 10) {
753
451
			if (!rflag)
754
451
				++outline;
755
902
			putc('\n', output_file);
756
			j = 1;
757
451
		} else
758
4001
			++j;
759
4452
		fprintf(output_file, "%5d,", check[i]);
760
	}
761
762
9
	if (!rflag)
763
9
		outline += 2;
764
9
	fprintf(output_file, "\n};\n");
765
9
	free(check);
766
9
}
767
768
769
int
770
is_C_identifier(char *name)
771
{
772
	char *s;
773
	int c;
774
775
	s = name;
776
1182
	c = (unsigned char)*s;
777
591
	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
591
	if (!isalpha(c) && c != '_' && c != '$')
789
30
		return (0);
790
4596
	while ((c = (unsigned char)*++s)) {
791
1737
		if (!isalnum(c) && c != '_' && c != '$')
792
			return (0);
793
	}
794
561
	return (1);
795
591
}
796
797
798
void
799
output_defines(void)
800
{
801
	int c, i;
802
	char *s;
803
804
1209
	for (i = 2; i < ntokens; ++i) {
805
591
		s = symbol_name[i];
806
591
		if (is_C_identifier(s)) {
807
561
			fprintf(code_file, "#define ");
808
561
			if (dflag)
809
216
				fprintf(defines_file, "#define ");
810
561
			c = (unsigned char)*s;
811
561
			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
561
				do {
819
4596
					putc(c, code_file);
820
2298
					if (dflag)
821
2156
						putc(c, defines_file);
822
2298
				} while ((c = (unsigned char)*++s));
823
			}
824
561
			++outline;
825
561
			fprintf(code_file, " %d\n", symbol_value[i]);
826
561
			if (dflag)
827
216
				fprintf(defines_file, " %d\n", symbol_value[i]);
828
		}
829
	}
830
831
9
	++outline;
832
9
	fprintf(code_file, "#define YYERRCODE %d\n", symbol_value[1]);
833
834

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

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

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

60530
	while ((c = getc(in)) != EOF) {
867
12097
		if (c == '\n')
868
583
			++outline;
869
24194
		putc(c, out);
870
	}
871
9
	if (!lflag)
872
9
		fprintf(out, line_format, ++outline + 1, code_file_name);
873
18
}
874
875
876
void
877
output_debug(void)
878
{
879
	int i, j, k, max;
880
	char **symnam, *s;
881
882
18
	++outline;
883
9
	fprintf(code_file, "#define YYFINAL %d\n", final_state);
884
9
	outline += 3;
885
18
	fprintf(code_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
886
9
		tflag);
887
9
	if (rflag)
888
		fprintf(output_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
889
		    tflag);
890
891
	max = 0;
892
1200
	for (i = 2; i < ntokens; ++i)
893
591
		if (symbol_value[i] > max)
894
561
			max = symbol_value[i];
895
9
	++outline;
896
9
	fprintf(code_file, "#define YYMAXTOKEN %d\n", max);
897
898
9
	symnam = calloc(max+1, sizeof(char *));
899
9
	if (symnam == NULL)
900
		no_space();
901
902
1200
	for (i = ntokens - 1; i >= 2; --i)
903
591
		symnam[symbol_value[i]] = symbol_name[i];
904
9
	symnam[0] = "end-of-file";
905
906
9
	if (!rflag)
907
9
		++outline;
908
18
	fprintf(output_file,
909
	    "#if YYDEBUG\n"
910
	    "const char * const %sname[] =\n"
911
9
	    "\t{", symbol_prefix);
912
	j = 80;
913
5766
	for (i = 0; i <= max; ++i) {
914
2874
		if ((s = symnam[i]) != '\0') {
915
600
			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
600
			} else if (s[0] == '\'') {
946
30
				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
120
					while (*++s != '\'') {
958
30
						++k;
959
30
						if (*s == '\\') {
960
							k += 2;
961
							if (*++s == '\\')
962
								++k;
963
						}
964
					}
965
30
					j += k;
966
30
					if (j > 80) {
967
4
						if (!rflag)
968
4
							++outline;
969
8
						putc('\n', output_file);
970
						j = k;
971
4
					}
972
30
					fprintf(output_file, "\"'");
973
30
					s = symnam[i];
974
120
					while (*++s != '\'') {
975
30
						if (*s == '\\') {
976
							fprintf(output_file, "\\\\");
977
							if (*++s == '\\')
978
								fprintf(output_file, "\\\\");
979
							else
980
								putc(*s, output_file);
981
						} else
982
60
							putc(*s, output_file);
983
					}
984
30
					fprintf(output_file, "'\",");
985
				}
986
			} else {
987
570
				k = strlen(s) + 3;
988
570
				j += k;
989
570
				if (j > 80) {
990
61
					if (!rflag)
991
61
						++outline;
992
122
					putc('\n', output_file);
993
					j = k;
994
61
				}
995
1140
				putc('"', output_file);
996
570
				do {
997
4794
					putc(*s, output_file);
998
2397
				} while (*++s);
999
570
				fprintf(output_file, "\",");
1000
			}
1001
		} else {
1002
2274
			j += 2;
1003
2274
			if (j > 80) {
1004
52
				if (!rflag)
1005
52
					++outline;
1006
104
				putc('\n', output_file);
1007
				j = 2;
1008
52
			}
1009
2274
			fprintf(output_file, "0,");
1010
		}
1011
	}
1012
9
	if (!rflag)
1013
9
		outline += 2;
1014
9
	fprintf(output_file, "\n};\n");
1015
9
	free(symnam);
1016
1017
9
	if (!rflag)
1018
9
		++outline;
1019
18
	fprintf(output_file,
1020
	    "const char * const %srule[] =\n"
1021
9
	    "\t{", symbol_prefix);
1022
1824
	for (i = 2; i < nrules; ++i) {
1023
903
		fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
1024
6922
		for (j = rrhs[i]; ritem[j] > 0; ++j) {
1025
2558
			s = symbol_name[ritem[j]];
1026
2558
			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
2558
			} else if (s[0] == '\'') {
1040
56
				if (s[1] == '"')
1041
					fprintf(output_file, " '\\\"'");
1042
56
				else if (s[1] == '\\') {
1043
					if (s[2] == '\\')
1044
						fprintf(output_file, " '\\\\\\\\");
1045
					else
1046
						fprintf(output_file, " '\\\\%c", s[2]);
1047
					s += 2;
1048
					while (*++s != '\'')
1049
						putc(*s, output_file);
1050
					putc('\'', output_file);
1051
				} else
1052
56
					fprintf(output_file, " '%c'", s[1]);
1053
			} else
1054
2502
				fprintf(output_file, " %s", s);
1055
		}
1056
903
		if (!rflag)
1057
903
			++outline;
1058
903
		fprintf(output_file, "\",\n");
1059
	}
1060
1061
9
	if (!rflag)
1062
9
		outline += 2;
1063
9
	fprintf(output_file, "};\n#endif\n");
1064
9
}
1065
1066
1067
void
1068
output_stype(void)
1069
{
1070
18
	if (!unionized && ntags == 0) {
1071
		outline += 3;
1072
		fprintf(code_file, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
1073
	}
1074
9
}
1075
1076
1077
void
1078
output_trailing_text(void)
1079
{
1080
	int c, last;
1081
	FILE *in, *out;
1082
1083
18
	if (line == 0)
1084
		return;
1085
1086
9
	in = input_file;
1087
9
	out = code_file;
1088
9
	c = (unsigned char)*cptr;
1089
9
	if (c == '\n') {
1090
9
		++lineno;
1091

36
		if ((c = getc(in)) == EOF)
1092
2
			return;
1093
7
		if (!lflag) {
1094
7
			++outline;
1095
7
			fprintf(out, line_format, lineno, input_file_name);
1096
7
		}
1097
7
		if (c == '\n')
1098
7
			++outline;
1099
14
		putc(c, out);
1100
		last = c;
1101
7
	} 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

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

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

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