File: skeleton.c

package info (click to toggle)
perl-byacc 2.0-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 716 kB
  • sloc: ansic: 7,136; yacc: 2,035; perl: 1,779; makefile: 206; sh: 9
file content (607 lines) | stat: -rw-r--r-- 17,478 bytes parent folder | download | duplicates (7)
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
#include "defs.h"

/*  The banner used here should be replaced with an #ident directive    */
/*  if the target C compiler supports #ident directives.                */
/*                                                                      */
/*  If the skeleton is changed, the banner should be changed so that    */
/*  the altered version can easily be distinguished from the original.  */

static char *c_banner[] =
{
    "#ifndef lint",
    "static char yysccsid[] = \"@(#)yaccpar 1.8 (Berkeley) 01/20/91\";",
    "#endif",
    "#define YYBYACC 1",
    (char *) NULL
};

static char *perl_banner[] =
{
    "# @(#)yaccpar 1.8 (Berkeley) 01/20/91 (JAKE-P5BP-0.6 04/26/98)",
    (char *) NULL
};
  
char **banner[] = { c_banner, perl_banner };

static char *c_tables[] =
{
    "extern short yylhs[];",
    "extern short yylen[];",
    "extern short yydefred[];",
    "extern short yydgoto[];",
    "extern short yysindex[];",
    "extern short yyrindex[];",
    "extern short yygindex[];",
    "extern short yytable[];",
    "extern short yycheck[];",
    "#if YYDEBUG",
    "extern char *yyname[];",
    "extern char *yyrule[];",
    "#endif",
    (char *) NULL
};

char **tables[] = { c_tables, (char **) NULL };

static char *c_header[] =
{
    "#define yyclearin (yychar=(-1))",
    "#define yyerrok (yyerrflag=0)",
    "#ifdef YYSTACKSIZE",
    "#ifndef YYMAXDEPTH",
    "#define YYMAXDEPTH YYSTACKSIZE",
    "#endif",
    "#else",
    "#ifdef YYMAXDEPTH",
    "#define YYSTACKSIZE YYMAXDEPTH",
    "#else",
    "#define YYSTACKSIZE 500",
    "#define YYMAXDEPTH 500",
    "#endif",
    "#endif",
    "int yydebug;",
    "int yynerrs;",
    "int yyerrflag;",
    "int yychar;",
    "short *yyssp;",
    "YYSTYPE *yyvsp;",
    "YYSTYPE yyval;",
    "YYSTYPE yylval;",
    "short yyss[YYSTACKSIZE];",
    "YYSTYPE yyvs[YYSTACKSIZE];",
    "#define yystacksize YYSTACKSIZE",
    (char *) NULL
};

static char *perl_header[] =
{
    "sub yyclearin {",
    "  my PP $p;",
    "  ($p) = @_;",
    "  $p->{yychar} = -1;",
    "}",
    "sub yyerrok {",
    "  my PP $p;",
    "  ($p) = @_;",
    "  $p->{yyerrflag} = 0;",
    "}",
    (char *) NULL
};

char **header[] = { c_header, perl_header };

