|
|
1.1 root 1: /*
2: * COCOA structure definitions
3: *
4: * This parser generator is dedicated to Reinaldo Braga.
5: * May he live a hundred years
6: */
7: /*
8: * Modified by nigel to eliminate ill-advised use of "flex-arrays" in the
9: * data structures, eliminate use of '%r' in yyerror (), and to use <limits.h>
10: * rather than magic internal definitions. Other than that, I had nothing to
11: * do with this garbage.
12: */
13: #include <stdio.h>
14: #include <limits.h>
15:
16: enum {
17: FATAL = 1, /* flag for yyerror */
18: SKIP, /* ditto */
19: NLNO, /* no line number on error line */
20: WARNING
21: };
22:
23: enum {
24: TTERM = 0, /* "genre" for token */
25: TNTERM, /* non-terminal */
26: TTYPE, /* <type> */
27: MAXT /* number of different genres */
28: };
29:
30:
31: /* defaults -- can be changed with run time options */
32: enum {
33: MAXPROD = 400, /* maximum number of productions */
34: MAXTERM = 200, /* maximum number of different terminals */
35: MAXNTERM = 150, /* maximum number of non terminal symbols */
36: MAXSTATE = 800, /* max # of states */
37: MAXPRODL = 20, /* maximum number of symbols in any prodn */
38: MAXITEM = 160, /* maximum number of items in any state */
39: MAXREDS = 300, /* maximum number of reductions per state */
40: MAXTYPE = 20 /* for the union of YYSTYPE */
41: };
42:
43: /*
44: * This defines the size of some char-based bit-sets declared below, and thus
45: * sets a hard upper bound on the configurable number of terminals. The
46: * original form of this was:
47: */
48: #if 0
49: /* if maxterm is > 127 change LSETSIZE */
50: #define LSETSIZE 30 /* chars in ws ::= MAXTERM/8 + 1 */
51: #endif
52:
53: #define LSETSIZE (MAXTERM * 2 / CHAR_BIT + 1)
54:
55: /* keyword codings */
56: enum {
57: START = 1,
58: /* %token .. %nonassoc must be contiguously coded */
59: TOKEN,
60: LEFT,
61: RIGHT,
62: NONASSOC,
63: UNION,
64: PREC,
65: TYPE,
66: SEMICOLON,
67: VBAR, /* production separators */
68: LBRAC, /* beginning of action */
69: T_IDENT,
70: C_IDENT,
71: MARK,
72: IDENT,
73: COMMA,
74: INTEGER
75: };
76:
77: /* precedence associativities */
78: enum {
79: UNASSOC = 0,
80: LASSOC,
81: RASSOC,
82: BASSOC /* "binary" associativity, meaning %nonassoc */
83: };
84:
85: /* macros */
86: /* character manipulation */
87:
88: /* run of the mill manifests */
89: #define UINT -1 /* "unknown" type -- integer */
90: #define UNKNOWN -1
91: #define SYMSIZE 32
92: #define DERIV 01 /* non terminal derives the empty string */
93: #define CPRES 02 /* temp flag for closure */
94: #define nterm gtab[TTERM].g_ordno
95: #define nnonterm gtab[TNTERM].g_ordno
96: #define ntype gtab[TTYPE].g_ordno
97: #define maxterm gtab[TTERM].g_maxord
98: #define maxnterm gtab[TNTERM].g_maxord
99: #define maxtype gtab[TTYPE].g_maxord
100: #define maxsym (maxterm+maxnterm+maxtype)
101: #define NTBASE 010000 /* base number for non terminal allocation */
102: #define EOFNO 0
103: #define ERRNO 1
104: #define bounded(v,l,name) if( v>=l ) yyerror(FATAL, bounderr, name, l)
105: #define MAXSYM 353
106:
107:
108: struct sym
109: {
110: char s_name[SYMSIZE];
111: int s_no; /* ordinal number of symbol */
112: char s_prc, s_ass; /* precedence, associativity */
113: int s_type;
114: char s_genre; /* "kind" of symbol -- terminal, nonterminal, type */
115: /* remaining flags are only used for non-terminals */
116:
117: char s_flags; /* for closure and lookahead computations */
118: int s_val; /* external value, for non terminal only */
119: int s_nprods; /* number of productions having nt as lhs */
120: struct prod **s_prods;
121: int s_nstates; /* for nt A, # of states with item A->. ai* */
122: int *s_states; /* list */
123: };
124:
125: struct sitem
126: {
127: int i_nitems; /* number of items in set */
128: int ** i_items;
129: };
130: #define SITEM_EXTRA_SIZE(n) ((n) * sizeof (int *))
131: #define SITEM_TOTAL_SIZE(n) (sizeof (struct sitem) + SITEM_EXTRA_SIZE (n))
132: #define SITEM_EXTRA_INIT(p) ((p)->i_items = (int **) ((p) + 1))
133:
134: struct state
135: {
136: int s_tgo;
137: struct tgo * s_tgos;
138: int s_ntgo;
139: struct ntgo * s_ntgos;
140: int s_nred;
141: struct redn * s_reds;
142: } ;
143:
144: struct prod
145: {
146: int p_prodno; /* index in prdptr */
147: char p_prc, p_ass; /* precedence, associativity */
148: int * p_ord; /*
149: * Ordinal numbers for production,
150: * starting with LHS and then the
151: * RHS ordinals terminated by a -1
152: * sentinel.
153: * The LHS ordinal is negated.
154: */
155: #if 0
156: int p_left; /* -(ordinal number for lhs) */
157: int * p_right; /*
158: * ordinal numbers for rhs w/ -1
159: * end marker
160: */
161: #endif
162: };
163: #define p_left p_ord [0]
164:
165: /*
166: * Macro for determining additional space required for "n" items in "p_right"
167: * vector, above.
168: */
169: #define PROD_EXTRA_SIZE(n) (((n) + 1) * sizeof (int))
170: #define PROD_TOTAL_SIZE(n) (sizeof (struct prod) + PROD_EXTRA_SIZE (n))
171: #define PROD_EXTRA_INIT(p) ((p)->p_ord = (int *) ((p) + 1))
172: #define PROD_RIGHT(p) ((p)->p_ord + 1)
173:
174: struct tgo
175: {
176: int tg_trm; /* ordinal number of terminal */
177: int tg_st;
178: };
179:
180: struct ntgo
181: {
182: int ng_nt; /* ordinal number of non terminal */
183: int ng_st; /* index of state */
184: union { /* MWC DSC */
185: struct rel *reln; /* pointer to relation set */
186: struct lset *look; /* pointer to lookahead set */
187: } cheapo; /* they're not used at once, so... */
188: };
189: #define ng_rel cheapo.reln
190: #define ng_lset cheapo.look
191:
192: struct redn
193: {
194: struct prod *rd_prod; /* production pointer */
195: struct lset *rd_lset; /* lookahead set */
196: };
197:
198: /* relation between nt transitions */
199: struct rel
200: {
201: int r_count;
202: int * r_list;
203: };
204: #define REL_EXTRA_SIZE(n) ((n) * sizeof (int))
205: #define REL_TOTAL_SIZE(n) (sizeof (struct rel) + REL_EXTRA_SIZE (n))
206: #define REL_EXTRA_INIT(p) ((p)->r_list = (int *) ((p) + 1))
207:
208: struct trans
209: {
210: struct ntgo * t_trans;
211: int t_level;
212: };
213:
214: struct lset
215: {
216: union {
217: unsigned char u_bits[LSETSIZE];
218: struct lset *u_next;
219: } un;
220: };
221: #define l_bits un.u_bits
222: #define l_next un.u_next
223:
224: struct resv
225: {
226: char *r_name;
227: int r_val;
228: };
229:
230: struct genre
231: {
232: int g_ordno; /* current index for table ptr */
233: int g_maxord; /* limit value for g_ordno */
234: int g_base; /* "base" value for s_no */
235: struct sym ***g_sptr; /* pointer to table for type - MWC DSC */
236: char *g_name;
237: };
238:
239: typedef union
240: {
241: struct sym *sptr;
242: int ival;
243: } YYSTYPE;
244:
245: extern struct sym **symtab; /* global symbol table */
246: extern struct sym **ntrmptr; /* non terminal pointers into symtab */
247: extern struct sym **trmptr; /* " terminal " " */
248: extern struct sym **typeptr; /* " type " " */
249: extern struct state *states;
250: extern struct sitem **items;
251: extern struct prod **prdptr;
252: extern int yyline;
253: extern nerrors;
254: extern FILE *defin, *tabout, *actout, *listout, *optout, *fhdr;
255: extern int tno;
256: extern char *gramy; /* input file name */
257: extern char *ytabc; /* y.tab.c output file */
258: extern char *youtput; /* listing file */
259: extern char *ytabh; /* header file for token # definers */
260: extern char acttmp[], opttmp[];
261: extern char *parser;
262: extern char bounderr[];
263: extern struct sitem *nititem;
264: extern struct prod *nitprod;
265: extern verbose, yydebug;
266: extern pstat;
267: extern int nstates;
268: extern int nprod;
269: extern int maxstates;
270: extern int maxprod;
271: extern int nrrconf, nsrconf;
272: extern int ndupgos, ndupacts;
273: extern struct genre gtab[MAXT];
274: extern int startsym;
275: extern int predlev;
276: extern struct resv restab[];
277: extern int maxitem, maxprodl, maxreds;
278:
279: char *calloc();
280: char *yalloc();
281: char *ptosym();
282: char *prsym();
283: char *nextarg();
284: struct ntgo *findnt();
285: struct lset *getset();
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.