summaryrefslogtreecommitdiff
path: root/utils/Burg/main.c
blob: dbfdbf4faca98ef03321863ab97f7783a745b78e (plain)
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
179
180
181
182
char rcsid_main[] = "$Id$";

#include <math.h>
#include <stdio.h>
#include "b.h"
#include "fe.h"

int debugTables = 0;
static int simpleTables = 0;
static int internals = 0;
static int diagnostics = 0;

static char *inFileName;
static char *outFileName;

static char version[] = "BURG, Version 1.0";

extern int main ARGS((int argc, char **argv));

int
main(argc, argv) int argc; char **argv;
{
	int i;
	extern int atoi ARGS((char *));

	for (i = 1; argv[i]; i++) {
		char **needStr = 0;
		int *needInt = 0;

		if (argv[i][0] == '-') {
			switch (argv[i][1]) {
			case 'V':
				fprintf(stderr, "%s\n", version);
				break;
			case 'p':
				needStr = (char**)&prefix;
				break;
			case 'o':
				needStr = &outFileName;
				break;
			case 'I':
				internals = 1;
				break;
			case 'T':
				simpleTables = 1;
				break;
			case '=':
#ifdef NOLEX
				fprintf(stderr, "'%s' was not compiled to support lexicographic ordering\n", argv[0]);
#else
				lexical = 1;
#endif /* NOLEX */
				break;
			case 'O':
				needInt = &principleCost;
				break;
			case 'c':
				needInt = &prevent_divergence;
				break;
			case 'e':
				needInt = &exceptionTolerance;
				break;
			case 'd':
				diagnostics = 1;
				break;
			case 'S':
				speedflag = 1;
				break;
			case 't':
				trimflag = 1;
				break;
			case 'G':
				grammarflag = 1;
				break;
			default:
				fprintf(stderr, "Bad option (%s)\n", argv[i]);
				exit(1);
			}
		} else {
			if (inFileName) {
				fprintf(stderr, "Unexpected Filename (%s) after (%s)\n", argv[i], inFileName);
				exit(1);
			}
			inFileName = argv[i];
		}
		if (needInt || needStr) {
			char *v;
			char *opt = argv[i];

			if (argv[i][2]) {
				v = &argv[i][2];
			} else {
				v = argv[++i];
				if (!v) {
					fprintf(stderr, "Expection argument after %s\n", opt);
					exit(1);
				}
			}
			if (needInt) {
				*needInt = atoi(v);
			} else if (needStr) {
				*needStr = v;
			}
		}
	}

	if (inFileName) {
		if(freopen(inFileName, "r", stdin)==NULL) {
			fprintf(stderr, "Failed opening (%s)", inFileName);
			exit(1);
		}
	}

	if (outFileName) {
		if ((outfile = fopen(outFileName, "w")) == NULL) {
			fprintf(stderr, "Failed opening (%s)", outFileName);
			exit(1);
		}
	} else {
		outfile = stdout;
	}


	yyparse();

	if (!rules) {
		fprintf(stderr, "ERROR: No rules present\n");
		exit(1);
	}

	findChainRules();
	findAllPairs();
	doGrammarNts();
	build();

	debug(debugTables, foreachList((ListFn) dumpOperator_l, operators));
	debug(debugTables, printf("---final set of states ---\n"));
	debug(debugTables, dumpMapping(globalMap));


	startBurm();
	makeNts();
	if (simpleTables) {
		makeSimple();
	} else {
		makePlanks();
	}

	startOptional();
	makeLabel();
	makeKids();

	if (internals) {
		makeChild();
		makeOpLabel();
		makeStateLabel();
	}
	endOptional();

	makeOperatorVector();
	makeNonterminals();
	if (internals) {
		makeOperators();
		makeStringArray();
		makeRuleDescArray();
		makeCostArray();
		makeDeltaCostArray();
		makeStateStringArray();
		makeNonterminalArray();
		/*
		makeLHSmap();
		*/
	}
	makeClosureArray();

	if (diagnostics) {
		reportDiagnostics();
	}

	yypurge();
	exit(0);
}