-
Notifications
You must be signed in to change notification settings - Fork 7
/
ComMain.cpp
401 lines (365 loc) · 12.2 KB
/
ComMain.cpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#if defined(LRSTAR)
const char* program = "LRSTAR";
#elif defined(DFASTAR)
const char* program = "DFASTAR";
#endif
const char* version = "6.6.003";
const char* copywrt = "Copyright 2014 Paul B Mann";
#define MAIN
#include "ComGlobal.h"
#include "ComGenerate.h"
#ifdef LRSTAR
# include "PGGenerate.h"
#else
# include "LGGenerate.h"
#endif
#ifdef LRSTAR
#include "PGGlobal.h"
OPTION PGOption[]= // Parser Generator Options
{
{"a", "Analyze the grammar only", PG_ANALYZEONLY, 0},
{ "ast", "Parser will build an AST", PG_ASTCONST, 1},
// {"bm", "Boolean matrix (0=none,1=bytes,2=bits)", PG_BOOLMATRIX, 2},
{"c", "Conflict report", PG_CONFLICTS, 0},
{"clr", "Canonical LR(1) parser", PG_CLR_PARSER, 0},
{"ct", "Conflict traceback information", PG_CONFLICTTRACE, 0},
{"d", "Debug option for generated parser", PG_DEBUG, 0},
// {"dr", "Default reductions", PG_DEFAULTRED, 1},
{"e", "Error count limit for grammar errors", PG_ERRORCOUNT, 500},
{"eof", "End-of-file character stops parsing", PG_EOFINCLUDED, 1},
{"exp", "Expecting list for syntax errors (1,2)", PG_EXPECTING, 0},
{"g", "Grammar listing (1,2)", PG_GRAMMAR, 0},
// {"h", "HTML grammar listing", PG_HTML, 0},
{"k", "Keyword checking in grammar", PG_KEYWORDCHECK, 0},
{"lalr", "LALR(1) parser", PG_LALR_PARSER, 0},
{"lr", "Minimal LR(1) parser", PG_LR_PARSER, 1},
{"m", "Minimize parser-table size", PG_MINIMIZE, 0},
{"na", "Number of arguments for node actions", PG_NODEARGS, 0},
{"nd", "Nondeterministic LR(k) parsing (1,2)", PG_NONDETER, 0},
{"o", "Optimize parser speed", PG_OPTIMIZE, 0},
{"q", "Quiet mode, minimal screen display", PG_QUIET, 0},
{"rr", "Reduce-reduce conflicts only", PG_REDUCEONLY, 0},
{"s", "State machine listing (1,2)", PG_STATELIST, 0},
// {"sra", "Shift-Reduce Actions in parser", PG_SHIFTREDUCE, 1},
{"tab", "Tab setting in grammar (2,3,4)", PG_TAB, 3},
{"v", "Verbose mode, output more information", PG_VERBOSE, 1},
{"w", "Warnings listing", PG_WARNINGS, 0},
{"x", "Defined by user (x=?)", PG_OPTION_X, 0},
{"y", "Defined by user (y=?)", PG_OPTION_Y, 0},
{"z", "Defined by user (z=?)", PG_OPTION_Z, 0},
{"", "", 0, 0}
};
#endif
#ifdef DFASTAR
#include "LGGlobal.h"
OPTION LGOption[]= // Lexer Generator Options
{
"a", "Analyze the grammar only", LG_ANALYZEONLY, 0,
"c", "Conflict report (all conflicts)", LG_CONFLICTS, 0,
"col", "Column number counting in lexer", LG_COLNUMB, 0,
"d", "Debug option for generated lexer", LG_DEBUG, 0,
"dc", "Direct-code lexer", LG_DIRECTCODE, 0,
"e", "Error count limit for grammar errors", LG_ERRORCOUNT, 256,
"g", "Grammar listing", LG_GRAMMAR, 0,
"i", "Case insensitive lexer ('a'='A')", LG_INSENSITIVE, 0,
"line", "Line number counting in lexer", LG_LINENUMB, 1,
"m", "Minimize lexer-table size", LG_MINIMIZE, 1,
// "nc", "Number of characters (128,256)", LG_NUMBCHAR, 256,
"ki", "Keyword and <identifier> recognition", LG_KEYWORDIDENT, 1,
"q", "Quiet mode, minimal screen display", LG_QUIET, 0,
"r", "Remove duplicate states", LG_REMOVEDUP, 1,
"s", "State machine for conflicts report", LG_STATELIST, 0,
"so", "Optimized state machine", LG_STATELISTOPT, 0,
"tab", "Tab setting in grammar (2,3,4)", LG_TAB, 3,
"ts", "Table-driven small", LG_TABL_SMALL, 0,
"tm", "Table-driven medium", LG_TABL_MEDIUM, 0,
"tl", "Table-driven large", LG_TABL_LARGE, 0,
"v", "Verbose, output more information", LG_VERBOSE, 1,
"w", "Warnings listing", LG_WARNINGS, 0,
"", "", 0, 0
};
#endif
#if defined WINDOWS
static const char *maxValsFile = "lrstar.txt";
#elif defined UNIX
static const char *maxValsFile = ".lrstar.cfg";
#endif
OPTION MAOption[]= // Memory Allocation Options
{
"/sym", "Symbols", MAX_SYM, 20000,
"/pro", "Productions", MAX_PRO, 50000,
"/tail", "Tail symbols", MAX_TAIL, 200000,
"/ebnf", "EBNF symbol space", MAX_EBNF, 10000,
"/sta", "States", MAX_STA, 300000,
"/fin", "Final items", MAX_FIN, 300000,
"/ker", "Kernel items", MAX_KER, 10000000,
"/ntt", "Nonterminal transitions", MAX_NTT, 200000,
"/tt", "Terminal transitions", MAX_TT, 2000000,
"/tta", "Terminal transitions added", MAX_TTA, 5000000,
"/la", "Lookaheads", MAX_LA, 10000000,
"/lb", "Lookbacks", MAX_LB, 2000000,
"/inc", "Includes", MAX_INC, 500000,
"/ch", "Child nodes in graph", MAX_CH, 500000,
"/nd", "Nondeterministic actions", MAX_ND, 100000,
"", "", 0, 0
};
void Options ();
void InitOptions ();
///////////////////////////////////////////////////////////////////////////////
// //
int main(int argc, char** argv)
{
printf ("\n%s %s %s.\n", program, version, copywrt);
if (argc == 1) // No arguments after program name?
{
Options();
quit (0);
}
if (get_fid (argv[1], gdn, gfn, gft))
{
int i;
char dn[256], fn[256], ft[256];
char GFT [MAX_FILETYPE];
#ifdef LRSTAR
if (*gft == 0) strcpy (gft, ".grm"); // Default filetype.
strcpy (grmfid, gdn);
strcat (grmfid, gfn);
strcat (grmfid, gft);
strcpy (GFT, gft);
mystrlwr (GFT);
if (strcmp (GFT, ".grm") != 0)
{
n_errors++;
printf ("\nInput file '%s' does not have a filetype of '.grm'\n\n", grmfid);
quit (0);
}
#endif
#ifdef DFASTAR
if (*gft == 0) strcpy (gft, ".lgr"); // Default filetype.
strcpy (grmfid, gdn);
strcat (grmfid, gfn);
strcat (grmfid, gft);
strcpy (GFT, gft);
mystrlwr (GFT);
if (strcmp (GFT, ".lgr") != 0)
{
if (strcmp (GFT, ".lex") != 0)
{
n_errors++;
printf ("\nInput file '%s' does not have a filetype of '.lex' or '.lgr'\n\n", grmfid);
quit (0);
}
}
#endif
filedesc = open (grmfid, 0);
if (filedesc < 0)
{
n_errors++;
printf ("\nCannot find input file: '%s'\n\n", grmfid);
quit (0);
}
close (filedesc);
open_log (grmfid);
open_con (grmfid);
open_grm (grmfid);
open_sta (grmfid);
open_lst (grmfid);
prt_logonly ("\n%s %s %s.\n", program, version, copywrt);
prt_log ("\nInput %s ", grmfid);
InitOptions ();
PRT_OPTNS (argc, argv);
if (SET_OPTNS (argc, argv) == 0)
{
Terminate (0);
}
for (i = 2; i < argc; )
{
#ifdef WINDOWS
if (argv[i][0] == '/') break; // Options start.
#endif
#ifdef UNIX
if (argv[i][0] == '-') break; // Options start.
#endif
if (GetSkeletonFilename (argv[i], i, argc) == 0) Terminate (0);
++i;
if (GetOutputFilename (argv[i], i, argc) == 0) Terminate (0);
++i;
}
#if defined WINDOWS
if (get_fid (argv[0], dn, fn, ft) == 0) Terminate (0);
#elif defined UNIX
strcpy(dn, getenv("HOME"));
strcat(dn, "/");
#endif
if (GetMaxValues (dn, "options.txt") == 0) Terminate (0);
#ifdef LRSTAR
int verbose = optn[PG_VERBOSE];
if (PG::Main (argc, argv))
#endif
#ifdef DFASTAR
int verbose = optn[LG_VERBOSE];
if (LG::Main (argc, argv))
#endif
{
int i = 2;
int n = 0;
int r = 1;
#if defined(LRSTAR)
PGGenerate generator;
#else
LGGenerate generator;
#endif
while (r > 0 && i < argc)
{
#ifdef WINDOWS
if (argv[i][0] == '/') break; // Options start.
#endif
#ifdef UNIX
if (argv[i][0] == '-') break; // Options start.
#endif
if (r = GetSkeletonFilename (argv[i], i, argc))
{
i++;
if (r = GetOutputFilename (argv[i], i, argc))
{
i++; n++;
generator.GenerateCode(sklfid, outfid, verbose);
}
}
}
if (n)
{
if (verbose > 1) prt_log ("\n");
else prt_logonly ("\n");
}
}
}
Terminate (0);
}
///////////////////////////////////////////////////////////////////////////////
// //
#ifdef LRSTAR
void Options ()
{
int i;
char programdesc[] =
"|\n"
"| LR(k) PARSER GENERATOR\n"
"|\n"
#ifdef WINDOWS
"| lrstar <grammar> [<skeleton> <output>]... [/<option>...]\n"
#endif
#ifdef UNIX
"| lrstar <grammar> [<skeleton> <output>]... [-<option>...]\n"
#endif
;
printf ("%s|\n", programdesc);
printf ("| OPTION DEFAULT DESCRIPTION\n");
for (i = 0; *PGOption[i].name != 0; i++)
{
printf ("| %-6s %5d %s\n", PGOption[i].name, PGOption[i].defvalue, PGOption[i].desc);
}
printf ("|_\n");
}
#endif
#ifdef DFASTAR
void Options ()
{
int i;
char programdesc[] =
"|\n"
"| DFA LEXER GENERATOR\n"
"|\n"
#ifdef WINDOWS
"| dfastar <grammar> [<skeleton> <output>]... [/<option>...]\n"
#endif
#ifdef UNIX
"| dfastar <grammar> [<skeleton> <output>]... [-<option>...]\n"
#endif
;
printf ("%s|\n", programdesc);
printf ("| OPTION DEFAULT DESCRIPTION\n");
for (i = 0; *LGOption[i].name != 0; i++)
{
printf ("| %-6s %5d %s\n", LGOption[i].name, LGOption[i].defvalue, LGOption[i].desc);
}
printf ("|_\n");
}
#endif
///////////////////////////////////////////////////////////////////////////////
// //
void InitOptions ()
{
int i;
for (i = 0; i < N_OPTIONS; i++)
{
optn[i] = 0;
}
#ifdef LRSTAR
for (i = 0; * PGOption[i].name != 0; i++)
{
optn[PGOption[i].numb] = PGOption[i].defvalue;
}
#endif
#ifdef DFASTAR
for (i = 0; * LGOption[i].name != 0; i++)
{
optn[LGOption[i].numb] = LGOption[i].defvalue;
}
#endif
for (i = 0; * MAOption[i].name != 0; i++)
{
optn[MAOption[i].numb] = MAOption[i].defvalue;
}
#ifdef LRSTAR
// Define invisible PG options ...
optn [PG_BACKSLASH] = 0;
optn [PG_SYMBOLS] = 0;
optn [PG_TRANSITIONS] = 1;
optn [PG_BOOLMATRIX] = 2;
optn [PG_DEFAULTRED] = 1;
optn [PG_HTML] = 0;
optn [PG_SHIFTREDUCE] = 1;
#endif
#ifdef DFASTAR
// Define invisible LG options ...
optn [LG_TABLES] = 1;
optn [LG_BACKSLASH] = 0;
optn [LG_TRANSITIONS] = 1;
#endif
}
///////////////////////////////////////////////////////////////////////////////
// //
void quit(int rc)
{
exit(rc);
}
///////////////////////////////////////////////////////////////////////////////
// //
void InternalError (int n)
{
n_errors++;
if (n_errors == 1) printf ("\n");
printf ("INTERNAL ERROR %d\n\n", n);
Terminate (0);
}
///////////////////////////////////////////////////////////////////////////////
// //
void MemCrash (char* msg)
{
prt_log ("\n%s\n", msg);
Terminate (1);
}
///////////////////////////////////////////////////////////////////////////////
// //
void MemCrash (char* value, int n)
{
n_errors++;
if (n_errors == 1) prt_log ("\n");
prt_log ("%s(%04d) : %s exceeds the limit of %d.\n", exefid, 1, value, n);
prt_log ("%s(%04d) : %s\n", exefid, 1, "Please increase the appropriate maximum value in this file:");
prt_log ("%s(%04d) : %s\n", exefid, 1, "<-- double click here.");
Terminate (1);
}
// //
///////////////////////////////////////////////////////////////////////////////