xref: /original-bsd/usr.bin/pascal/src/yyprint.c (revision a9157423)
1 /* Copyright (c) 1979 Regents of the University of California */
2 
3 #ifndef lint
4 static	char sccsid[] = "@(#)yyprint.c 1.3 08/19/83";
5 #endif
6 
7 #include "whoami.h"
8 #include "0.h"
9 #include "tree_ty.h"	/* must be included for yy.h */
10 #include "yy.h"
11 
12 char	*tokname();
13 
14 STATIC	short bounce;
15 
16 /*
17  * Printing representation of a
18  * "character" - a lexical token
19  * not in a yytok structure.
20  * 'which' indicates which char * you want
21  * should always be called as "charname(...,0),charname(...,1)"
22  */
23 char *
24 charname(ch , which )
25 	int ch;
26 	int which;
27 {
28 	struct yytok Ych;
29 
30 	Ych.Yychar = ch;
31 	Ych.Yylval = nullsem(ch);
32 	return (tokname(&Ych , which ));
33 }
34 
35 /*
36  * Printing representation of a token
37  * 'which' as above.
38  */
39 char *
40 tokname(tp , which )
41 	register struct yytok *tp;
42 	int	 	      which;
43 {
44 	register char *cp;
45 	register struct kwtab *kp;
46 	char	*cp2;
47 
48 	cp2 = "";
49 	switch (tp->Yychar) {
50 		case YCASELAB:
51 			cp = "case-label";
52 			break;
53 		case YEOF:
54 			cp = "end-of-file";
55 			break;
56 		case YILLCH:
57 			cp = "illegal character";
58 			break;
59 		case 256:
60 			/* error token */
61 			cp = "error";
62 			break;
63 		case YID:
64 			cp = "identifier";
65 			break;
66 		case YNUMB:
67 			cp = "real number";
68 			break;
69 		case YINT:
70 		case YBINT:
71 			cp = "number";
72 			break;
73 		case YSTRING:
74 			cp = (char *) tp->Yylval;
75 			cp = cp == NIL || cp[1] == 0 ? "character" : "string";
76 			break;
77 		case YDOTDOT:
78 			cp = "'..'";
79 			break;
80 		default:
81 			if (tp->Yychar < 256) {
82 				cp = "'x'\0'x'\0'x'\0'x'";
83 				/*
84 				 * for four times reentrant code!
85 				 * used to be:
86 				 * if (bounce = ((bounce + 1) & 1))
87 				 *	cp += 4;
88 				 */
89 				bounce = ( bounce + 1 ) % 4;
90 				cp += (4 * bounce);	/* 'x'\0 is 4 chars */
91 				cp[1] = tp->Yychar;
92 				break;
93 			}
94 			for (kp = yykey; kp->kw_str != NIL && kp->kw_val != tp->Yychar; kp++)
95 				continue;
96 			cp = "keyword ";
97 			cp2 = kp->kw_str;
98 	}
99 	return ( which ? cp2 : cp );
100 }
101