static char *c_body[] =
{
    "#define YYABORT goto yyabort",
    "#define YYACCEPT goto yyaccept",
    "#define YYERROR goto yyerrlab",
    "int",
    "yyparse()",
    "{",
    "    register int yym, yyn, yystate;",
    "#if YYDEBUG",
    "    register char *yys;",
    "    extern char *getenv();",
    "",
    "    if (yys = getenv(\"YYDEBUG\"))",
    "    {",
    "        yyn = *yys;",
    "        if (yyn >= '0' && yyn <= '9')",
    "            yydebug = yyn - '0';",
    "    }",
    "#endif",
    "",
    "    yynerrs = 0;",
    "    yyerrflag = 0;",
    "    yychar = (-1);",
    "",
    "    yyssp = yyss;",
    "    yyvsp = yyvs;",
    "    *yyssp = yystate = 0;",
    "",
    "yyloop:",
    "    if (yyn = yydefred[yystate]) goto yyreduce;",
    "    if (yychar < 0)",
    "    {",
    "        if ((yychar = yylex()) < 0) yychar = 0;",
    "#if YYDEBUG",
    "        if (yydebug)",
    "        {",
    "            yys = 0;",
    "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
    "            if (!yys) yys = \"illegal-symbol\";",
    "            printf(\"yydebug: state %d, reading %d (%s)\\n\", yystate,",
    "                    yychar, yys);",
    "        }",
    "#endif",
    "    }",
    "    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&",
    "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
    "    {",
    "#if YYDEBUG",
    "        if (yydebug)",
    "            printf(\"yydebug: state %d, shifting to state %d\\n\",",
    "                    yystate, yytable[yyn]);",
    "#endif",
    "        if (yyssp >= yyss + yystacksize - 1)",
    "        {",
    "            goto yyoverflow;",
    "        }",
    "        *++yyssp = yystate = yytable[yyn];",
    "        *++yyvsp = yylval;",
    "        yychar = (-1);",
    "        if (yyerrflag > 0)  --yyerrflag;",
    "        goto yyloop;",
    "    }",
    "    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&",
    "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
    "    {",
    "        yyn = yytable[yyn];",
    "        goto yyreduce;",
    "    }",
    "    if (yyerrflag) goto yyinrecovery;",
    "#ifdef lint",
    "    goto yynewerror;",
    "#endif",
    "yynewerror:",
    "    yyerror(\"syntax error\");",
    "#ifdef lint",
    "    goto yyerrlab;",
    "#endif",
    "yyerrlab:",
    "    ++yynerrs;",
    "yyinrecovery:",
    "    if (yyerrflag < 3)",
    "    {",
    "        yyerrflag = 3;",
    "        for (;;)",
    "        {",
    "            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&",
    "                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)",
    "            {",
    "#if YYDEBUG",
    "                if (yydebug)",
    "                    printf(\"yydebug: state %d, error recovery shifting\\",
    " to state %d\\n\", *yyssp, yytable[yyn]);",
    "#endif",
    "                if (yyssp >= yyss + yystacksize - 1)",
    "                {",
    "                    goto yyoverflow;",
    "                }",
    "                *++yyssp = yystate = yytable[yyn];",
    "                *++yyvsp = yylval;",
    "                goto yyloop;",
    "            }",
    "            else",
    "            {",
    "#if YYDEBUG",
    "                if (yydebug)",
    "                    printf(\"yydebug: error recovery discarding state %d\
\\n\",",
    "                            *yyssp);",
    "#endif",
    "                if (yyssp <= yyss) goto yyabort;",
    "                --yyssp;",
    "                --yyvsp;",
    "            }",
    "        }",
    "    }",
    "    else",
    "    {",
    "        if (yychar == 0) goto yyabort;",
    "#if YYDEBUG",
    "        if (yydebug)",
    "        {",
    "            yys = 0;",
    "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
    "            if (!yys) yys = \"illegal-symbol\";",
    "            printf(\"yydebug: state %d, error recovery discards token %d\
 (%s)\\n\",",
    "                    yystate, yychar, yys);",
    "        }",
    "#endif",
    "        yychar = (-1);",
    "        goto yyloop;",
    "    }",
    "yyreduce:",
    "#if YYDEBUG",
    "    if (yydebug)",
    "        printf(\"yydebug: state %d, reducing by rule %d (%s)\\n\",",
    "                yystate, yyn, yyrule[yyn]);",
    "#endif",
    "    yym = yylen[yyn];",
    "    yyval = yyvsp[1-yym];",
    "    switch (yyn)",
    "    {",
    (char *) NULL
};

