Parameters are parsed with getopt. Simplify, constify.

This commit is contained in:
David Given 2016-09-24 16:59:49 +02:00
parent 434eafd35d
commit 13132128a1
2 changed files with 40 additions and 68 deletions

View file

@ -1,4 +1,5 @@
#include <assert.h> #include <assert.h>
#include <unistd.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -9,10 +10,10 @@
static char rcsid[] = "$Id$"; static char rcsid[] = "$Id$";
int maxcost = SHRT_MAX; int maxcost = SHRT_MAX / 2;
static char* prefix = "burm"; static char* prefix = "burm";
static int Iflag = 0, Tflag = 0; static int Tflag = 1; /* tracing */
static int ntnumber = 0; static int ntnumber = 0;
static Nonterm start = 0; static Nonterm start = 0;
static Term terms; static Term terms;
@ -51,49 +52,26 @@ int main(int argc, char* argv[])
yydebug = 1; yydebug = 1;
#endif #endif
if (sizeof(short) == sizeof(int)) for (;;)
maxcost = SHRT_MAX / 2; {
for (i = 1; i < argc; i++) int opt = getopt(argc, argv, "p:");
if (strcmp(argv[i], "-I") == 0) if (opt == -1)
Iflag = 1; break;
else if (strcmp(argv[i], "-T") == 0)
Tflag = 1; switch (opt)
else if (strncmp(argv[i], "-maxcost=", 9) == 0 && isdigit(argv[i][9]))
maxcost = atoi(argv[i] + 9);
else if (strncmp(argv[i], "-p", 2) == 0 && argv[i][2])
prefix = &argv[i][2];
else if (strncmp(argv[i], "-p", 2) == 0 && i + 1 < argc)
prefix = argv[++i];
else if (*argv[i] == '-' && argv[i][1])
{ {
yyerror("usage: %s [-T | -I | -p prefix | -maxcost=ddd ]... [ [ input ] output \n", case 'p':
argv[0]); prefix = optarg;
exit(1); break;
}
else if (infp == NULL) default:
{ yyerror("usage: %s [-p prefix] < input > output\n", argv[0]);
if (strcmp(argv[i], "-") == 0)
infp = stdin;
else if ((infp = fopen(argv[i], "r")) == NULL)
{
yyerror("%s: can't read `%s'\n", argv[0], argv[i]);
exit(1); exit(1);
}
} }
else if (outfp == NULL) }
{
if (strcmp(argv[i], "-") == 0) infp = stdin;
outfp = stdout; outfp = stdout;
if ((outfp = fopen(argv[i], "w")) == NULL)
{
yyerror("%s: can't write `%s'\n", argv[0], argv[i]);
exit(1);
}
}
if (infp == NULL)
infp = stdin;
if (outfp == NULL)
outfp = stdout;
yyin = infp; yyin = infp;
yyparse(); yyparse();
@ -108,8 +86,7 @@ int main(int argc, char* argv[])
emitstruct(nts, ntnumber); emitstruct(nts, ntnumber);
emitnts(rules, nrules); emitnts(rules, nrules);
emitterms(terms); emitterms(terms);
if (Iflag) emitstring(rules);
emitstring(rules);
emitrule(nts); emitrule(nts);
emitclosure(nts); emitclosure(nts);
emitpredicatedefinitions(rules); emitpredicatedefinitions(rules);
@ -521,14 +498,11 @@ static void emitdefs(Nonterm nts, int ntnumber)
for (p = nts; p; p = p->link) for (p = nts; p; p = p->link)
print("#define %P%S_NT %d\n", p, p->number); print("#define %P%S_NT %d\n", p, p->number);
print("int %Pmax_nt = %d;\n\n", ntnumber); print("static const int %Pmax_nt = %d;\n\n", ntnumber);
if (Iflag) print("const char *%Pntname[] = {\n%10,\n");
{ for (p = nts; p; p = p->link)
print("char *%Pntname[] = {\n%10,\n"); print("%1\"%S\",\n", p);
for (p = nts; p; p = p->link) print("%10\n};\n\n");
print("%1\"%S\",\n", p);
print("%10\n};\n\n");
}
} }
/* emitfuncs - emit functions to access node fields */ /* emitfuncs - emit functions to access node fields */
@ -683,12 +657,12 @@ static void emitnts(Rule rules, int nrules)
; ;
if (str[j] == NULL) if (str[j] == NULL)
{ {
print("static short %Pnts_%d[] = { %s0 };\n", j, buf); print("static const short %Pnts_%d[] = { %s0 };\n", j, buf);
str[j] = strdup(buf); str[j] = strdup(buf);
} }
nts[i++] = j; nts[i++] = j;
} }
print("\nshort *%Pnts[] = {\n"); print("\nconst short *%Pnts[] = {\n");
for (i = j = 0, r = rules; r; r = r->link) for (i = j = 0, r = rules; r; r = r->link)
{ {
for (; j < r->ern; j++) for (; j < r->ern; j++)
@ -722,7 +696,7 @@ static void emitrule(Nonterm nts)
for (p = nts; p; p = p->link) for (p = nts; p; p = p->link)
{ {
Rule r; Rule r;
print("static short %Pdecode_%S[] = {\n%10,\n", p); print("static const short %Pdecode_%S[] = {\n%10,\n", p);
for (r = p->rules; r; r = r->decode) for (r = p->rules; r; r = r->decode)
print("%1%d,\n", r->ern); print("%1%d,\n", r->ern);
print("};\n\n"); print("};\n\n");
@ -802,14 +776,14 @@ static void emitstring(Rule rules)
Rule r; Rule r;
int k; int k;
print("short %Pcost[][4] = {\n"); print("static const short %Pcost[][4] = {\n");
for (k = 0, r = rules; r; r = r->link) for (k = 0, r = rules; r; r = r->link)
{ {
for (; k < r->ern; k++) for (; k < r->ern; k++)
print("%1{ 0 },%1/* %d */\n", k); print("%1{ 0 },%1/* %d */\n", k);
print("%1{ %d },%1/* %d = %R */\n", r->cost, k++, r); print("%1{ %d },%1/* %d = %R */\n", r->cost, k++, r);
} }
print("};\n\nchar *%Pstring[] = {\n"); print("};\n\nconst char *%Pstring[] = {\n");
for (k = 0, r = rules; r; r = r->link) for (k = 0, r = rules; r; r = r->link)
{ {
for (; k < r->ern; k++) for (; k < r->ern; k++)
@ -841,7 +815,7 @@ static void emitterms(Term terms)
Term p; Term p;
int k; int k;
print("char %Parity[] = {\n"); print("static const char %Parity[] = {\n");
for (k = 0, p = terms; p; p = p->link) for (k = 0, p = terms; p; p = p->link)
{ {
for (; k < p->esn; k++) for (; k < p->esn; k++)
@ -849,17 +823,15 @@ static void emitterms(Term terms)
print("%1%d,%1/* %d=%S */\n", p->arity < 0 ? 0 : p->arity, k++, p); print("%1%d,%1/* %d=%S */\n", p->arity < 0 ? 0 : p->arity, k++, p);
} }
print("};\n\n"); print("};\n\n");
if (Iflag)
print("static const char *%Popname[] = {\n");
for (k = 0, p = terms; p; p = p->link)
{ {
print("char *%Popname[] = {\n"); for (; k < p->esn; k++)
for (k = 0, p = terms; p; p = p->link) print("%1/* %d */%10,\n", k);
{ print("%1/* %d */%1\"%S\",\n", k++, p);
for (; k < p->esn; k++)
print("%1/* %d */%10,\n", k);
print("%1/* %d */%1\"%S\",\n", k++, p);
}
print("};\n\n");
} }
print("};\n\n");
} }
/* emittest - emit clause for testing a match */ /* emittest - emit clause for testing a match */

View file

@ -1,7 +1,7 @@
static void dumpCover(NODEPTR_TYPE p, int goalnt, int indent) { static void dumpCover(NODEPTR_TYPE p, int goalnt, int indent) {
#ifdef TRACE #ifdef TRACE
int eruleno = burm_rule(STATE_LABEL(p), goalnt); int eruleno = burm_rule(STATE_LABEL(p), goalnt);
short *nts = burm_nts[eruleno]; const short *nts = burm_nts[eruleno];
NODEPTR_TYPE kids[10]; NODEPTR_TYPE kids[10];
int i; int i;