110984Srrh %term xxif 300 xxelse 301 xxwhile 302 xxrept 303 xxdo 304 xxrb 305 xxpred 306
210984Srrh %term xxident 307 xxle 308 xxge 309 xxne 310 xxnum 311 xxcom 312
310984Srrh %term xxstring 313 xxexplist 314 xxidpar 315 xxelseif 316 xxlb 318 xxend 319
410984Srrh %term xxcase 320 xxswitch 321 xxuntil 322 xxdefault 323
510984Srrh %term xxeq 324
610984Srrh
710984Srrh %left '|'
810984Srrh %left '&'
910984Srrh %left '!'
1010984Srrh %binary '<' '>' xxeq xxne xxge xxle
1110984Srrh %left '+' '-'
1210984Srrh %left '*' '/'
1310984Srrh %left xxuminus
1410984Srrh %right '^'
1510984Srrh
1610984Srrh %{
1748120Sbostic /*-
1848120Sbostic * %sccs.include.proprietary.c%
1948120Sbostic */
2048120Sbostic
2110984Srrh #ifndef lint
22*62262Sbostic static char sccsid[] = "@(#)beauty.y 8.1 (Berkeley) 06/06/93";
2348120Sbostic #endif /* not lint */
2410984Srrh
2510984Srrh #include "b.h"
2610984Srrh #include <stdio.h>
2735273Sbostic
2835273Sbostic extern struct node *checkneg(), *addroot();
2910984Srrh %}
3010984Srrh
3110984Srrh %%
3210984Srrh %{
3310984Srrh struct node *t;
3410984Srrh %}
3510984Srrh
3610984Srrh
3710984Srrh allprog: prog xxnew
3810984Srrh ;
3910984Srrh
4010984Srrh prog: stat
4110984Srrh | prog stat
4210984Srrh ;
4310984Srrh
4410984Srrh stat: iftok pred nlevel elsetok nlevel
4510984Srrh | iftok pred nlevel
4610984Srrh | xxtab whtok pred nlevel
4710984Srrh | xxtab rpttok nlevel optuntil
4810984Srrh | xxtab dotok nlevel
4910984Srrh | xxtab swtok oppred pindent lbtok caseseq xxtab rbtok mindent
5010984Srrh | xxtab fstok
5110984Srrh | lbtok prog xxtab rbtok
5210984Srrh | lbtok rbtok
5310984Srrh | labtok stat
5410984Srrh | xxnl comtok stat
5510984Srrh | error
5610984Srrh ;
5710984Srrh
5810984Srrh
5910984Srrh xxtab: = {
6010984Srrh if (!xxlablast) tab(xxindent);
6110984Srrh xxlablast = 0;
6210984Srrh }
6310984Srrh
6410984Srrh xxnl: = newline();
6510984Srrh xxnew: = putout('\n',"\n");
6610984Srrh nlevel: pindent stat mindent;
6710984Srrh pindent: =
6810984Srrh {
6910984Srrh if (xxstack[xxstind] != xxlb)
7010984Srrh ++xxindent;
7110984Srrh };
7210984Srrh mindent: =
7310984Srrh {if (xxstack[xxstind] != xxlb && xxstack[xxstind] != xxelseif)
7410984Srrh --xxindent;
7510984Srrh pop();
7610984Srrh };
7710984Srrh caseseq: casetok caseseq
7810984Srrh | casetok
7910984Srrh ;
8010984Srrh
8110984Srrh casetok: xxtab xxctok predlist pindent prog mindent
8210984Srrh | xxtab xxctok predlist pindent mindent
8310984Srrh | xxtab deftok pindent prog mindent
8410984Srrh | xxnl comtok casetok
8510984Srrh ;
8610984Srrh
8710984Srrh xxctok: xxcase = {putout(xxcase,"case "); free ($1); push(xxcase); }
8810984Srrh
8910984Srrh
9010984Srrh deftok: xxdefault ':' = {
9110984Srrh putout(xxcase,"default");
9210984Srrh free($1);
9310984Srrh putout(':',":");
9410984Srrh free($2);
9510984Srrh push(xxcase);
9610984Srrh }
9710984Srrh swtok: xxswitch = {putout(xxswitch,"switch"); free($1); push(xxswitch); }
9810984Srrh
9910984Srrh fstok: xxend = {
10010984Srrh free($1);
10110984Srrh putout(xxident,"end");
10210984Srrh putout('\n',"\n");
10310984Srrh putout('\n',"\n");
10410984Srrh putout('\n',"\n");
10510984Srrh }
10610984Srrh | xxident = {
10710984Srrh putout(xxident,$1);
10810984Srrh free($1);
10910984Srrh newflag = 1;
11010984Srrh forst();
11110984Srrh newflag = 0;
11210984Srrh };
11310984Srrh
11410984Srrh
11510984Srrh
11610984Srrh identtok: xxident '(' explist ')' = {
11710984Srrh xxt = addroot($1,xxident,0,0);
11810984Srrh $$ = addroot("",xxidpar,xxt,$3);
11910984Srrh }
12010984Srrh
12110984Srrh | xxident = $$ = addroot($1,xxident,0,0);
12210984Srrh ;
12310984Srrh
12410984Srrh predlist: explist ':' = {
12510984Srrh yield($1,0);
12610984Srrh putout(':',":");
12710984Srrh freetree($1);
12810984Srrh }
12910984Srrh explist: expr ',' explist = $$ = addroot($2,xxexplist,checkneg($1,0),$3);
13010984Srrh | expr = $$ = checkneg($1,0);
13110984Srrh ;
13210984Srrh
13310984Srrh
13410984Srrh oppred: pred
13510984Srrh |
13610984Srrh ;
13710984Srrh
13810984Srrh pred: '(' expr ')' = { t = checkneg($2,0);
13910984Srrh yield(t,100); freetree(t); };
14010984Srrh
14110984Srrh expr: '(' expr ')' = $$ = $2;
14210984Srrh | '-' expr %prec xxuminus = $$ = addroot($1,xxuminus,$2,0);
14310984Srrh | '!' expr = $$ = addroot($1,'!',$2,0);
14410984Srrh | expr '+' expr = $$ = addroot($2,'+',$1,$3);
14510984Srrh | expr '-' expr = $$ = addroot($2,'-',$1,$3);
14610984Srrh | expr '*' expr = $$ = addroot($2,'*',$1,$3);
14710984Srrh | expr '/' expr = $$ = addroot($2,'/',$1,$3);
14810984Srrh | expr '^' expr = $$ = addroot($2,'^',$1,$3);
14910984Srrh | expr '|' expr = $$ = addroot($2,'|',$1,$3);
15010984Srrh | expr '&' expr = $$ = addroot($2,'&',$1,$3);
15110984Srrh | expr '>' expr = $$ = addroot($2,'>',$1,$3);
15210984Srrh | expr '<' expr = $$ = addroot($2,'<',$1,$3);
15310984Srrh | expr xxeq expr = $$ = addroot($2,xxeq,$1,$3);
15410984Srrh | expr xxle expr = $$ = addroot($2,xxle,$1,$3);
15510984Srrh | expr xxge expr = $$ = addroot($2,xxge,$1,$3);
15610984Srrh | expr xxne expr = $$ = addroot($2,xxne,$1,$3);
15710984Srrh | identtok = $$ = $1;
15810984Srrh | xxnum = $$ = addroot($1,xxnum,0,0);
15910984Srrh | xxstring = $$ = addroot($1,xxstring,0,0);
16010984Srrh ;
16110984Srrh
16210984Srrh iftok: xxif =
16310984Srrh {
16410984Srrh if (xxstack[xxstind] == xxelse && !xxlablast)
16510984Srrh {
16610984Srrh --xxindent;
16710984Srrh xxstack[xxstind] = xxelseif;
16810984Srrh putout(' '," ");
16910984Srrh }
17010984Srrh else
17110984Srrh {
17210984Srrh if (!xxlablast)
17310984Srrh tab(xxindent);
17410984Srrh xxlablast = 0;
17510984Srrh }
17610984Srrh putout(xxif,"if");
17710984Srrh free($1);
17810984Srrh push(xxif);
17910984Srrh }
18010984Srrh elsetok: xxelse =
18110984Srrh {
18210984Srrh tab(xxindent);
18310984Srrh putout(xxelse,"else");
18410984Srrh free($1);
18510984Srrh push(xxelse);
18610984Srrh }
18710984Srrh whtok: xxwhile = {
18810984Srrh putout(xxwhile,"while");
18910984Srrh free($1);
19010984Srrh push(xxwhile);
19110984Srrh }
19210984Srrh rpttok: xxrept = {
19310984Srrh putout(xxrept,"repeat");
19410984Srrh free($1);
19510984Srrh push(xxrept);
19610984Srrh }
19710984Srrh optuntil: xxtab unttok pred
19810984Srrh |
19910984Srrh ;
20010984Srrh
20110984Srrh unttok: xxuntil = {
20210984Srrh putout('\t',"\t");
20310984Srrh putout(xxuntil,"until");
20410984Srrh free($1);
20510984Srrh }
20610984Srrh dotok: dopart opdotok
20710984Srrh ;
20810984Srrh dopart: xxdo identtok '=' expr ',' expr =
20910984Srrh {push(xxdo);
21010984Srrh putout(xxdo,"do");
21110984Srrh free($1);
21210984Srrh puttree($2);
21310984Srrh putout('=',"=");
21410984Srrh free($3);
21510984Srrh puttree($4);
21610984Srrh putout(',',",");
21710984Srrh free($5);
21810984Srrh puttree($6);
21910984Srrh }
22010984Srrh opdotok: ',' expr = {
22110984Srrh putout(',',",");
22210984Srrh puttree($2);
22310984Srrh }
22410984Srrh | ;
22510984Srrh lbtok: '{' = {
22610984Srrh putout('{'," {");
22710984Srrh push(xxlb);
22810984Srrh }
22910984Srrh rbtok: '}' = { putout('}',"}"); pop(); }
23010984Srrh labtok: xxnum = {
23110984Srrh tab(xxindent);
23210984Srrh putout(xxnum,$1);
23310984Srrh putout(' '," ");
23410984Srrh xxlablast = 1;
23510984Srrh }
23610984Srrh comtok: xxcom = { putout(xxcom,$1); free($1); xxlablast = 0; }
23710984Srrh | comtok xxcom = { putout ('\n',"\n"); putout(xxcom,$2); free($2); xxlablast = 0; };
23810984Srrh %%
23910984Srrh #define ASSERT(X,Y) if (!(X)) error("struct bug: assertion 'X' invalid in routine Y","","");
24010984Srrh
yyerror(s)24110984Srrh yyerror(s)
24210984Srrh char *s;
24310984Srrh {
24410984Srrh extern int yychar;
24510984Srrh fprintf(stderr,"\n%s",s);
24610984Srrh fprintf(stderr," in beautifying, output line %d,",xxlineno + 1);
24710984Srrh fprintf(stderr," on input: ");
24810984Srrh switch (yychar) {
24910984Srrh case '\t': fprintf(stderr,"\\t\n"); return;
25010984Srrh case '\n': fprintf(stderr,"\\n\n"); return;
25110984Srrh case '\0': fprintf(stderr,"$end\n"); return;
25210984Srrh default: fprintf(stderr,"%c\n",yychar); return;
25310984Srrh }
25410984Srrh }
25510984Srrh
yyinit(argc,argv)25610984Srrh yyinit(argc, argv) /* initialize pushdown store */
25710984Srrh int argc;
25810984Srrh char *argv[];
25910984Srrh {
26010984Srrh xxindent = 0;
26110984Srrh xxbpertab = 8;
26210984Srrh xxmaxchars = 120;
26310984Srrh }
26410984Srrh
26510984Srrh
26610984Srrh #include <signal.h>
main()26710984Srrh main()
26810984Srrh {
26910984Srrh int exit();
27010984Srrh if ( signal(SIGINT, SIG_IGN) != SIG_IGN)
27110984Srrh signal(SIGINT, exit);
27210984Srrh yyinit();
27310984Srrh yyparse();
27410984Srrh }
27510984Srrh
27610984Srrh
putout(type,string)27710984Srrh putout(type,string) /* output string with proper indentation */
27810984Srrh int type;
27910984Srrh char *string;
28010984Srrh {
28110984Srrh static int lasttype;
28210984Srrh if ( (lasttype != 0) && (lasttype != '\n') && (lasttype != ' ') && (lasttype != '\t') && (type == xxcom))
28310984Srrh accum("\t");
28410984Srrh else if (lasttype == xxcom && type != '\n')
28510984Srrh tab(xxindent);
28610984Srrh else
28710984Srrh if (lasttype == xxif ||
28810984Srrh lasttype == xxwhile ||
28910984Srrh lasttype == xxdo ||
29010984Srrh type == '=' ||
29110984Srrh lasttype == '=' ||
29210984Srrh (lasttype == xxident && (type == xxident || type == xxnum) ) ||
29310984Srrh (lasttype == xxnum && type == xxnum) )
29410984Srrh accum(" ");
29510984Srrh accum(string);
29610984Srrh lasttype = type;
29710984Srrh }
29810984Srrh
29910984Srrh
accum(token)30010984Srrh accum(token) /* fill output buffer, generate continuation lines */
30110984Srrh char *token;
30210984Srrh {
30310984Srrh static char *buffer;
30410984Srrh static int lstatus,llen,bufind;
30510984Srrh int tstatus,tlen,i;
30610984Srrh
30710984Srrh #define NEW 0
30810984Srrh #define MID 1
30910984Srrh #define CONT 2
31010984Srrh
31110984Srrh if (buffer == 0)
31210984Srrh {
31310984Srrh buffer = malloc(xxmaxchars);
31410984Srrh if (buffer == 0) error("malloc out of space","","");
31510984Srrh }
31610984Srrh tlen = slength(token);
31710984Srrh if (tlen == 0) return;
31810984Srrh for (i = 0; i < tlen; ++i)
31910984Srrh ASSERT(token[i] != '\n' || tlen == 1,accum);
32010984Srrh switch(token[tlen-1])
32110984Srrh {
32210984Srrh case '\n': tstatus = NEW;
32310984Srrh break;
32410984Srrh case '+':
32510984Srrh case '-':
32610984Srrh case '*':
32710984Srrh case ',':
32810984Srrh case '|':
32910984Srrh case '&':
33010984Srrh case '(': tstatus = CONT;
33110984Srrh break;
33210984Srrh default: tstatus = MID;
33310984Srrh }
33410984Srrh if (llen + bufind + tlen > xxmaxchars && lstatus == CONT && tstatus != NEW)
33510984Srrh {
33610984Srrh putchar('\n');
33710984Srrh ++xxlineno;
33810984Srrh for (i = 0; i < xxindent; ++i)
33910984Srrh putchar('\t');
34010984Srrh putchar(' ');putchar(' ');
34110984Srrh llen = 2 + xxindent * xxbpertab;
34210984Srrh lstatus = NEW;
34310984Srrh }
34410984Srrh if (lstatus == CONT && tstatus == MID)
34510984Srrh { /* store in buffer in case need \n after last CONT char */
34610984Srrh ASSERT(bufind + tlen < xxmaxchars,accum);
34710984Srrh for (i = 0; i < tlen; ++i)
34810984Srrh buffer[bufind++] = token[i];
34910984Srrh }
35010984Srrh else
35110984Srrh {
35210984Srrh for (i = 0; i < bufind; ++i)
35310984Srrh putchar(buffer[i]);
35410984Srrh llen += bufind;
35510984Srrh bufind = 0;
35610984Srrh for (i = 0; i < tlen; ++i)
35710984Srrh putchar(token[i]);
35810984Srrh if (tstatus == NEW) ++xxlineno;
35910984Srrh llen = (tstatus == NEW) ? 0 : llen + tlen;
36010984Srrh lstatus = tstatus;
36110984Srrh }
36210984Srrh }
36310984Srrh
tab(n)36410984Srrh tab(n)
36510984Srrh int n;
36610984Srrh {
36710984Srrh int i;
36810984Srrh newline();
36910984Srrh for ( i = 0; i < n; ++i)
37010984Srrh putout('\t',"\t");
37110984Srrh }
37210984Srrh
newline()37310984Srrh newline()
37410984Srrh {
37510984Srrh static int already;
37610984Srrh if (already)
37710984Srrh putout('\n',"\n");
37810984Srrh else
37910984Srrh already = 1;
38010984Srrh }
38110984Srrh
error(mess1,mess2,mess3)38210984Srrh error(mess1, mess2, mess3)
38310984Srrh char *mess1, *mess2, *mess3;
38410984Srrh {
38510984Srrh fprintf(stderr,"\nerror in beautifying, output line %d: %s %s %s \n",
38610984Srrh xxlineno, mess1, mess2, mess3);
38710984Srrh exit(1);
38810984Srrh }
38910984Srrh
39010984Srrh
39110984Srrh
39210984Srrh
39310984Srrh
39410984Srrh
39510984Srrh
push(type)39610984Srrh push(type)
39710984Srrh int type;
39810984Srrh {
39910984Srrh if (++xxstind > xxtop)
40010984Srrh error("nesting too deep, stack overflow","","");
40110984Srrh xxstack[xxstind] = type;
40210984Srrh }
40310984Srrh
pop()40410984Srrh pop()
40510984Srrh {
40610984Srrh if (xxstind <= 0)
40710984Srrh error("stack exhausted, can't be popped as requested","","");
40810984Srrh --xxstind;
40910984Srrh }
41010984Srrh
41110984Srrh
forst()41210984Srrh forst()
41310984Srrh {
41410984Srrh while( (xxval = yylex()) != '\n')
41510984Srrh {
41610984Srrh putout(xxval, yylval);
41710984Srrh free(yylval);
41810984Srrh }
41910984Srrh free(yylval);
42010984Srrh }
421