1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/******** Parameters **********/
#define MINNAME 1
/*************************/
static char* header =
"#ifndef %s\n\
#define %s\n\n\
/* generated by mkdefs; do not edit */\n\n";
static char* prefix =
"#define Y(i) (1<<(i))\n\
\n\
#define V 0x4 /* NODE */\n\
#define E 0x5 /* EDGE */\n\
#define G 0x6 /* GRAPH */\n\
#define O 0x7 /* OBJECT */\n\
#define TV 0x8 /* TV_ */\n\
#define YALL (Y(V)|Y(E)|Y(G))\n\
\n";
/*************************/
#define BSIZE 512
#define K_val 1
#define K_member 2
struct rec; typedef struct rec record;
struct rec {
record* next;
char* symbol;
char* name;
char* lex;
char* type;
char* domain;
char* range;
char* data;
int kind;
};
static int lineno;
static int tokno;
static record*
newRec (record* curr)
{
record* newr;
newr = (record*)malloc(sizeof(record));
if (!newr) {
fprintf (stderr, "mkdefs: out of memory, line %d\n", lineno);
exit(1);
}
curr->next = newr;
newr->next = 0;
return newr;
}
static void
genGuard (char* filename, char* guard)
{
char c;
while ((c = *filename++)) {
if (c == '.') *guard++ = '_';
else *guard++ = toupper (c);
}
*guard = '\0';
}
static char*
strTok (char* str)
{
char* tk;
tokno++;
tk = strtok (str, " \t\n");
return tk;
}
main (int argc, char* argv[])
{
char* filename;
char* buf;
char* tk;
record vals;
record* recp = &vals;
FILE* fp;
char guard[100];
int idx = MINNAME;
int lastval = 0;
int lastmem = 0;
if (argc != 2) {
fprintf (stderr, "mkdefs: 1 arguments necessary\n");
exit(1);
}
filename = argv[1];
vals.next = 0;
buf = malloc(BSIZE);
while (fgets (buf, BSIZE, stdin)) {
lineno++;
tokno = 0;
tk = strTok (buf);
if ((!tk) || (*tk == '#')) continue; /* comment */
recp = newRec (recp);
if (*tk == 'V')
recp->kind = K_val;
else if (*tk == 'M')
recp->kind = K_member;
else
recp->kind = 0;
recp->data = buf;
recp->symbol = tk;
recp->name = strTok (0);
recp->lex = strTok (0);
recp->type = strTok (0);
if (recp->kind) {
recp->domain = strTok (0);
recp->range = strTok (0);
}
buf = malloc(BSIZE);
}
fp = fopen (filename, "w");
if (!fp) {
fprintf (stderr, "mkdefs: Could not open %s for writing\n", filename);
exit(1);
}
genGuard (filename, guard);
fprintf (fp, header, guard, guard);
fputs (prefix, fp);
for (recp = vals.next; recp; recp = recp->next) {
if (recp->kind == K_val) lastval = idx;
else if (recp->kind == K_member) lastmem = idx;
fprintf (fp, "#define\t%s\t% 5d\n", recp->symbol, idx++);
}
idx--;
fprintf (fp, "\n#define LAST_V %d\n", lastval);
fprintf (fp, "#define LAST_M %d\n", lastmem);
fprintf (fp, "#define MINNAME %d\n#define MAXNAME %d\n\n", MINNAME, idx);
fprintf (fp, "static Exid_t symbols[] = {\n");
for (recp = vals.next; recp; recp = recp->next) {
fprintf (fp, "\tEXID ( %s, %s, %s, %s, 0),\n",
recp->name, recp->lex, recp->symbol, recp->type);
}
fprintf (fp, "\tEXID ( 0, 0, 0, 0, 0)\n};\n");
fprintf (fp, "\nstatic char* gprnames[] = {\n\t\"\",\n");
for (recp = vals.next; recp; recp = recp->next) {
fprintf (fp, "\t\"%s\",\n", recp->symbol);
}
fprintf (fp, "};\n");
fprintf (fp, "\ntypedef unsigned short tctype;\n");
fprintf (fp, "\nstatic tctype tchk[][2] = {\n\t{ 0, 0 },\n");
for (recp = vals.next; recp; recp = recp->next) {
if (recp->kind)
fprintf (fp, "\t{ %s, %s },\n", recp->domain, recp->range);
}
fprintf (fp, "};\n");
fprintf (fp, "\n#endif\n");
fclose (fp);
exit (0);
}
|