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