Annotation of coherent/d/bin/yacc/y4.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.