static char *perl_body[] =
{
  /* This is kludgy--the top of this sub needs to vary based on -5, so
   * it was moved to output.c.
   */
    "  $p->{yylex} = $_[1];",
    "  $p->{yyerror} = $_[2];",
    "  $p->{yydebug} = $_[3];",
    "  return $p;",
    "}",
    "sub YYERROR {",
    "  my PP $p;",
    "  ($p) = @_;",
    "  ++$p->{yynerrs};",
    "  $p->yy_err_recover;",
    "}",
    "sub yy_err_recover {",
    "  my PP $p;",
    "  ($p) = @_;",
    "  if ($p->{yyerrflag} < 3)",
    "  {",
    "    $p->{yyerrflag} = 3;",
    "    while (1)",
    "    {",
    "      if (($p->{yyn} = $yysindex[$p->{yyss}->[$p->{yyssp}]]) && ",
    "          ($p->{yyn} += $YYERRCODE) >= 0 && ",
    "          $p->{yyn} <= $#yycheck &&",
    "          $yycheck[$p->{yyn}] == $YYERRCODE)",
    "      {",
    "        warn(\"yydebug: state \" . ",
    "                     $p->{yyss}->[$p->{yyssp}] . ",
    "                     \", error recovery shifting to state\" . ",
    "                     $yytable[$p->{yyn}] . \"\\n\") ",
    "                       if $p->{yydebug};",
    "        $p->{yyss}->[++$p->{yyssp}] = ",
    "          $p->{yystate} = $yytable[$p->{yyn}];",
    "        $p->{yyvs}->[++$p->{yyvsp}] = $p->{yylval};",
    "        next yyloop;",
    "      }",
    "      else",
    "      {",
    "        warn(\"yydebug: error recovery discarding state \".",
    "              $p->{yyss}->[$p->{yyssp}]. \"\\n\") ",
    "                if $p->{yydebug};",
    "        return(undef) if $p->{yyssp} <= 0;",
    "        --$p->{yyssp};",
    "        --$p->{yyvsp};",
    "      }",
    "    }",
    "  }",
    "  else",
    "  {",
    "    return (undef) if $p->{yychar} == 0;",
    "    if ($p->{yydebug})",
    "    {",
    "      $p->{yys} = '';",
    "      if ($p->{yychar} <= $YYMAXTOKEN) { $p->{yys} = ",
    "        $yyname[$p->{yychar}]; }",
    "      if (!$p->{yys}) { $p->{yys} = 'illegal-symbol'; }",
    "      warn(\"yydebug: state \" . $p->{yystate} . ",
    "                   \", error recovery discards \" . ",
    "                   \"token \" . $p->{yychar} . \"(\" . ",
    "                   $p->{yys} . \")\\n\");",
    "    }",
    "    $p->{yychar} = -1;",
    "    next yyloop;",
    "  }",
    "0;",
    "} # yy_err_recover",
    "",
    "sub yyparse {",
    "  my PP $p;",
    "  my $s;",
    "  ($p, $s) = @_;",
    "  if ($p->{yys} = $ENV{'YYDEBUG'})",
    "  {",
    "    $p->{yydebug} = int($1) if $p->{yys} =~ /^(\\d)/;",
    "  }",
    "",
    "  $p->{yynerrs} = 0;",
    "  $p->{yyerrflag} = 0;",
    "  $p->{yychar} = (-1);",
    "",
    "  $p->{yyssp} = 0;",
    "  $p->{yyvsp} = 0;",
    "  $p->{yyss}->[$p->{yyssp}] = $p->{yystate} = 0;",
    "",
    "yyloop: while(1)",
    "  {",
    "    yyreduce: {",
    "      last yyreduce if ($p->{yyn} = $yydefred[$p->{yystate}]);",
    "      if ($p->{yychar} < 0)",
    "      {",
    "        if ((($p->{yychar}, $p->{yylval}) = ",
    "            &{$p->{yylex}}($s)) < 0) { $p->{yychar} = 0; }",
    "        if ($p->{yydebug})",
    "        {",
    "          $p->{yys} = '';",
    "          if ($p->{yychar} <= $#yyname) ",
    "             { $p->{yys} = $yyname[$p->{yychar}]; }",
    "          if (!$p->{yys}) { $p->{yys} = 'illegal-symbol'; };",
    "          warn(\"yydebug: state \" . $p->{yystate} . ",
    "                       \", reading \" . $p->{yychar} . \" (\" . ",
    "                       $p->{yys} . \")\\n\");",
    "        }",
    "      }",
    "      if (($p->{yyn} = $yysindex[$p->{yystate}]) && ",
    "          ($p->{yyn} += $p->{yychar}) >= 0 && ",
    "          $p->{yyn} <= $#yycheck &&",
    "          $yycheck[$p->{yyn}] == $p->{yychar})",
    "      {",
    "        warn(\"yydebug: state \" . $p->{yystate} . ",
    "                     \", shifting to state \" .",
    "              $yytable[$p->{yyn}] . \"\\n\") if $p->{yydebug};",
    "        $p->{yyss}->[++$p->{yyssp}] = $p->{yystate} = ",
    "          $yytable[$p->{yyn}];",
    "        $p->{yyvs}->[++$p->{yyvsp}] = $p->{yylval};",
    "        $p->{yychar} = (-1);",
    "        --$p->{yyerrflag} if $p->{yyerrflag} > 0;",
    "        next yyloop;",
    "      }",
    "      if (($p->{yyn} = $yyrindex[$p->{yystate}]) && ",
    "          ($p->{yyn} += $p->{'yychar'}) >= 0 &&",
    "          $p->{yyn} <= $#yycheck &&",
    "          $yycheck[$p->{yyn}] == $p->{yychar})",
    "      {",
    "        $p->{yyn} = $yytable[$p->{yyn}];",
    "        last yyreduce;",
    "      }",
    "      if (! $p->{yyerrflag}) {",
    "        &{$p->{yyerror}}('syntax error', $s);",
    "        ++$p->{yynerrs};",
    "      }",
    "      return(undef) if $p->yy_err_recover;",
    "    } # yyreduce",
    "    warn(\"yydebug: state \" . $p->{yystate} . ",
    "                 \", reducing by rule \" . ",
    "                 $p->{yyn} . \" (\" . $yyrule[$p->{yyn}] . ",
    "                 \")\\n\") if $p->{yydebug};",
    "    $p->{yym} = $yylen[$p->{yyn}];",
    "    $p->{yyval} = $p->{yyvs}->[$p->{yyvsp}+1-$p->{yym}];",
    (char *) NULL
};

