PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/usr.bin/awk/maketab.c

https://bitbucket.org/warthurton/gno
C | 170 lines | 131 code | 10 blank | 29 comment | 16 complexity | 28da9d9353099850c273a13350c08c8b MD5 | raw file
Possible License(s): AGPL-1.0
  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. /* $Id: maketab.c 563 1998-04-07 16:19:01Z tribby $ */
  23. /*
  24. * this program makes the table to link function names
  25. * and type indices that is used by execute() in run.c.
  26. * it finds the indices in ytab.h, produced by yacc.
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include "awk.h"
  32. #include "ytab.h"
  33. struct xx
  34. { int token;
  35. char *name;
  36. char *pname;
  37. } proc[] = {
  38. { PROGRAM, "program", NULL },
  39. { BOR, "boolop", " || " },
  40. { AND, "boolop", " && " },
  41. { NOT, "boolop", " !" },
  42. { NE, "relop", " != " },
  43. { EQ, "relop", " == " },
  44. { LE, "relop", " <= " },
  45. { LT, "relop", " < " },
  46. { GE, "relop", " >= " },
  47. { GT, "relop", " > " },
  48. { ARRAY, "array", NULL },
  49. { INDIRECT, "indirect", "$(" },
  50. { SUBSTR, "substr", "substr" },
  51. { SUB, "sub", "sub" },
  52. { GSUB, "gsub", "gsub" },
  53. { INDEX, "sindex", "sindex" },
  54. { SPRINTF, "awksprintf", "sprintf " },
  55. { ADD, "arith", " + " },
  56. { MINUS, "arith", " - " },
  57. { MULT, "arith", " * " },
  58. { DIVIDE, "arith", " / " },
  59. { MOD, "arith", " % " },
  60. { UMINUS, "arith", " -" },
  61. { POWER, "arith", " **" },
  62. { PREINCR, "incrdecr", "++" },
  63. { POSTINCR, "incrdecr", "++" },
  64. { PREDECR, "incrdecr", "--" },
  65. { POSTDECR, "incrdecr", "--" },
  66. { CAT, "cat", " " },
  67. { PASTAT, "pastat", NULL },
  68. { PASTAT2, "dopa2", NULL },
  69. { MATCH, "matchop", " ~ " },
  70. { NOTMATCH, "matchop", " !~ " },
  71. { MATCHFCN, "matchop", "matchop" },
  72. { INTEST, "intest", "intest" },
  73. { PRINTF, "awkprintf", "printf" },
  74. { PRINT, "printstat", "print" },
  75. { CLOSE, "closefile", "closefile" },
  76. { DELETE, "awkdelete", "awkdelete" },
  77. { SPLIT, "split", "split" },
  78. { ASSIGN, "assign", " = " },
  79. { ADDEQ, "assign", " += " },
  80. { SUBEQ, "assign", " -= " },
  81. { MULTEQ, "assign", " *= " },
  82. { DIVEQ, "assign", " /= " },
  83. { MODEQ, "assign", " %= " },
  84. { POWEQ, "assign", " ^= " },
  85. { CONDEXPR, "condexpr", " ?: " },
  86. { IF, "ifstat", "if(" },
  87. { WHILE, "whilestat", "while(" },
  88. { FOR, "forstat", "for(" },
  89. { DO, "dostat", "do" },
  90. { IN, "instat", "instat" },
  91. { NEXT, "jump", "next" },
  92. { NEXTFILE, "jump", "nextfile" },
  93. { EXIT, "jump", "exit" },
  94. { BREAK, "jump", "break" },
  95. { CONTINUE, "jump", "continue" },
  96. { RETURN, "jump", "ret" },
  97. { BLTIN, "bltin", "bltin" },
  98. { CALL, "call", "call" },
  99. { ARG, "arg", "arg" },
  100. { VARNF, "getnf", "NF" },
  101. { GETLINE, "getline", "getline" },
  102. { 0, "", "" },
  103. };
  104. #define SIZE (LASTTOKEN - FIRSTTOKEN + 1)
  105. char *table[SIZE];
  106. char *names[SIZE];
  107. int main(int argc, char *argv[])
  108. {
  109. struct xx *p;
  110. int i, n, tok;
  111. char c;
  112. FILE *fp;
  113. char buf[200], name[200], def[200];
  114. printf("#include <stdio.h>\n");
  115. printf("#include \"awk.h\"\n");
  116. printf("#include \"ytab.h\"\n\n");
  117. for (i = SIZE; --i >= 0; )
  118. names[i] = "";
  119. if ((fp = fopen("ytab.h", "r")) == NULL) {
  120. fprintf(stderr, "maketab can't open ytab.h!\n");
  121. exit(1);
  122. }
  123. printf("static char *printname[%d] = {\n", SIZE);
  124. i = 0;
  125. while (fgets(buf, sizeof buf, fp) != NULL) {
  126. n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
  127. if (c != '#' || (n != 4 && strcmp(def,"define") != 0)) /* not a valid #define */
  128. continue;
  129. if (tok < FIRSTTOKEN || tok > LASTTOKEN) {
  130. fprintf(stderr, "maketab funny token %d %s\n", tok, buf);
  131. exit(1);
  132. }
  133. names[tok-FIRSTTOKEN] = (char *) malloc(strlen(name)+1);
  134. strcpy(names[tok-FIRSTTOKEN], name);
  135. printf("\t(char *) \"%s\",\t/* %d */\n", name, tok);
  136. i++;
  137. }
  138. printf("};\n\n");
  139. for (p=proc; p->token!=0; p++)
  140. table[p->token-FIRSTTOKEN] = p->name;
  141. printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
  142. for (i=0; i<SIZE; i++)
  143. if (table[i]==0)
  144. printf("\tnullproc,\t/* %s */\n", names[i]);
  145. else
  146. printf("\t%s,\t/* %s */\n", table[i], names[i]);
  147. printf("};\n\n");
  148. printf("char *tokname(int n)\n"); /* print a tokname() function */
  149. printf("{\n");
  150. printf(" static char buf[100];\n\n");
  151. printf(" if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
  152. printf(" sprintf(buf, \"token %%d\", n);\n");
  153. printf(" return buf;\n");
  154. printf(" }\n");
  155. printf(" return printname[n-FIRSTTOKEN];\n");
  156. printf("}\n");
  157. return 0;
  158. }