Brian Kernighan | 87b9493 | 2012-12-22 10:35:39 -0500 | [diff] [blame] | 1 | /**************************************************************** |
| 2 | Copyright (C) Lucent Technologies 1997 |
| 3 | All Rights Reserved |
| 4 | |
| 5 | Permission to use, copy, modify, and distribute this software and |
| 6 | its documentation for any purpose and without fee is hereby |
| 7 | granted, provided that the above copyright notice appear in all |
| 8 | copies and that both that the copyright notice and this |
| 9 | permission notice and warranty disclaimer appear in supporting |
| 10 | documentation, and that the name Lucent Technologies or any of |
| 11 | its entities not be used in advertising or publicity pertaining |
| 12 | to distribution of the software without specific, written prior |
| 13 | permission. |
| 14 | |
| 15 | LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 16 | INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. |
| 17 | IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY |
| 18 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER |
| 20 | IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, |
| 21 | ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF |
| 22 | THIS 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. |
Arnold D. Robbins | 07f0438 | 2020-07-30 17:12:45 +0300 | [diff] [blame] | 28 | * it finds the indices in awkgram.tab.h, produced by bison. |
Brian Kernighan | 87b9493 | 2012-12-22 10:35:39 -0500 | [diff] [blame] | 29 | */ |
| 30 | |
| 31 | #include <stdio.h> |
| 32 | #include <string.h> |
| 33 | #include <stdlib.h> |
| 34 | #include "awk.h" |
Arnold D. Robbins | 07f0438 | 2020-07-30 17:12:45 +0300 | [diff] [blame] | 35 | #include "awkgram.tab.h" |
Brian Kernighan | 87b9493 | 2012-12-22 10:35:39 -0500 | [diff] [blame] | 36 | |
| 37 | struct 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. Robbins | 32093f5 | 2018-08-22 20:40:26 +0300 | [diff] [blame] | 65 | { UPLUS, "arith", " +" }, |
Brian Kernighan | 87b9493 | 2012-12-22 10:35:39 -0500 | [diff] [blame] | 66 | { 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) |
| 111 | const char *table[SIZE]; |
| 112 | char *names[SIZE]; |
| 113 | |
| 114 | int 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]; |
zoulasc | 94e4c04 | 2020-02-18 14:20:27 -0500 | [diff] [blame] | 121 | enum { TOK_UNKNOWN, TOK_ENUM, TOK_DEFINE } tokentype = TOK_UNKNOWN; |
Brian Kernighan | 87b9493 | 2012-12-22 10:35:39 -0500 | [diff] [blame] | 122 | |
| 123 | printf("#include <stdio.h>\n"); |
| 124 | printf("#include \"awk.h\"\n"); |
Arnold D. Robbins | 07f0438 | 2020-07-30 17:12:45 +0300 | [diff] [blame] | 125 | printf("#include \"awkgram.tab.h\"\n\n"); |
Brian Kernighan | 87b9493 | 2012-12-22 10:35:39 -0500 | [diff] [blame] | 126 | |
Elliott Hughes | cc165f4 | 2019-01-29 17:20:00 -0800 | [diff] [blame] | 127 | if (argc != 2) { |
| 128 | fprintf(stderr, "usage: maketab YTAB_H\n"); |
| 129 | exit(1); |
| 130 | } |
| 131 | if ((fp = fopen(argv[1], "r")) == NULL) { |
| 132 | fprintf(stderr, "maketab can't open %s!\n", argv[1]); |
Brian Kernighan | 87b9493 | 2012-12-22 10:35:39 -0500 | [diff] [blame] | 133 | exit(1); |
| 134 | } |
zoulasc | 0d8778b | 2019-10-25 10:59:10 -0400 | [diff] [blame] | 135 | printf("static const char * const printname[%d] = {\n", SIZE); |
Brian Kernighan | 87b9493 | 2012-12-22 10:35:39 -0500 | [diff] [blame] | 136 | i = 0; |
| 137 | while (fgets(buf, sizeof buf, fp) != NULL) { |
zoulasc | 0d8778b | 2019-10-25 10:59:10 -0400 | [diff] [blame] | 138 | // 199 is sizeof(def) - 1 |
zoulasc | 94e4c04 | 2020-02-18 14:20:27 -0500 | [diff] [blame] | 139 | if (tokentype != TOK_ENUM) { |
| 140 | n = sscanf(buf, "%1c %199s %199s %d", &c, def, name, |
| 141 | &tok); |
Arnold D. Robbins | ed6ff8c | 2020-02-18 21:26:24 +0200 | [diff] [blame] | 142 | if (n == 4 && c == '#' && strcmp(def, "define") == 0) { |
zoulasc | 94e4c04 | 2020-02-18 14:20:27 -0500 | [diff] [blame] | 143 | tokentype = TOK_DEFINE; |
| 144 | } else if (tokentype != TOK_UNKNOWN) { |
| 145 | continue; |
| 146 | } |
| 147 | } |
| 148 | if (tokentype != TOK_DEFINE) { |
| 149 | /* not a valid #define, bison uses enums now */ |
| 150 | n = sscanf(buf, "%199s = %d,\n", name, &tok); |
| 151 | if (n != 2) |
| 152 | continue; |
| 153 | tokentype = TOK_ENUM; |
| 154 | } |
| 155 | if (strcmp(name, "YYSTYPE_IS_DECLARED") == 0) { |
| 156 | tokentype = TOK_UNKNOWN; |
Brian Kernighan | 87b9493 | 2012-12-22 10:35:39 -0500 | [diff] [blame] | 157 | continue; |
zoulasc | 94e4c04 | 2020-02-18 14:20:27 -0500 | [diff] [blame] | 158 | } |
Brian Kernighan | 87b9493 | 2012-12-22 10:35:39 -0500 | [diff] [blame] | 159 | if (tok < FIRSTTOKEN || tok > LASTTOKEN) { |
zoulasc | 94e4c04 | 2020-02-18 14:20:27 -0500 | [diff] [blame] | 160 | tokentype = TOK_UNKNOWN; |
Brian Kernighan | 87b9493 | 2012-12-22 10:35:39 -0500 | [diff] [blame] | 161 | /* fprintf(stderr, "maketab funny token %d %s ignored\n", tok, buf); */ |
| 162 | continue; |
| 163 | } |
zoulasc | 6589208 | 2019-10-24 09:40:15 -0400 | [diff] [blame] | 164 | names[tok-FIRSTTOKEN] = strdup(name); |
zoulasc | 0d8778b | 2019-10-25 10:59:10 -0400 | [diff] [blame] | 165 | if (names[tok-FIRSTTOKEN] == NULL) { |
| 166 | fprintf(stderr, "maketab out of space copying %s", name); |
| 167 | continue; |
| 168 | } |
| 169 | printf("\t\"%s\",\t/* %d */\n", name, tok); |
Brian Kernighan | 87b9493 | 2012-12-22 10:35:39 -0500 | [diff] [blame] | 170 | i++; |
| 171 | } |
| 172 | printf("};\n\n"); |
| 173 | |
| 174 | for (p=proc; p->token!=0; p++) |
| 175 | table[p->token-FIRSTTOKEN] = p->name; |
| 176 | printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE); |
| 177 | for (i=0; i<SIZE; i++) |
zoulasc | 6a87709 | 2020-01-24 04:11:59 -0500 | [diff] [blame] | 178 | printf("\t%s,\t/* %s */\n", |
| 179 | table[i] ? table[i] : "nullproc", names[i] ? names[i] : ""); |
Brian Kernighan | 87b9493 | 2012-12-22 10:35:39 -0500 | [diff] [blame] | 180 | printf("};\n\n"); |
| 181 | |
zoulasc | 0d8778b | 2019-10-25 10:59:10 -0400 | [diff] [blame] | 182 | printf("const char *tokname(int n)\n"); /* print a tokname() function */ |
Brian Kernighan | 87b9493 | 2012-12-22 10:35:39 -0500 | [diff] [blame] | 183 | printf("{\n"); |
zoulasc | 0d8778b | 2019-10-25 10:59:10 -0400 | [diff] [blame] | 184 | printf("\tstatic char buf[100];\n\n"); |
| 185 | printf("\tif (n < FIRSTTOKEN || n > LASTTOKEN) {\n"); |
| 186 | printf("\t\tsnprintf(buf, sizeof(buf), \"token %%d\", n);\n"); |
| 187 | printf("\t\treturn buf;\n"); |
| 188 | printf("\t}\n"); |
| 189 | printf("\treturn printname[n-FIRSTTOKEN];\n"); |
Brian Kernighan | 87b9493 | 2012-12-22 10:35:39 -0500 | [diff] [blame] | 190 | printf("}\n"); |
| 191 | return 0; |
| 192 | } |