char **body[] = { c_body, perl_body };

static char *c_trailer[] =
{
    "    }",
    "    yyssp -= yym;",
    "    yystate = *yyssp;",
    "    yyvsp -= yym;",
    "    yym = yylhs[yyn];",
    "    if (yystate == 0 && yym == 0)",
    "    {",
    "#if YYDEBUG",
    "        if (yydebug)",
    "            printf(\"yydebug: after reduction, shifting from state 0 to\\",
    " state %d\\n\", YYFINAL);",
    "#endif",
    "        yystate = YYFINAL;",
    "        *++yyssp = YYFINAL;",
    "        *++yyvsp = yyval;",
    "        if (yychar < 0)",
    "        {",
    "            if ((yychar = yylex()) < 0) yychar = 0;",
    "#if YYDEBUG",
    "            if (yydebug)",
    "            {",
    "                yys = 0;",
    "                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
    "                if (!yys) yys = \"illegal-symbol\";",
    "                printf(\"yydebug: state %d, reading %d (%s)\\n\",",
    "                        YYFINAL, yychar, yys);",
    "            }",
    "#endif",
    "        }",
    "        if (yychar == 0) goto yyaccept;",
    "        goto yyloop;",
    "    }",
    "    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&",
    "            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)",
    "        yystate = yytable[yyn];",
    "    else",
    "        yystate = yydgoto[yym];",
    "#if YYDEBUG",
    "    if (yydebug)",
    "        printf(\"yydebug: after reduction, shifting from state %d \\",
    "to state %d\\n\", *yyssp, yystate);",
    "#endif",
    "    if (yyssp >= yyss + yystacksize - 1)",
    "    {",
    "        goto yyoverflow;",
    "    }",
    "    *++yyssp = yystate;",
    "    *++yyvsp = yyval;",
    "    goto yyloop;",
    "yyoverflow:",
    "    yyerror(\"yacc stack overflow\");",
    "yyabort:",
    "    return (1);",
    "yyaccept:",
    "    return (0);",
    "}",
    (char *) NULL
};

