yygenp.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*++
  2. Copyright (c) 2016 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. yygenp.h
  9. Abstract:
  10. This header contains internal definitions for the grammar generator
  11. library.
  12. Author:
  13. Evan Green 9-Apr-2016
  14. --*/
  15. //
  16. // ------------------------------------------------------------------- Includes
  17. //
  18. #include <stdio.h>
  19. #include <minoca/lib/types.h>
  20. #include <minoca/lib/status.h>
  21. #include <minoca/lib/yy.h>
  22. #include <minoca/lib/yygen.h>
  23. //
  24. // ---------------------------------------------------------------- Definitions
  25. //
  26. #define YYGEN_BITS_PER_WORD (sizeof(ULONG) * BITS_PER_BYTE)
  27. //
  28. // This macro computes the number of words needed to accomodate a bitmap that
  29. // holds at least the given number of bits.
  30. //
  31. #define YYGEN_BITMAP_WORD_COUNT(_Bits) \
  32. (((_Bits) + (YYGEN_BITS_PER_WORD - 1)) / YYGEN_BITS_PER_WORD)
  33. //
  34. // This macro sets a bit in the bitmap.
  35. //
  36. #define YYGEN_BITMAP_SET(_Row, _Bit) \
  37. (((PULONG)(_Row))[(_Bit) / YYGEN_BITS_PER_WORD] |= \
  38. 1 << ((_Bit) & (YYGEN_BITS_PER_WORD - 1)))
  39. //
  40. // This macro evaluates to non-zero if the given bit is set in the bitmap.
  41. //
  42. #define YYGEN_BITMAP_IS_SET(_Row, _Bit) \
  43. ((((PULONG)(_Row))[(_Bit) / YYGEN_BITS_PER_WORD] & \
  44. (1 << ((_Bit) & (YYGEN_BITS_PER_WORD - 1)))) != 0)
  45. //
  46. // ------------------------------------------------------ Data Type Definitions
  47. //
  48. typedef enum _YY_ACTION_CODE {
  49. YyActionInvalid,
  50. YyActionShift,
  51. YyActionReduce
  52. } YY_ACTION_CODE, *PYY_ACTION_CODE;
  53. typedef enum _YYGEN_SUPPRESSION {
  54. YyNotSuppressed,
  55. YySuppressedNoisily,
  56. YySuppressedQuietly
  57. } YYGEN_SUPPRESSION, *PYYGEN_SUPPRESSION;
  58. typedef YY_VALUE YY_RULE_INDEX, *PYY_RULE_INDEX;
  59. typedef YY_VALUE YY_ITEM_INDEX, *PYY_ITEM_INDEX;
  60. typedef YY_VALUE YY_STATE_INDEX, *PYY_STATE_INDEX;
  61. typedef YY_VALUE YY_GOTO_INDEX, *PYY_GOTO_INDEX;
  62. typedef YY_VALUE YY_ACTION_INDEX, *PYY_ACTION_INDEX;
  63. typedef struct _YYGEN_STATE YYGEN_STATE, *PYYGEN_STATE;
  64. typedef struct _YYGEN_SHIFTS YYGEN_SHIFTS, *PYYGEN_SHIFTS;
  65. typedef struct _YYGEN_REDUCTIONS YYGEN_REDUCTIONS, *PYYGEN_REDUCTIONS;
  66. typedef struct _YYGEN_ACTION YYGEN_ACTION, *PYYGEN_ACTION;
  67. /*++
  68. Structure Description:
  69. This structure defines an individual grammar rule.
  70. Members:
  71. LeftSide - Stores the left side of the rule.
  72. RightSide - Stores an index into the items array where the right side of
  73. this rule resides.
  74. Precedence - Stores the precedence for the rule.
  75. Associativity - Stores an associativity for the rule.
  76. Used - Stores a boolean indicating whether or not the rules were used.
  77. --*/
  78. typedef struct _YYGEN_RULE {
  79. YY_VALUE LeftSide;
  80. YY_ITEM_INDEX RightSide;
  81. ULONG Precedence;
  82. YY_ASSOCIATIVITY Associativity;
  83. BOOL Used;
  84. } YYGEN_RULE, *PYYGEN_RULE;
  85. /*++
  86. Structure Description:
  87. This structure contains the core state structure of the LR(0) state machine.
  88. Members:
  89. Next - Stores a pointer to the next state globally in the state machine.
  90. Link - Stores a pointer to next element in this bucket in the hash table of
  91. states.
  92. Number - Stores the state number.
  93. AccessingSymbol - Stores the shift symbol that causes entrance into this
  94. state.
  95. ItemCount - Stores the number of elements in the items array.
  96. Items - Stores an array of indices into the item array representing the
  97. right hand sides of all the rules in this state.
  98. --*/
  99. struct _YYGEN_STATE {
  100. PYYGEN_STATE Next;
  101. PYYGEN_STATE Link;
  102. YY_STATE_INDEX Number;
  103. YY_VALUE AccessingSymbol;
  104. YY_VALUE ItemsCount;
  105. PYY_ITEM_INDEX Items;
  106. };
  107. /*++
  108. Structure Description:
  109. This structure contains the set of reductions for a state in the LR(0)
  110. state machine.
  111. Members:
  112. Next - Stores a pointer to the next set of reductions globally in the state
  113. machine.
  114. Number - Stores the state number these reductions correspond to.
  115. Count - Stores the number of elements in the rules array.
  116. Rules - Stores the set of rules that reduce in this state.
  117. --*/
  118. struct _YYGEN_REDUCTIONS {
  119. PYYGEN_REDUCTIONS Next;
  120. YY_STATE_INDEX Number;
  121. YY_VALUE Count;
  122. PYY_RULE_INDEX Rules;
  123. };
  124. /*++
  125. Structure Description:
  126. This structure descibes the set of shifts out of a given state.
  127. Members:
  128. Next - Stores a pointer to the next set of shifts in the state machine.
  129. Number - Stores the state number of the shifts.
  130. Count - Stores the number of shift states in the array.
  131. States - Stores an array of state numbers to possible next states, sorted.
  132. --*/
  133. struct _YYGEN_SHIFTS {
  134. PYYGEN_SHIFTS Next;
  135. YY_STATE_INDEX Number;
  136. YY_VALUE Count;
  137. PYY_STATE_INDEX States;
  138. };
  139. /*++
  140. Structure Description:
  141. This structure descibes a parser action.
  142. Members:
  143. Next - Stores a pointer to the next action.
  144. Symbol - Stores the action symbol.
  145. Number - Stores the action index.
  146. Precedence - Stores the action precedence.
  147. Associativity - Stores the associativity of the action.
  148. Code - Stores the action type: shift or reduce.
  149. Suppression - Stores the suppression state of this action.
  150. --*/
  151. struct _YYGEN_ACTION {
  152. PYYGEN_ACTION Next;
  153. YY_VALUE Symbol;
  154. YY_ACTION_INDEX Number;
  155. YY_VALUE Precedence;
  156. YY_ASSOCIATIVITY Associativity;
  157. YY_ACTION_CODE Code;
  158. YYGEN_SUPPRESSION Suppression;
  159. };
  160. /*++
  161. Structure Description:
  162. This structure contains the working state for the grammar generator.
  163. Members:
  164. Flags - Stores the global flags. See YYGEN_FLAG_* definitions.
  165. Elements - Stores the array of elements.
  166. VariablePrefix - Stores the prefix to prepend to all the variable names.
  167. OutputFileName - Stores the name of the output file, which is printed in
  168. the output source.
  169. TokenCount - Stores the first invalid token number. Any value below this is
  170. assumed to be a token.
  171. SymbolCount - Stores the number of tokens plus non-terminals.
  172. NonTerminalCount - Stores the number of non-terminals, including the
  173. start symbol.
  174. StartSymbol - Stores the starting symbol of the grammar.
  175. ItemCount - Stores the total count of all the elements in all the rules.
  176. RuleCount - Stores the number of rules.
  177. Nullable - Stores an array of booleans indexed by symbol that indicates
  178. if that production is empty.
  179. Items - Stores an array of all right sides of all rules. This establishes
  180. a total order of item sets by rule. The arrays are also terminated by
  181. a negative number, the rule index.
  182. Rules - Stores an array of rules.
  183. Derives - Stores an array of pointers to the set of rules for each left
  184. hand side. That is, these are indices into the rules array for each
  185. production. This is index by symbol.
  186. ItemSet - Stores the item set for the state currently being built.
  187. ItemSetEnd - Stores the end of the current item set.
  188. RuleSet - Stores a bitmap of the rule set.
  189. FirstDerives - Stores an array of bitmaps. Each column represents a rule,
  190. and the rows are the non-terminals. The bitmap describes the rules
  191. in the FIRST set for each production. The FIRST set is the set of
  192. non-terminals that can appear first for any production.
  193. FirstState - Stores a pointer to the first state in the LR(0) state machine.
  194. StateCount - Stores the number of states in the LR(0) state machine.
  195. FirstReduction - Stores the head of the singly linked list of reductions.
  196. LastReduction - Stores the tail of the singly linked list of reductions.
  197. FirstShift - Stores the head of the singly linked list of shifts.
  198. LastShift - Stores the tail of the singly linked list of shifts.
  199. StateTable - Stores a pointer to the final states.
  200. AccessingSymbol - Stores an array of shift symbols that cause entrance to
  201. the state at each index.
  202. ShiftTable - Stores an array of shifts to other states, indexed by starting
  203. state.
  204. ReductionTable - Stores an array of reductions at each state.
  205. Lookaheads - Stores the array of indices into the lookahead sets, indexed
  206. by state. That is, for a given state, the element in this array shows
  207. where in the lookahead sets array to begin.
  208. LookaheadSets - Stores an array of token (terminal) bitmaps showing the
  209. lookaheads for every reduction in every state.
  210. LookaheadRule - Stores an array that runs parallel to the lookahead sets
  211. pointing back to a rule for the array index.
  212. GotoMap - Stores an array of indices into the FromState/ToState arrays
  213. where the gotos using a particular non-terminal symbol start. That is,
  214. given a symbol (minus the number of tokens), this array shows the index
  215. to start looking in the FromState/ToState arrays for gotos using this
  216. symbol.
  217. FromState - Stores the array of starting states for all the gotos.
  218. ToState - Stores the array of destination goto states, running parallel to
  219. the FromState array.
  220. Parser - Stores the combined action table.
  221. FinalState - Stores the state index of the "accept" state.
  222. UnusedRules - Stores the count of unused rules.
  223. ShiftReduceConflicts - Stores an array of counts of shift-reduce conflicts
  224. for each state.
  225. ReduceReduceConflicts - Stores an array of counts of reduce-reduce
  226. conflicts for each state.
  227. ShiftReduceConflictCount - Stores the total number of shift-reduce
  228. conflicts.
  229. ReduceReduceConflictCount - Stores the total number of reduce-reduce
  230. conflicts.
  231. ExpectedShiftReduceConflicts - Stores the expected number of shift-reduce
  232. conflicts.
  233. ExpectedReduceReduceConflicts - Stores the expected number of
  234. reduce-reduce conflicts.
  235. DefaultReductions - Stores the table of rules to reduce by, indexed by
  236. state.
  237. --*/
  238. struct _YYGEN_CONTEXT {
  239. ULONG Flags;
  240. PYY_ELEMENT Elements;
  241. PSTR VariablePrefix;
  242. PSTR OutputFileName;
  243. YY_VALUE TokenCount;
  244. YY_VALUE SymbolCount;
  245. YY_VALUE NonTerminalCount;
  246. YY_VALUE StartSymbol;
  247. ULONG ItemCount;
  248. ULONG RuleCount;
  249. PBOOL Nullable;
  250. PYY_VALUE Items;
  251. PYYGEN_RULE Rules;
  252. PYY_RULE_INDEX Derives;
  253. PYY_ITEM_INDEX ItemSet;
  254. PYY_ITEM_INDEX ItemSetEnd;
  255. PULONG RuleSet;
  256. PULONG FirstDerives;
  257. PYYGEN_STATE FirstState;
  258. YY_STATE_INDEX StateCount;
  259. PYYGEN_REDUCTIONS FirstReduction;
  260. PYYGEN_REDUCTIONS LastReduction;
  261. PYYGEN_SHIFTS FirstShift;
  262. PYYGEN_SHIFTS LastShift;
  263. PYYGEN_STATE *StateTable;
  264. PYY_VALUE AccessingSymbol;
  265. PYYGEN_SHIFTS *ShiftTable;
  266. PYYGEN_REDUCTIONS *ReductionTable;
  267. PYY_VALUE Lookaheads;
  268. PULONG LookaheadSets;
  269. PYY_RULE_INDEX LookaheadRule;
  270. PYY_GOTO_INDEX GotoMap;
  271. PYY_STATE_INDEX FromState;
  272. PYY_STATE_INDEX ToState;
  273. PYYGEN_ACTION *Parser;
  274. YY_STATE_INDEX FinalState;
  275. ULONG UnusedRules;
  276. PYY_VALUE ShiftReduceConflicts;
  277. PYY_VALUE ReduceReduceConflicts;
  278. YY_VALUE ShiftReduceConflictCount;
  279. YY_VALUE ReduceReduceConflictCount;
  280. YY_VALUE ExpectedShiftReduceConflicts;
  281. YY_VALUE ExpectedReduceReduceConflicts;
  282. PYY_RULE_INDEX DefaultReductions;
  283. };
  284. //
  285. // -------------------------------------------------------------------- Globals
  286. //
  287. //
  288. // -------------------------------------------------------- Function Prototypes
  289. //
  290. YY_STATUS
  291. YypGenerateLr0Grammar (
  292. PYYGEN_CONTEXT Context
  293. );
  294. /*++
  295. Routine Description:
  296. This routine generates an LR(0) grammar based on the description.
  297. Arguments:
  298. Context - Supplies a pointer to the generator context.
  299. Return Value:
  300. YY status.
  301. --*/
  302. VOID
  303. YypEstablishClosure (
  304. PYYGEN_CONTEXT Context,
  305. PYYGEN_STATE State
  306. );
  307. /*++
  308. Routine Description:
  309. This routine creates a closure on the itemset of the current state.
  310. Arguments:
  311. Context - Supplies a pointer to the generator context.
  312. State - Supplies a pointer to the state to create the closure of.
  313. Return Value:
  314. None.
  315. --*/
  316. YY_STATUS
  317. YypGenerateLalr (
  318. PYYGEN_CONTEXT Context
  319. );
  320. /*++
  321. Routine Description:
  322. This routine generates an LALR(1) state machine based on an LR(0) state
  323. machine.
  324. Arguments:
  325. Context - Supplies a pointer to the generator context.
  326. Return Value:
  327. YY status.
  328. --*/
  329. YY_STATUS
  330. YypBuildParser (
  331. PYYGEN_CONTEXT Context
  332. );
  333. /*++
  334. Routine Description:
  335. This routine generates the parser data structures based on the LALR(1)
  336. construction.
  337. Arguments:
  338. Context - Supplies a pointer to the generator context.
  339. Return Value:
  340. YY status.
  341. --*/
  342. PVOID
  343. YypAllocate (
  344. UINTN Size
  345. );
  346. /*++
  347. Routine Description:
  348. This routine allocates and clears a region of memory.
  349. Arguments:
  350. Size - Supplies the size in bytes of the allocation.
  351. Return Value:
  352. Returns a pointer to the new memory on success.
  353. NULL on failure.
  354. --*/
  355. PVOID
  356. YypReallocate (
  357. PVOID Allocation,
  358. UINTN NewSize
  359. );
  360. /*++
  361. Routine Description:
  362. This routine reallocates previously allocated memory.
  363. Arguments:
  364. Allocation - Supplies an optional pointer to the memory to reallocate.
  365. NewSize - Supplies the desired size of the allocation.
  366. Return Value:
  367. Returns a pointer to the newly sized buffer on success. This might be the
  368. same buffer passed in or a new buffer. The old buffer will be invalid
  369. after this.
  370. NULL on reallocation failure. The old buffer will remain valid and still
  371. needs to be freed.
  372. --*/
  373. VOID
  374. YypFree (
  375. PVOID Allocation
  376. );
  377. /*++
  378. Routine Description:
  379. This routine frees a previously allocated region of memory.
  380. Arguments:
  381. Allocation - Supplies a pointer to the allocation to free.
  382. Return Value:
  383. None.
  384. --*/