[BACK]Return to skeleton.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / yacc

Annotation of src/usr.bin/yacc/skeleton.c, Revision 1.15

1.15    ! deraadt     1: /*     $OpenBSD: skeleton.c,v 1.14 2000/01/26 22:37:24 deraadt Exp $   */
1.4       deraadt     2: /*     $NetBSD: skeleton.c,v 1.10 1996/03/25 00:36:18 mrg 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. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *     This product includes software developed by the University of
                     22:  *     California, Berkeley and its contributors.
                     23:  * 4. Neither the name of the University nor the names of its contributors
                     24:  *    may be used to endorse or promote products derived from this software
                     25:  *    without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     28:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     29:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     30:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     31:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     32:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     33:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     34:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     35:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     36:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     37:  * SUCH DAMAGE.
                     38:  */
                     39:
                     40: #ifndef lint
                     41: #if 0
                     42: static char sccsid[] = "@(#)skeleton.c 5.8 (Berkeley) 4/29/95";
                     43: #else
                     44: static char rcsid[] = "$NetBSD: skeleton.c,v 1.10 1996/03/25 00:36:18 mrg Exp $";
                     45: #endif
                     46: #endif /* not lint */
1.3       niklas     47:
1.1       deraadt    48: #include "defs.h"
                     49:
                     50: /*  The definition of yysccsid in the banner should be replaced with   */
                     51: /*  a #pragma ident directive if the target C compiler supports                */
                     52: /*  #pragma ident directives.                                          */
                     53: /*                                                                     */
                     54: /*  If the skeleton is changed, the banner should be changed so that   */
                     55: /*  the altered version can be easily distinguished from the original. */
                     56: /*                                                                     */
                     57: /*  The #defines included with the banner are there because they are   */
                     58: /*  useful in subsequent code.  The macros #defined in the header or   */
                     59: /*  the body either are not useful outside of semantic actions or      */
                     60: /*  are conditional.                                                   */
                     61:
                     62: char *banner[] =
                     63: {
                     64:     "#ifndef lint",
                     65:     "/*static char yysccsid[] = \"from: @(#)yaccpar    1.9 (Berkeley) 02/21/93\";*/",
1.13      dm         66:     "static char yyrcsid[]",
                     67:     "#if __GNUC__ == 2",
                     68:     "  __attribute__ ((unused))",
                     69:     "#endif /* __GNUC__ == 2 */",
1.15    ! deraadt    70:     "  = \"$OpenBSD: skeleton.c,v 1.14 2000/01/26 22:37:24 deraadt Exp $\";",
1.1       deraadt    71:     "#endif",
1.4       deraadt    72:     "#include <stdlib.h>",
1.1       deraadt    73:     "#define YYBYACC 1",
                     74:     "#define YYMAJOR 1",
                     75:     "#define YYMINOR 9",
1.2       niklas     76:     "#define YYLEX yylex()",
                     77:     "#define YYEMPTY -1",
                     78:     "#define yyclearin (yychar=(YYEMPTY))",
1.1       deraadt    79:     "#define yyerrok (yyerrflag=0)",
1.8       tholo      80:     "#define YYRECOVERING() (yyerrflag!=0)",
1.1       deraadt    81:     0
                     82: };
                     83:
                     84:
                     85: char *tables[] =
                     86: {
                     87:     "extern short yylhs[];",
                     88:     "extern short yylen[];",
                     89:     "extern short yydefred[];",
                     90:     "extern short yydgoto[];",
                     91:     "extern short yysindex[];",
                     92:     "extern short yyrindex[];",
                     93:     "extern short yygindex[];",
                     94:     "extern short yytable[];",
                     95:     "extern short yycheck[];",
                     96:     "#if YYDEBUG",
                     97:     "extern char *yyname[];",
                     98:     "extern char *yyrule[];",
                     99:     "#endif",
                    100:     0
                    101: };
                    102:
                    103:
                    104: char *header[] =
                    105: {
                    106:     "#ifdef YYSTACKSIZE",
                    107:     "#undef YYMAXDEPTH",
                    108:     "#define YYMAXDEPTH YYSTACKSIZE",
                    109:     "#else",
                    110:     "#ifdef YYMAXDEPTH",
                    111:     "#define YYSTACKSIZE YYMAXDEPTH",
                    112:     "#else",
1.4       deraadt   113:     "#define YYSTACKSIZE 10000",
                    114:     "#define YYMAXDEPTH 10000",
1.1       deraadt   115:     "#endif",
                    116:     "#endif",
1.4       deraadt   117:     "#define YYINITSTACKSIZE 200",
1.1       deraadt   118:     "int yydebug;",
                    119:     "int yynerrs;",
                    120:     "int yyerrflag;",
                    121:     "int yychar;",
                    122:     "short *yyssp;",
                    123:     "YYSTYPE *yyvsp;",
                    124:     "YYSTYPE yyval;",
                    125:     "YYSTYPE yylval;",
1.4       deraadt   126:     "short *yyss;",
                    127:     "short *yysslim;",
                    128:     "YYSTYPE *yyvs;",
                    129:     "int yystacksize;",
1.1       deraadt   130:     0
                    131: };
                    132:
                    133:
                    134: char *body[] =
                    135: {
1.4       deraadt   136:     "/* allocate initial stack or double stack size, up to YYMAXDEPTH */",
1.5       tholo     137:     "#if defined(__cplusplus) || __STDC__",
                    138:     "static int yygrowstack(void)",
                    139:     "#else",
1.4       deraadt   140:     "static int yygrowstack()",
1.5       tholo     141:     "#endif",
1.4       deraadt   142:     "{",
                    143:     "    int newsize, i;",
                    144:     "    short *newss;",
                    145:     "    YYSTYPE *newvs;",
                    146:     "",
                    147:     "    if ((newsize = yystacksize) == 0)",
                    148:     "        newsize = YYINITSTACKSIZE;",
                    149:     "    else if (newsize >= YYMAXDEPTH)",
                    150:     "        return -1;",
                    151:     "    else if ((newsize *= 2) > YYMAXDEPTH)",
                    152:     "        newsize = YYMAXDEPTH;",
                    153:     "    i = yyssp - yyss;",
1.10      millert   154:     "    newss = yyss ? (short *)realloc(yyss, newsize * sizeof *newss) :",
                    155:     "      (short *)malloc(newsize * sizeof *newss);",
                    156:     "    if (newss == NULL)",
1.14      deraadt   157:     "        goto bail;",
1.4       deraadt   158:     "    yyss = newss;",
                    159:     "    yyssp = newss + i;",
1.10      millert   160:     "    newvs = yyvs ? (YYSTYPE *)realloc(yyvs, newsize * sizeof *newvs) :",
                    161:     "      (YYSTYPE *)malloc(newsize * sizeof *newvs);",
                    162:     "    if (newvs == NULL)",
1.14      deraadt   163:     "        goto bail;",
1.4       deraadt   164:     "    yyvs = newvs;",
                    165:     "    yyvsp = newvs + i;",
                    166:     "    yystacksize = newsize;",
                    167:     "    yysslim = yyss + newsize - 1;",
                    168:     "    return 0;",
1.14      deraadt   169:     "bail:",
                    170:     "    if (yyss)",
                    171:     "            free(yyss);",
                    172:     "    if (yyvs)",
                    173:     "            free(yyvs);",
1.15    ! deraadt   174:     "    yyss = yyssp = NULL;",
        !           175:     "    yyvs = yyvsp = NULL;",
1.14      deraadt   176:     "    yystacksize = 0;",
                    177:     "    return -1;",
1.4       deraadt   178:     "}",
                    179:     "",
1.1       deraadt   180:     "#define YYABORT goto yyabort",
                    181:     "#define YYREJECT goto yyabort",
                    182:     "#define YYACCEPT goto yyaccept",
                    183:     "#define YYERROR goto yyerrlab",
                    184:     "int",
1.5       tholo     185:     "#if defined(__cplusplus) || __STDC__",
                    186:     "yyparse(void)",
                    187:     "#else",
1.1       deraadt   188:     "yyparse()",
1.5       tholo     189:     "#endif",
1.1       deraadt   190:     "{",
                    191:     "    register int yym, yyn, yystate;",
                    192:     "#if YYDEBUG",
                    193:     "    register char *yys;",
                    194:     "",
1.6       deraadt   195:     "    if ((yys = getenv(\"YYDEBUG\")))",
1.1       deraadt   196:     "    {",
                    197:     "        yyn = *yys;",
                    198:     "        if (yyn >= '0' && yyn <= '9')",
                    199:     "            yydebug = yyn - '0';",
                    200:     "    }",
                    201:     "#endif",
                    202:     "",
                    203:     "    yynerrs = 0;",
                    204:     "    yyerrflag = 0;",
                    205:     "    yychar = (-1);",
                    206:     "",
1.4       deraadt   207:     "    if (yyss == NULL && yygrowstack()) goto yyoverflow;",
1.1       deraadt   208:     "    yyssp = yyss;",
                    209:     "    yyvsp = yyvs;",
                    210:     "    *yyssp = yystate = 0;",
                    211:     "",
                    212:     "yyloop:",
                    213:     "    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;",
                    214:     "    if (yychar < 0)",
                    215:     "    {",
                    216:     "        if ((yychar = yylex()) < 0) yychar = 0;",
                    217:     "#if YYDEBUG",
                    218:     "        if (yydebug)",
                    219:     "        {",
                    220:     "            yys = 0;",
                    221:     "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
                    222:     "            if (!yys) yys = \"illegal-symbol\";",
                    223:     "            printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
                    224:     "                    YYPREFIX, yystate, yychar, yys);",
                    225:     "        }",
                    226:     "#endif",
                    227:     "    }",
                    228:     "    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&",
                    229:     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
                    230:     "    {",
                    231:     "#if YYDEBUG",
                    232:     "        if (yydebug)",
                    233:     "            printf(\"%sdebug: state %d, shifting to state %d\\n\",",
                    234:     "                    YYPREFIX, yystate, yytable[yyn]);",
                    235:     "#endif",
1.4       deraadt   236:     "        if (yyssp >= yysslim && yygrowstack())",
1.1       deraadt   237:     "        {",
                    238:     "            goto yyoverflow;",
                    239:     "        }",
                    240:     "        *++yyssp = yystate = yytable[yyn];",
                    241:     "        *++yyvsp = yylval;",
                    242:     "        yychar = (-1);",
                    243:     "        if (yyerrflag > 0)  --yyerrflag;",
                    244:     "        goto yyloop;",
                    245:     "    }",
                    246:     "    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&",
                    247:     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
                    248:     "    {",
                    249:     "        yyn = yytable[yyn];",
                    250:     "        goto yyreduce;",
                    251:     "    }",
                    252:     "    if (yyerrflag) goto yyinrecovery;",
1.7       tholo     253:     "#if defined(lint) || defined(__GNUC__)",
1.4       deraadt   254:     "    goto yynewerror;",
                    255:     "#endif",
                    256:     "yynewerror:",
1.1       deraadt   257:     "    yyerror(\"syntax error\");",
1.7       tholo     258:     "#if defined(lint) || defined(__GNUC__)",
1.1       deraadt   259:     "    goto yyerrlab;",
                    260:     "#endif",
                    261:     "yyerrlab:",
                    262:     "    ++yynerrs;",
                    263:     "yyinrecovery:",
                    264:     "    if (yyerrflag < 3)",
                    265:     "    {",
                    266:     "        yyerrflag = 3;",
                    267:     "        for (;;)",
                    268:     "        {",
                    269:     "            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&",
                    270:     "                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)",
                    271:     "            {",
                    272:     "#if YYDEBUG",
                    273:     "                if (yydebug)",
                    274:     "                    printf(\"%sdebug: state %d, error recovery shifting\\",
                    275:     " to state %d\\n\", YYPREFIX, *yyssp, yytable[yyn]);",
                    276:     "#endif",
1.4       deraadt   277:     "                if (yyssp >= yysslim && yygrowstack())",
1.1       deraadt   278:     "                {",
                    279:     "                    goto yyoverflow;",
                    280:     "                }",
                    281:     "                *++yyssp = yystate = yytable[yyn];",
                    282:     "                *++yyvsp = yylval;",
                    283:     "                goto yyloop;",
                    284:     "            }",
                    285:     "            else",
                    286:     "            {",
                    287:     "#if YYDEBUG",
                    288:     "                if (yydebug)",
                    289:     "                    printf(\"%sdebug: error recovery discarding state %d\
                    290: \\n\",",
                    291:     "                            YYPREFIX, *yyssp);",
                    292:     "#endif",
                    293:     "                if (yyssp <= yyss) goto yyabort;",
                    294:     "                --yyssp;",
                    295:     "                --yyvsp;",
                    296:     "            }",
                    297:     "        }",
                    298:     "    }",
                    299:     "    else",
                    300:     "    {",
                    301:     "        if (yychar == 0) goto yyabort;",
                    302:     "#if YYDEBUG",
                    303:     "        if (yydebug)",
                    304:     "        {",
                    305:     "            yys = 0;",
                    306:     "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
                    307:     "            if (!yys) yys = \"illegal-symbol\";",
                    308:     "            printf(\"%sdebug: state %d, error recovery discards token %d\
                    309:  (%s)\\n\",",
                    310:     "                    YYPREFIX, yystate, yychar, yys);",
                    311:     "        }",
                    312:     "#endif",
                    313:     "        yychar = (-1);",
                    314:     "        goto yyloop;",
                    315:     "    }",
                    316:     "yyreduce:",
                    317:     "#if YYDEBUG",
                    318:     "    if (yydebug)",
                    319:     "        printf(\"%sdebug: state %d, reducing by rule %d (%s)\\n\",",
                    320:     "                YYPREFIX, yystate, yyn, yyrule[yyn]);",
                    321:     "#endif",
                    322:     "    yym = yylen[yyn];",
                    323:     "    yyval = yyvsp[1-yym];",
                    324:     "    switch (yyn)",
                    325:     "    {",
                    326:     0
                    327: };
                    328:
                    329:
                    330: char *trailer[] =
                    331: {
                    332:     "    }",
                    333:     "    yyssp -= yym;",
                    334:     "    yystate = *yyssp;",
                    335:     "    yyvsp -= yym;",
                    336:     "    yym = yylhs[yyn];",
                    337:     "    if (yystate == 0 && yym == 0)",
                    338:     "    {",
                    339:     "#if YYDEBUG",
                    340:     "        if (yydebug)",
                    341:     "            printf(\"%sdebug: after reduction, shifting from state 0 to\\",
                    342:     " state %d\\n\", YYPREFIX, YYFINAL);",
                    343:     "#endif",
                    344:     "        yystate = YYFINAL;",
                    345:     "        *++yyssp = YYFINAL;",
                    346:     "        *++yyvsp = yyval;",
                    347:     "        if (yychar < 0)",
                    348:     "        {",
                    349:     "            if ((yychar = yylex()) < 0) yychar = 0;",
                    350:     "#if YYDEBUG",
                    351:     "            if (yydebug)",
                    352:     "            {",
                    353:     "                yys = 0;",
                    354:     "                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
                    355:     "                if (!yys) yys = \"illegal-symbol\";",
                    356:     "                printf(\"%sdebug: state %d, reading %d (%s)\\n\",",
                    357:     "                        YYPREFIX, YYFINAL, yychar, yys);",
                    358:     "            }",
                    359:     "#endif",
                    360:     "        }",
                    361:     "        if (yychar == 0) goto yyaccept;",
                    362:     "        goto yyloop;",
                    363:     "    }",
                    364:     "    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&",
                    365:     "            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)",
                    366:     "        yystate = yytable[yyn];",
                    367:     "    else",
                    368:     "        yystate = yydgoto[yym];",
                    369:     "#if YYDEBUG",
                    370:     "    if (yydebug)",
                    371:     "        printf(\"%sdebug: after reduction, shifting from state %d \\",
                    372:     "to state %d\\n\", YYPREFIX, *yyssp, yystate);",
                    373:     "#endif",
1.4       deraadt   374:     "    if (yyssp >= yysslim && yygrowstack())",
1.1       deraadt   375:     "    {",
                    376:     "        goto yyoverflow;",
                    377:     "    }",
                    378:     "    *++yyssp = yystate;",
                    379:     "    *++yyvsp = yyval;",
                    380:     "    goto yyloop;",
                    381:     "yyoverflow:",
                    382:     "    yyerror(\"yacc stack overflow\");",
                    383:     "yyabort:",
                    384:     "    return (1);",
                    385:     "yyaccept:",
                    386:     "    return (0);",
                    387:     "}",
                    388:     0
                    389: };
                    390:
                    391:
                    392: write_section(section)
                    393: char *section[];
                    394: {
                    395:     register int c;
                    396:     register int i;
                    397:     register char *s;
                    398:     register FILE *f;
                    399:
                    400:     f = code_file;
                    401:     for (i = 0; s = section[i]; ++i)
                    402:     {
                    403:        ++outline;
                    404:        while (c = *s)
                    405:        {
                    406:            putc(c, f);
                    407:            ++s;
                    408:        }
                    409:        putc('\n', f);
                    410:     }
                    411: }