static char *perl_trailer[] =
{
    "    $p->{yyssp} -= $p->{yym};",
    "    $p->{yystate} = $p->{yyss}->[$p->{yyssp}];",
    "    $p->{yyvsp} -= $p->{yym};",
    "    $p->{yym} = $yylhs[$p->{yyn}];",
    "    if ($p->{yystate} == 0 && $p->{yym} == 0)",
    "    {",
    "      warn(\"yydebug: after reduction, shifting from state 0 \",",
    "            \"to state $YYFINAL\\n\") if $p->{yydebug};",
    "      $p->{yystate} = $YYFINAL;",
    "      $p->{yyss}->[++$p->{yyssp}] = $YYFINAL;",
    "      $p->{yyvs}->[++$p->{yyvsp}] = $p->{yyval};",
    "      if ($p->{yychar} < 0)",
    "      {",
    "        if ((($p->{yychar}, $p->{yylval}) = ",
    "            &{$p->{yylex}}($s)) < 0) { $p->{yychar} = 0; }",
    "        if ($p->{yydebug})",
    "        {",
    "          $p->{yys} = '';",
    "          if ($p->{yychar} <= $#yyname) ",
    "            { $p->{yys} = $yyname[$p->{yychar}]; }",
    "          if (!$p->{yys}) { $p->{yys} = 'illegal-symbol'; }",
    "          warn(\"yydebug: state $YYFINAL, reading \" . ",
    "               $p->{yychar} . \" (\" . $p->{yys} . \")\\n\");",
    "        }",
    "      }",
    "      return ($p->{yyvs}->[1]) if $p->{yychar} == 0;",
    "      next yyloop;",
    "    }",
    "    if (($p->{yyn} = $yygindex[$p->{yym}]) && ",
    "        ($p->{yyn} += $p->{yystate}) >= 0 && ",
    "        $p->{yyn} <= $#yycheck && ",
    "        $yycheck[$p->{yyn}] == $p->{yystate})",
    "    {",
    "        $p->{yystate} = $yytable[$p->{yyn}];",
    "    } else {",
    "        $p->{yystate} = $yydgoto[$p->{yym}];",
    "    }",
    "    warn(\"yydebug: after reduction, shifting from state \" . ",
    "        $p->{yyss}->[$p->{yyssp}] . \" to state \" . ",
    "        $p->{yystate} . \"\\n\") if $p->{yydebug};",
    "    $p->{yyss}[++$p->{yyssp}] = $p->{yystate};",
    "    $p->{yyvs}[++$p->{yyvsp}] = $p->{yyval};",
    "  } # yyloop",
    "} # yyparse",
    (char *) NULL
};

char **trailer[] = { c_trailer, perl_trailer };

#if __STDC__
static char *add_perl_package(char *old_str, char *new_str)
#else
static char *add_perl_package(old_str, new_str)
char *old_str;
char *new_str;
#endif
{
    register char *from = old_str;
    register char *to = new_str;
    register char *p;

    while (*from) {
	if (*from == 'P' && *(from + 1) == 'P') {
	    from += 2;
	    p = perl5005flag ? perl_package : "";
	    while (*to++ = *p++)
	        /* void */ ;
	    to--;
	}
	else {
	    *to++ = *from++;
	}
    }

    *to = *from;
    
    return new_str;
}

#if __STDC__
static char *add_prefixes(char *old_str, char *new_str)
#else
static char *add_prefixes(old_str, new_str)
char *old_str;
char *new_str;
#endif
{
    register char *from = old_str;
    register char *to = new_str;
    register char *p;

    while (*from) {
	if (*from == 'Y' && *(from + 1) == 'Y') {
	    from += 2;
	    p = define_prefix;
	    while (*to++ = *p++)
		/* void */ ;
	    to--;
	}
	else if (*from == 'y' && *(from + 1) == 'y') {
	    from += 2;
	    p = symbol_prefix;
	    while (*to++ = *p++)
		/* void */ ;
	    to--;
	}
	else {
	    *to++ = *from++;
	}
    }

    *to = *from;
    
    return new_str;
}

#if __STDC__
void write_section(char **section[])
#else
void write_section(section)
char **section[];
#endif
{
    register int i;
    register FILE *fp;
    register char **sec = section[(int) language];

    if (sec != (char **) NULL)
    {
	fp = code_file;
	if (language == PERL)
	{
	    char buf[BUFSIZ];

	    for (i = 0; sec[i]; ++i)
	    {
		++outline;
		fprintf(fp, "%s\n", add_perl_package(sec[i], buf));
	    }
	}
	else if (prefix_changed)
	{
	    char buf[BUFSIZ];

	    for (i = 0; sec[i]; ++i)
	    {
		++outline;
		fprintf(fp, "%s\n", add_prefixes(sec[i], buf));
	    }
	}
	else
	{
	    for (i = 0; sec[i]; ++i)
	    {
		++outline;
		fprintf(fp, "%s\n", sec[i]);
	    }
	}
    }
}