maketab.c 5.0 KB

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