blob: bb8e317f3bdbef3cd8bdf2375763382a7a305218 [file] [log] [blame]
Brian Kernighan87b94932012-12-22 10:35:39 -05001/****************************************************************
2Copyright (C) Lucent Technologies 1997
3All Rights Reserved
4
5Permission to use, copy, modify, and distribute this software and
6its documentation for any purpose and without fee is hereby
7granted, provided that the above copyright notice appear in all
8copies and that both that the copyright notice and this
9permission notice and warranty disclaimer appear in supporting
10documentation, and that the name Lucent Technologies or any of
11its entities not be used in advertising or publicity pertaining
12to distribution of the software without specific, written prior
13permission.
14
15LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22THIS SOFTWARE.
23****************************************************************/
24
25/*
26 * this program makes the table to link function names
27 * and type indices that is used by execute() in run.c.
28 * it finds the indices in ytab.h, produced by yacc.
29 */
30
31#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34#include "awk.h"
35#include "ytab.h"
36
37struct xx
38{ int token;
39 const char *name;
40 const char *pname;
41} proc[] = {
42 { PROGRAM, "program", NULL },
43 { BOR, "boolop", " || " },
44 { AND, "boolop", " && " },
45 { NOT, "boolop", " !" },
46 { NE, "relop", " != " },
47 { EQ, "relop", " == " },
48 { LE, "relop", " <= " },
49 { LT, "relop", " < " },
50 { GE, "relop", " >= " },
51 { GT, "relop", " > " },
52 { ARRAY, "array", NULL },
53 { INDIRECT, "indirect", "$(" },
54 { SUBSTR, "substr", "substr" },
55 { SUB, "sub", "sub" },
56 { GSUB, "gsub", "gsub" },
57 { INDEX, "sindex", "sindex" },
58 { SPRINTF, "awksprintf", "sprintf " },
59 { ADD, "arith", " + " },
60 { MINUS, "arith", " - " },
61 { MULT, "arith", " * " },
62 { DIVIDE, "arith", " / " },
63 { MOD, "arith", " % " },
64 { UMINUS, "arith", " -" },
Arnold D. Robbins32093f52018-08-22 20:40:26 +030065 { UPLUS, "arith", " +" },
Brian Kernighan87b94932012-12-22 10:35:39 -050066 { POWER, "arith", " **" },
67 { PREINCR, "incrdecr", "++" },
68 { POSTINCR, "incrdecr", "++" },
69 { PREDECR, "incrdecr", "--" },
70 { POSTDECR, "incrdecr", "--" },
71 { CAT, "cat", " " },
72 { PASTAT, "pastat", NULL },
73 { PASTAT2, "dopa2", NULL },
74 { MATCH, "matchop", " ~ " },
75 { NOTMATCH, "matchop", " !~ " },
76 { MATCHFCN, "matchop", "matchop" },
77 { INTEST, "intest", "intest" },
78 { PRINTF, "awkprintf", "printf" },
79 { PRINT, "printstat", "print" },
80 { CLOSE, "closefile", "closefile" },
81 { DELETE, "awkdelete", "awkdelete" },
82 { SPLIT, "split", "split" },
83 { ASSIGN, "assign", " = " },
84 { ADDEQ, "assign", " += " },
85 { SUBEQ, "assign", " -= " },
86 { MULTEQ, "assign", " *= " },
87 { DIVEQ, "assign", " /= " },
88 { MODEQ, "assign", " %= " },
89 { POWEQ, "assign", " ^= " },
90 { CONDEXPR, "condexpr", " ?: " },
91 { IF, "ifstat", "if(" },
92 { WHILE, "whilestat", "while(" },
93 { FOR, "forstat", "for(" },
94 { DO, "dostat", "do" },
95 { IN, "instat", "instat" },
96 { NEXT, "jump", "next" },
97 { NEXTFILE, "jump", "nextfile" },
98 { EXIT, "jump", "exit" },
99 { BREAK, "jump", "break" },
100 { CONTINUE, "jump", "continue" },
101 { RETURN, "jump", "ret" },
102 { BLTIN, "bltin", "bltin" },
103 { CALL, "call", "call" },
104 { ARG, "arg", "arg" },
105 { VARNF, "getnf", "NF" },
106 { GETLINE, "awkgetline", "getline" },
107 { 0, "", "" },
108};
109
110#define SIZE (LASTTOKEN - FIRSTTOKEN + 1)
111const char *table[SIZE];
112char *names[SIZE];
113
114int main(int argc, char *argv[])
115{
116 const struct xx *p;
117 int i, n, tok;
118 char c;
119 FILE *fp;
120 char buf[200], name[200], def[200];
121
122 printf("#include <stdio.h>\n");
123 printf("#include \"awk.h\"\n");
124 printf("#include \"ytab.h\"\n\n");
125 for (i = SIZE; --i >= 0; )
126 names[i] = "";
127
128 if ((fp = fopen("ytab.h", "r")) == NULL) {
129 fprintf(stderr, "maketab can't open ytab.h!\n");
130 exit(1);
131 }
132 printf("static char *printname[%d] = {\n", SIZE);
133 i = 0;
134 while (fgets(buf, sizeof buf, fp) != NULL) {
135 n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
136 if (c != '#' || (n != 4 && strcmp(def,"define") != 0)) /* not a valid #define */
137 continue;
Brian Kernighane8c28002018-10-25 13:28:54 -0400138 if (strcmp(name, "YYSTYPE_IS_DECLARED") == 0)
139 continue;
Brian Kernighan87b94932012-12-22 10:35:39 -0500140 if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
141 /* fprintf(stderr, "maketab funny token %d %s ignored\n", tok, buf); */
142 continue;
143 }
144 names[tok-FIRSTTOKEN] = (char *) malloc(strlen(name)+1);
145 strcpy(names[tok-FIRSTTOKEN], name);
146 printf("\t(char *) \"%s\",\t/* %d */\n", name, tok);
147 i++;
148 }
149 printf("};\n\n");
150
151 for (p=proc; p->token!=0; p++)
152 table[p->token-FIRSTTOKEN] = p->name;
153 printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
154 for (i=0; i<SIZE; i++)
155 if (table[i]==0)
156 printf("\tnullproc,\t/* %s */\n", names[i]);
157 else
158 printf("\t%s,\t/* %s */\n", table[i], names[i]);
159 printf("};\n\n");
160
161 printf("char *tokname(int n)\n"); /* print a tokname() function */
162 printf("{\n");
163 printf(" static char buf[100];\n\n");
164 printf(" if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
165 printf(" sprintf(buf, \"token %%d\", n);\n");
166 printf(" return buf;\n");
167 printf(" }\n");
168 printf(" return printname[n-FIRSTTOKEN];\n");
169 printf("}\n");
170 return 0;
171}