1/**************************************************************** 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#if HAVE_NBTOOL_CONFIG_H 26#include "nbtool_config.h" 27#endif 28 29/* 30 * this program makes the table to link function names 31 * and type indices that is used by execute() in run.c. 32 * it finds the indices in ytab.h, produced by yacc. 33 */ 34 35#include <stdio.h> 36#include <string.h> 37#include <stdlib.h> 38#include "awk.h" 39#include "awkgram.h" 40 41struct xx 42{ int token; 43 const char *name; 44 const char *pname; 45} proc[] = { 46 { PROGRAM, "program", NULL }, 47 { BOR, "boolop", " || " }, 48 { AND, "boolop", " && " }, 49 { NOT, "boolop", " !" }, 50 { NE, "relop", " != " }, 51 { EQ, "relop", " == " }, 52 { LE, "relop", " <= " }, 53 { LT, "relop", " < " }, 54 { GE, "relop", " >= " }, 55 { GT, "relop", " > " }, 56 { ARRAY, "array", NULL }, 57 { INDIRECT, "indirect", "$(" }, 58 { SUBSTR, "substr", "substr" }, 59 { SUB, "sub", "sub" }, 60 { GSUB, "gsub", "gsub" }, 61 { INDEX, "sindex", "sindex" }, 62 { SPRINTF, "awksprintf", "sprintf " }, 63 { ADD, "arith", " + " }, 64 { MINUS, "arith", " - " }, 65 { MULT, "arith", " * " }, 66 { DIVIDE, "arith", " / " }, 67 { MOD, "arith", " % " }, 68 { UMINUS, "arith", " -" }, 69 { UPLUS, "arith", " +" }, 70 { POWER, "arith", " **" }, 71 { PREINCR, "incrdecr", "++" }, 72 { POSTINCR, "incrdecr", "++" }, 73 { PREDECR, "incrdecr", "--" }, 74 { POSTDECR, "incrdecr", "--" }, 75 { CAT, "cat", " " }, 76 { PASTAT, "pastat", NULL }, 77 { PASTAT2, "dopa2", NULL }, 78 { MATCH, "matchop", " ~ " }, 79 { NOTMATCH, "matchop", " !~ " }, 80 { MATCHFCN, "matchop", "matchop" }, 81 { INTEST, "intest", "intest" }, 82 { PRINTF, "awkprintf", "printf" }, 83 { PRINT, "printstat", "print" }, 84 { CLOSE, "closefile", "closefile" }, 85 { DELETE, "awkdelete", "awkdelete" }, 86 { SPLIT, "split", "split" }, 87 { ASSIGN, "assign", " = " }, 88 { ADDEQ, "assign", " += " }, 89 { SUBEQ, "assign", " -= " }, 90 { MULTEQ, "assign", " *= " }, 91 { DIVEQ, "assign", " /= " }, 92 { MODEQ, "assign", " %= " }, 93 { POWEQ, "assign", " ^= " }, 94 { CONDEXPR, "condexpr", " ?: " }, 95 { IF, "ifstat", "if(" }, 96 { WHILE, "whilestat", "while(" }, 97 { FOR, "forstat", "for(" }, 98 { DO, "dostat", "do" }, 99 { IN, "instat", "instat" }, 100 { NEXT, "jump", "next" }, 101 { NEXTFILE, "jump", "nextfile" }, 102 { EXIT, "jump", "exit" }, 103 { BREAK, "jump", "break" }, 104 { CONTINUE, "jump", "continue" }, 105 { RETURN, "jump", "ret" }, 106 { BLTIN, "bltin", "bltin" }, 107 { CALL, "call", "call" }, 108 { ARG, "arg", "arg" }, 109 { VARNF, "getnf", "NF" }, 110 { GETLINE, "awkgetline", "getline" }, 111 { GENSUB, "gensub", "gensub" }, 112 { 0, "", "" }, 113}; 114 115#define SIZE (LASTTOKEN - FIRSTTOKEN + 1) 116const char *table[SIZE]; 117char *names[SIZE]; 118 119int main(int argc, char *argv[]) 120{ 121 const struct xx *p; 122 int i, n, tok; 123 char c; 124 FILE *fp; 125 char buf[200], name[200], def[200]; 126 enum { TOK_UNKNOWN, TOK_ENUM, TOK_DEFINE } tokentype = TOK_UNKNOWN; 127 128 printf("#include <stdio.h>\n"); 129 printf("#include \"awk.h\"\n"); 130 printf("#include \"ytab.h\"\n\n"); 131 132 if (argc != 2) { 133 fprintf(stderr, "usage: maketab YTAB_H\n"); 134 exit(1); 135 } 136 if ((fp = fopen(argv[1], "r")) == NULL) { 137 fprintf(stderr, "maketab can't open %s!\n", argv[1]); 138 exit(1); 139 } 140 printf("static const char * const printname[%d] = {\n", SIZE); 141 i = 0; 142 while (fgets(buf, sizeof buf, fp) != NULL) { 143 // 199 is sizeof(def) - 1 144 if (tokentype != TOK_ENUM) { 145 n = sscanf(buf, "%1c %199s %199s %d", &c, def, name, 146 &tok); 147 if (n == 4 && c == '#' && strcmp(def, "define") == 0) { 148 tokentype = TOK_DEFINE; 149 } else if (tokentype != TOK_UNKNOWN) { 150 continue; 151 } 152 } 153 if (tokentype != TOK_DEFINE) { 154 /* not a valid #define, bison uses enums now */ 155 n = sscanf(buf, "%199s = %d,\n", name, &tok); 156 if (n != 2) 157 continue; 158 tokentype = TOK_ENUM; 159 } 160 if (strcmp(name, "YYSTYPE_IS_DECLARED") == 0) { 161 tokentype = TOK_UNKNOWN; 162 continue; 163 } 164 if (tok < FIRSTTOKEN || tok > LASTTOKEN) { 165 tokentype = TOK_UNKNOWN; 166 /* fprintf(stderr, "maketab funny token %d %s ignored\n", tok, buf); */ 167 continue; 168 } 169 names[tok-FIRSTTOKEN] = strdup(name); 170 if (names[tok-FIRSTTOKEN] == NULL) { 171 fprintf(stderr, "maketab out of space copying %s", name); 172 continue; 173 } 174 printf("\t\"%s\",\t/* %d */\n", name, tok); 175 i++; 176 } 177 printf("};\n\n"); 178 179 for (p=proc; p->token!=0; p++) 180 table[p->token-FIRSTTOKEN] = p->name; 181 printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE); 182 for (i=0; i<SIZE; i++) 183 printf("\t%s,\t/* %s */\n", 184 table[i] ? table[i] : "nullproc", names[i] ? names[i] : ""); 185 printf("};\n\n"); 186 187 printf("const char *tokname(int n)\n"); /* print a tokname() function */ 188 printf("{\n"); 189 printf("\tstatic char buf[100];\n\n"); 190 printf("\tif (n < FIRSTTOKEN || n > LASTTOKEN) {\n"); 191 printf("\t\tsnprintf(buf, sizeof(buf), \"token %%d\", n);\n"); 192 printf("\t\treturn buf;\n"); 193 printf("\t}\n"); 194 printf("\treturn printname[n-FIRSTTOKEN];\n"); 195 printf("}\n"); 196 return 0; 197} 198