-
Notifications
You must be signed in to change notification settings - Fork 1
/
struct.h
256 lines (242 loc) · 10.6 KB
/
struct.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
/*+@@file@@----------------------------------------------------------------*//*!
\file struct.h
\par Description
Principal data structures for the LEMON parser generator.
\par Status:
\par Project:
Lemon parser
\date Created on Sat Sep 1 16:21:49 2018
\date Modified on Sat Sep 1 16:21:49 2018
\author
\*//*-@@file@@----------------------------------------------------------------*/
#ifndef STRUCT_H__
#define STRUCT_H__
#pragma once
typedef enum
{
LEMON_FALSE = 0,
LEMON_TRUE
} Boolean;
/*!
* Symbol type enumeration
*/
enum symbol_type
{
TERMINAL,
NONTERMINAL,
MULTITERMINAL
};
/*!
* Symbol association type enum.
*/
enum e_assoc
{
LEFT,
RIGHT,
NONE,
UNK
};
/*!
* This structure stores grammar symbols,
* terminals and nonterminals
*/
struct symbol
{
const char *name; /*!< Name of the symbol */
int index; /*!< Index number for this symbol */
enum symbol_type type; /*!< Symbols are all either TERMINALS or NTs */
struct rule *rule; /*!< Linked list of rules of this (if an NT) */
struct symbol *fallback; /*!< fallback token in case this token doesn't parse */
int prec; /*!< Precedence if defined (-1 otherwise) */
enum e_assoc assoc; /*!< Associativity if precedence is defined */
char *firstset; /*!< First-set for all rules of this symbol */
Boolean lambda; /*!< True if NT and can generate an empty string */
int useCnt; /*!< Number of times used */
char *destructor; /*!< Code which executes whenever this symbol is
popped from the stack during error processing */
int destLineno; /*!< Line number for start of destructor.\n
Set to -1 for duplicate destructors. */
char *datatype; /*!< The data type of information held by this object.\n
Only used if type==NONTERMINAL */
int dtnum; /*!< The data type number.
In the parser, the value stack is a union.\n
The .yy%d element of this union is the correct data
type for this object */
int bContent; /*!< True if this symbol ever carries content - if it is
ever more than just syntax */
/*! The following fields are used by MULTITERMINALs only */
int nsubsym; /*!< Number of constituent symbols in the MULTI */
struct symbol **subsym; /*!< Array of constituent symbols */
};
/*!
* This structure stores all production rule in the grammar
*/
struct rule
{
struct symbol *lhs; /*!< Left-hand side of the rule */
const char *lhsalias; /*!< Alias for the LHS (NULL if none) */
int lhsStart; /*!< True if left-hand side is the start symbol */
int ruleline; /*!< Line number for the rule */
int nrhs; /*!< Number of RHS symbols */
struct symbol **rhs; /*!< The RHS symbols */
const char **rhsalias; /*!< An alias for each RHS symbol (NULL if none) */
int line; /*!< Line number at which code begins */
const char *code; /*!< The code executed when this rule is reduced */
const char *codePrefix; /*!< Setup code before code[] above */
const char *codeSuffix; /*!< Breakdown code after code[] above */
int noCode; /*!< True if this rule has no associated C code */
int codeEmitted; /*!< True if the code has been emitted already */
struct symbol *precsym; /*!< Precedence symbol for this rule */
int index; /*!< An index number for this rule */
int iRule; /*!< Rule number as used in the generated tables */
Boolean canReduce; /*!< True if this rule is ever reduced */
Boolean doesReduce; /*!< Reduce actions occur after optimization */
struct rule *nextlhs; /*!< Next rule with the same LHS */
struct rule *next; /*!< Next rule in the global list */
};
/*!
* A configuration is a production rule of the grammar together with
* a mark (dot) showing how much of that rule has been processed so far.\n
* Configurations also contain a follow-set which is a list of terminal
* symbols which are allowed to immediately follow the end of the rule.\n
*/
/*!
* Status of each configuration.
*/
enum cfgstatus
{
COMPLETE,
INCOMPLETE
};
/*!
* Records an instance of each configuration.
*/
struct config
{
struct rule *rp; /*!< The rule upon which the configuration is based */
int dot; /*!< The parse point */
char *fws; /*!< Follow-set for this configuration only */
struct plink *fplp; /*!< Follow-set forward propagation links */
struct plink *bplp; /*!< Follow-set backwards propagation links */
struct state *stp; /*!< Pointer to state which contains this */
enum cfgstatus status; /*!< used during followset and shift computations */
struct config *next; /*!< Next configuration in the state */
struct config *bp; /*!< The next basis configuration */
};
/*!
* Action type
*/
enum e_action
{
SHIFT,
ACCEPT,
REDUCE,
ERROR,
SSCONFLICT, /*!< A shift/shift conflict */
SRCONFLICT, /*!< Was a reduce, but part of a conflict */
RRCONFLICT, /*!< Was a reduce, but part of a conflict */
SH_RESOLVED, /*!< Was a shift. Precedence resolved conflict */
RD_RESOLVED, /*!< Was reduce. Precedence resolved conflict */
NOT_USED, /*!< Deleted by compression */
SHIFTREDUCE /*!< Shift first, then reduce */
};
/*!
* Stores every shift or reduce operation
*/
struct action
{
struct symbol *sp; /*!< The look-ahead symbol */
enum e_action type;
union
{
struct state *stp; /*!< The new state, if a shift */
struct rule *rp; /*!< The rule, if a reduce */
} x;
struct symbol *spOpt; /*!< SHIFTREDUCE optimization to this symbol */
struct action *next; /*!< Next action for this state */
struct action *collide; /*!< Next action with the same hash */
};
/*!
* This structure encodes all instances of each state of the generated parser's
* finite state machine.
*/
struct state
{
struct config *bp; /*!< The basis configurations for this state */
struct config *cfp; /*!< All configurations in this set */
int statenum; /*!< Sequential number for this state */
struct action *ap; /*!< List of actions for this state */
int nTknAct, nNtAct; /*!< Number of actions on terminals and nonterminals */
int iTknOfst, iNtOfst; /*!< yy_action[] offset for terminals and nonterms */
int iDfltReduce; /*!< Default action is to REDUCE by this rule */
struct rule *pDfltReduce; /*!< The default REDUCE rule. */
int autoReduce; /*!< True if this is an auto-reduce state */
};
#define NO_OFFSET (-2147483647)
/*!
* A followset propagation link indicates that the contents of one
* configuration followset should be propagated to another whenever
* the first changes.
*/
struct plink
{
struct config *cfp; /*!< The configuration to which linked */
struct plink *next; /*!< The next propagate link */
};
/*!
* The state vector for the entire parser generator is hold in this
* structure.\n
* LEMON uses no global variables and makes little use of static
* variables.\n
* Fields in the following structure can be thought of as begin
* global variables in the program.
*/
struct lemon
{
struct state **sorted; /*!< Table of states sorted by state number */
struct rule *rule; /*!< List of all rules */
struct rule *startRule; /*!< First rule */
int nstate; /*!< Number of states */
int nxstate; /*!< nstate with tail degenerate states removed */
int nrule; /*!< Number of rules */
int nsymbol; /*!< Number of terminal and nonterminal symbols */
int nterminal; /*!< Number of terminal symbols */
int minShiftReduce; /*!< Minimum shift-reduce action value */
int errAction; /*!< Error action value */
int accAction; /*!< Accept action value */
int noAction; /*!< No-op action value */
int minReduce; /*!< Minimum reduce action */
int maxAction; /*!< Maximum action value of any kind */
struct symbol **symbols; /*!< Sorted array of pointers to symbols */
int errorcnt; /*!< Number of errors */
struct symbol *errsym; /*!< The error symbol */
struct symbol *wildcard; /*!< Token that matches anything */
char *name; /*!< Name of the generated parser */
char *arg; /*!< Declaration of the 3th argument to parser */
char *ctx; /*!< Declaration of 2nd argument to constructor */
char *tokentype; /*!< Type of terminal symbols in the parser stack */
char *vartype; /*!< The default type of non-terminal symbols */
char *start; /*!< Name of the start symbol for the grammar */
char *stacksize; /*!< Size of the parser stack */
char *include; /*!< Code to put at the start of the C file */
char *error; /*!< Code to execute when an error is seen */
char *overflow; /*!< Code to execute on a stack overflow */
char *failure; /*!< Code to execute on parser failure */
char *accept; /*!< Code to execute when the parser excepts */
char *extracode; /*!< Code appended to the generated file */
char *tokendest; /*!< Code to execute to destroy token data */
char *vardest; /*!< Code for the default non-terminal destructor */
char *filename; /*!< Name of the input file */
char *outname; /*!< Name of the current output file */
char *tokenprefix; /*!< A prefix added to token names in the .h file */
int nconflict; /*!< Number of parsing conflicts */
int nactiontab; /*!< Number of entries in the yy_action[] table */
int nlookaheadtab; /*!< Number of entries in yy_lookahead[] */
int tablesize; /*!< Total table size of all tables in bytes */
int basisflag; /*!< Print only basis configurations */
int emitenumflag; /*!< Emit an enum for terminal tokens. */
int has_fallback; /*!< True if any %fallback is seen in the grammar */
int nolinenosflag; /*!< True if #line statements should not be printed */
char *argv0; /*!< Name of the program */
};
#endif //STRUCT_H__