-
Notifications
You must be signed in to change notification settings - Fork 7
/
ComGlobal.h
612 lines (528 loc) · 19.7 KB
/
ComGlobal.h
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#pragma once
#ifndef WINDOWS
#define UNIX
#endif
#ifdef MAIN
#define EXTERN
#else
#define EXTERN extern
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Includes
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "ctype.h"
#include "stdio.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#ifdef WINDOWS
#include "wtypes.h"
#include <io.h>
#include "malloc.h"
#endif
#ifdef UNIX
#include <unistd.h>
#define S_IREAD S_IRUSR
#define S_IWRITE S_IWUSR
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Defines
enum options
{
#ifdef LRSTAR
PG_ANALYZEONLY ,
PG_ASTCONST ,
PG_BACKSLASH ,
PG_BOOLMATRIX ,
PG_CLR_PARSER ,
PG_CONFLICTS ,
PG_CONFLICTTRACE ,
PG_DEBUG ,
PG_DEFAULTRED ,
PG_EOFINCLUDED ,
PG_ERRORCOUNT ,
PG_EXPECTING ,
PG_GRAMMAR ,
PG_HTML ,
PG_KEYWORDCHECK ,
PG_LALR_PARSER ,
PG_LR_PARSER ,
PG_MINIMIZE ,
PG_NODEARGS ,
PG_NONDETER ,
PG_OPTIMIZE ,
PG_OPTION_X ,
PG_OPTION_Y ,
PG_OPTION_Z ,
PG_PARSETREE ,
PG_QUIET ,
PG_REDUCEONLY ,
PG_SHIFTREDUCE ,
PG_STATELIST ,
PG_SYMBOLS ,
PG_TAB ,
PG_TRANSITIONS ,
PG_VERBOSE ,
PG_WARNINGS ,
#endif
#ifdef DFASTAR
LG_ANALYZEONLY,
LG_BACKSLASH,
LG_BLANKGOTOS,
LG_COLNUMB,
LG_CONFLICTS,
LG_DEBUG,
LG_DIRECTCODE,
LG_ERRORCOUNT,
LG_GRAMMAR,
LG_INSENSITIVE,
LG_KEYWORDIDENT,
LG_LINENUMB,
LG_MINIMIZE,
LG_NUMBCHAR,
LG_OPTIMIZE,
LG_QUIET,
LG_REDUCEONLY,
LG_REMOVEDUP,
LG_SORTACTIONS,
LG_STATELIST,
LG_STATELISTOPT,
LG_TAB,
LG_TABLES,
LG_TABL_SMALL,
LG_TABL_MEDIUM,
LG_TABL_LARGE,
LG_TRANSITIONS,
LG_VERBOSE,
LG_WARNINGS,
#endif
MAX_SYM,
MAX_PRO,
MAX_TAIL,
MAX_EBNF,
MAX_STA,
MAX_FIN,
MAX_KER,
MAX_NTT,
MAX_TT,
MAX_TTA,
MAX_LA,
MAX_LB,
MAX_INC,
MAX_CH,
MAX_ND,
N_OPTIONS
};
#ifdef WINDOWS
#define read _read
#define write _write
#define open _open
#define close _close
#define chmod _chmod
#define unlink _unlink
#else // WINDOWS
#define strlwr mystrlwr
#define strupr mystrupr
#define _filelength filelength
#endif
#define cchar const char
#define uchar unsigned char
#define uint unsigned int
#define ushort unsigned short
#define ulong unsigned long
#define YES 1 // Yes value.
#define NO 0 // No value.
#undef MAX_PATH
#define MAX_PATH 260 // Maximum path length.
#define MAX_DIR 160 // Maximum directory name length, 159.
#define MAX_FILENAME 64 // Maximum file name length, 63.
#define MAX_FILETYPE 32 // Maximum file type length, 31.
#undef UINT_MAX
#define UINT_MAX 0xFFFFFFFF // 32-bit integer value for hasing.
#define MAX_INT 2147483647 // Maximum integer.
#define EOF_CHAR 26
#define EOL_CHAR 10
#define UPPERCASE 1
#define LOWERCASE 2
#define UNDERSCORE 4
#define DIGIT 8
#define QUOTE 16
// Function-call defines ...
#ifdef _DEBUG
#define ALLOC(x,norg) alloc (#x, (char*&)x, sizeof(*x), norg)
#else
#define ALLOC(x,norg) alloc ((char*&)x, sizeof(*x), norg)
#endif
#define REALLOC(x,norg,nnew) ralloc((char*&)x, sizeof(*x), norg, nnew)
#define FREE(x,n) frea ((char*&)x, sizeof(*x), n)
#define FASTCMP(a,b,n) fastcmp ((int*)a, (int*)b, n)
#define FASTCPY(a,b,n) fastcpy ((int*)a, (int*)b, n)
#define FASTINI(a,b,n) fastini ( a, (int*)b, n)
#define FASTMRG(a,b,n) fastmrg ((int*)a, (int*)b, n)
#define FASTOR(a,b,n) fastor ((int*)a, (int*)b, n)
// USINGBITS (and the following defines) uses less memory
// but runs about the same speed, better for huge grammars.
#define USINGBITS
#define ifbit1(matrix,x) if (matrix[x>>3] & (0x80>>(x&0x0007)))
#define ifbit2(matrix,x,y) if (matrix[x][y>>3] & (0x80>>(y&0x0007)))
#define setbit1(matrix,x) matrix[x>>3] |= (0x80>>(x&0x0007))
#define setbit2(matrix,x,y) matrix[x][y>>3] |= (0x80>>(y&0x0007))
/////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Typedefs
struct OPTION
{
const char* name;
const char* desc;
int numb;
int defvalue;
};
typedef struct ITEM
{
int symb;
int prod;
int dot;
}
ITEM;
typedef struct CLOSURE
{
int item;
int next;
}
CLOSURE;
typedef struct SYMLIST
{
int symb;
int start;
int end;
}
SYMLIST;
typedef struct CHILD
{
int numb;
int link;
}
CHILD;
typedef struct CODETABLE
{
char* keyword;
char* operators;
int number;
char* description;
}
CODETABLE;
#define MAXTOP 10
struct STAKTYPE
{
const char* groupstart;
int skipcode;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Classes
class Token
{
public:
char* start; // Start of symbol.
char* end; // End of symbol.
int sti; // Symbol table index.
int line; // Input line number.
int column; // Input column number.
};
class Symbol
{
public:
char* name; // Pointer to symbol name (allocated). 4 4
char* start; // Pointer to symbol start address (in source code). 4 8
int length; // Length of symbol. 2 12
int type; // Type (e.g. integer, float, double, char, ...) 2 16
int line; // Line of first encounter in grammar. 4 20
int numb; // Number assigned to symbol (sequence number). 4 24
int value; // Value of symbol (for constants and integers). 4 28
};
class Symtab
{
public:
static Symbol* symbol; // Symbols.
static int n_symbols; // Number of symbols.
static short length; // Current length of symbol name.
static int cell; // Current hash cell number.
static int sti; // Current symbol-table index.
static uint hashdiv; // Hash divisor.
static int* hashvec; // Hash vector.
static int max_cells; // Maximum number of cells in the hash vector = 2*max_symbols.
static int max_symbols; // Maximum number of symbols.
// Functions ...
static void init_symtab (int); // Initialize the symbol table.
static void term_symtab (); // Terminate the symbol table.
static char* symname (int); // Get symbol name.
static int add_symbol (short, char*, const char*, int, int); // Add symbol to symbol table.
static int get_symbol (const char*, const char*, int); // Get symbol number.
static void print_symtab (char**); // Print the symbol table.
};
class Node
{
public:
int id; // Node id number. 4 4
int numb; // Node number. 4 8
int prod; // Production number. 4 12
int sti; // Symbol-table index (can be negative). 4 16
int line; // Line number. 4 20
char* start; // Start of token in source line. 4 24
char* end; // End of token in source line. 4 28
Node* next; // Next node. 4 32
Node* prev; // Previous node. 4 36
Node* child; // Child node. 4 40
Node* parent; // Parent node. 4 44
};
class PStack // Parser stack.
{
public:
int state; // Parser state. 4 4
char* start; // Token start address. 4 8
char* end; // Token end address. 4 12
int line; // Input line number. 4 16
int sti; // Symbol table index. 4 20
int sym; // Symbol stacked, terminal (positive) or nonterminal (negative).
Node* node; // Node pointer. 4 24
Node* last; // Last pointer. 4 28 bytes
};
class RStack // Restore Stack.
{
public:
PStack* ptr; // Parse stack pointer. 4 4
int state; // State. 4 8 bytes.
};
class Stack
{
public:
int id; // Node id. 2 2
int counter; // Counter of node accesses. 2 4
};
class AST : public Symtab
{
public:
static char indent[256]; // Indentation for printing current node.
static int* counter; // Node counter array.
static short stacki; // AST stack index.
static Stack* stack; // AST stack array.
static Node* node; // AST node array.
static Node* root; // Root node pointer.
static Node* currnode; // Current node pointer.
static int n_nodes; // Number of nodes created.
static int n_nodenames; // Number of node names.
static int n_nodeactions; // Number of node actions.
static short pass; // Pass number when traversing the AST.
static short status; // Status (TOP_DOWN | PASS_OVER | BOTTOM_UP).
static char* node_name[]; // Node names array.
static char** term_symb; // Terminal symbols.
static int nact_numb[]; // Node action numbers.
static char* nact_name[]; // Node action names.
// Functions ...
static void init_ast ();
static void term_ast ();
static void print_ast (char**);
static void print_ast (Node* np);
static void print_node (char *indent, Node* np);
static void traverse ();
static void traverse (Node* np);
static void traverse (char *indent, Node* np);
static short emitstr (Node* np, char* str);
static int (*nact_func[])(Node*);
};
/////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Global Variables
extern const char* program;
extern const char* version;
extern uchar charcode[256];
extern uchar numeric[256];
extern uchar alpha[256];
extern uchar upper[256];
extern uchar lower[256];
EXTERN int n_errors;
EXTERN int n_warnings;
EXTERN char confid [MAX_PATH];
EXTERN char dir [MAX_DIR];
EXTERN char exefid [MAX_PATH];
EXTERN char gdn [MAX_DIR];
EXTERN char gfn [MAX_FILENAME];
EXTERN char gft [MAX_FILETYPE];
EXTERN char grmfid [MAX_PATH];
EXTERN char logfid [MAX_PATH];
EXTERN char lstfid [MAX_PATH];
EXTERN int optn[N_OPTIONS];
EXTERN int optncount[N_OPTIONS];
EXTERN char odn [MAX_DIR];
EXTERN char ofn [MAX_FILENAME];
EXTERN char oft [MAX_FILETYPE];
EXTERN char outfid [MAX_PATH];
EXTERN char sdn [MAX_DIR];
EXTERN char sfn [MAX_FILENAME];
EXTERN char sft [MAX_FILETYPE];
EXTERN char sklfid [MAX_PATH];
EXTERN char stafid [MAX_PATH];
EXTERN FILE* logfp;
EXTERN FILE* errfp;
EXTERN FILE* lstfp;
EXTERN FILE* confp;
EXTERN FILE* grmfp;
EXTERN FILE* stafp;
EXTERN int time1;
EXTERN int time2;
EXTERN char string [10000];
EXTERN int filesize;
EXTERN int filedesc;
EXTERN char* input_start; /* First byte of input area. */
EXTERN char* input_end; /* Byte after input. */
EXTERN char* lex_input_start; /* First byte of input area. */
EXTERN char* lex_input_end; /* Byte after input. */
EXTERN int n_lines;
EXTERN char spaces [256]
#ifdef MAIN
= " " // 64
" " // 128
" " // 192
" " // 255
#endif
;
extern OPTION PGOption[];
extern OPTION LGOption[];
extern OPTION MAOption[];
EXTERN char** line_ptr;
EXTERN char** lex_line_ptr;
EXTERN int memory_max;
EXTERN int memory_usage;
EXTERN int max_child; // Option setting (rename this).
EXTERN int max_child_usage; // Highest number found during processing.
EXTERN int max_lookah;
EXTERN int max_terml;
EXTERN int max_headl;
EXTERN int max_symbl;
EXTERN int max_errors;
EXTERN int option_warnings;
EXTERN int option_grammar;
EXTERN char* lexer_input;
EXTERN int lexer_linenumb;
EXTERN CHILD* child;
EXTERN int* f_child;
EXTERN int n_childs;
/////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Generate Output Variables
EXTERN int num_char;
EXTERN int num_uchar;
EXTERN int num_short;
EXTERN int num_ushort;
EXTERN int num_int;
EXTERN int num_uint;
EXTERN int num_charp;
EXTERN char str_char [32];
EXTERN char str_uchar [32];
EXTERN char str_short [32];
EXTERN char str_ushort[32];
EXTERN char str_int [32];
EXTERN char str_uint [32];
EXTERN char str_charp [32];
EXTERN int parser_defined;
EXTERN int lexer_defined;
/////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Global Functions
extern void MTSL ();
extern void C_CAMEFROM (int n_states, int* tt_start, int* tt_action, int* ntt_start, int* ntt_action, int*& f_camefrom, int*& camefrom);
extern void C_FIRST (int n_heads, int n_terms, int n_prods, int* f_prod, int* f_tail, int* tail, char**& FIRST, int* nullable, int* head_sym);
extern void C_FOLLOW(int n_heads, int n_terms, int n_prods, int* f_prod, int* f_tail, int* tail, char** FIRST, char**& FOLLOW, int* nullable, int* head_sym);
extern void C_FIRSTNT ();
extern void P_FIRST (int n_heads, int n_terms, char** FOLLOW, char** term_name, char** head_name);
extern void P_FOLLOW (int n_heads, int n_terms, char** FOLLOW, char** term_name, char** head_name);
extern int close_con ();
extern int close_grm ();
extern int close_log ();
extern int close_sta ();
extern int close_lst ();
#ifdef UNIX
extern long _filelength (int fd);
#endif
extern int fastcmp (int*, int*, int);
extern void fastcpy (int*, int*, int);
extern void fastini (int, int *, int);
extern int fastmrg (int*, int*, int);
extern void fastor (int*, int*, int);
extern void fix_backslash (char*in);
extern void frea (char*& x, int size, int n);
extern int get_fid (char*arg, char*dir, char*fn, char*ft);
extern int GetSkeletonFilename (char* arg, int i, int na);
extern int GetOutputFilename (char* arg, int i, int na);
extern int GetMaxValues (const char* dn, const char* fn);
extern void SaveMaxValues (const char* dn, const char* fn);
extern int itsakeyword (char* terminal);
extern int inputi (char*);
extern void InternalError (int n);
extern void MemCrash (char* msg);
extern void MemCrash (char* value, int n);
extern char* mystrlwr (char* s);
extern char* mystrupr (char* s);
extern void memcrash (char*, int);
extern void number (int x, char* num);
extern int open_log(const char*);
extern int open_grm (char*);
extern int open_con (char*);
extern int open_sta (char*);
extern int open_lst (char*);
extern void prt_error (char* msg, char* start, char* end, int linenumb);
extern void prt_warning (char* msg, char* start, char* end, int linenumb);
extern void prt_message (char* msgtype, char* msg, char* start, char* end, int linenumb);
extern void prt_line_with_pointer (char* tokenstart, char* tokenend, int tokenlinenumb);
extern void prt_lst (char*format,...);
extern void prt_log(const char* format, ...);
extern void prt_logonly(const char*format,...);
extern int prt_grm (char*format,...);
extern void prt_con (char*format,...);
extern void prt_sta (char*format,...);
extern void PRT_OPTNS (int na, char** arg);
extern void prt_path (int itm, int state);
extern void prt_pointer (int numb, char*line, char*object);
extern void prt_prod (int p, int dot);
extern void prt_num(const char* desc, int n, const char* name, int max);
extern int prt_sym (int s, char *sp, char** term_name, char** head_name);
extern void quit (int);
extern void ralloc (char*& x, int size, int n1, int n2);
extern void SORT (int *start, int *end);
extern void SORT2 (int* first, int* second, int n);
extern void SORT3 (int* first, int* second, int* third, int n);
extern void SORTNUMBS (int* numb, int n, int* seq);
extern int symlen (int s, char** term_name, char** head_name);
extern char* strchr (char*p, char c);
extern char* strrchr (char* str, char c);
extern int SET_OPTN (char* opt, char* fid, int linenumb);
extern int set_optn (OPTION* option, char* opt, char* fid, int linenumb);
extern int SET_OPTNS (int na, char** arg);
extern void SORTNAMES (char** start, int n, int* seq);
extern void SORTNAMES2 (char** start, int n, int* seq, int* pos);
extern void TRAVERSE (int x);
extern void Terminate (int n);
#ifdef _DEBUG
extern char* alloc (char *s, char*& x, int size, int n);
#else
extern char* alloc (char*& x, int size, int n);
#endif
extern void ralloc (char*& x, int size, int n1, int n2);
extern void frea (char*& x, int size, int n);
extern int fastcmp (int *a, int *b, int n);
extern void fastcpy (int *a, int *b, int n);
extern void fastini (int v, int *b, int n);
extern int fastmrg (int *a, int *b, int n);
extern void fastor (int *a, int *b, int n);
extern int ATTACH (int x, int y);
extern int ATTACHED (int x, int y);
extern void T_GRAPH (char **graph, int nr, int nc);
extern void P_GRAPH (char** graph, int nr, int nc);
extern void TRAVERSE (int x);
// //
///////////////////////////////////////////////////////////////////////////////