Lime Parser Generator 0.1.0
Runtime-extensible LALR(1) parser with SIMD tokenization and LLVM JIT
Loading...
Searching...
No Matches
glr.h
1#ifndef LIME_GLR_H
2#define LIME_GLR_H
3
4#include <stdint.h>
5#include <stdbool.h>
6#include <stddef.h>
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
12/* Forward declarations */
13typedef struct LimeArena LimeArena;
14
22typedef struct GSSNode {
23 uint32_t state;
25 union { int ival; void *pval; double dval; } value;
27 uint32_t npred;
28 uint32_t pred_capacity;
29 uint32_t refcount;
30#ifdef YYLOCATIONTYPE
32 struct { uint32_t first_line, first_column, last_line, last_column; } location;
33#endif
34} GSSNode;
35
43typedef int (*GLRDisambiguateFn)(uint32_t rule1_index, uint32_t rule2_index,
44 void *user_data);
45
52typedef struct GLRParser {
54 uint32_t nheads;
55 uint32_t max_heads;
57 GLRDisambiguateFn disambiguate;
59 uint32_t total_forks;
60 uint32_t total_merges;
62} GLRParser;
63
64/* Create a GLR parser. initial_state is the LALR start state (typically 0).
65** arena_size controls the initial arena block size (0 for default). */
66GLRParser *glr_parser_create(uint32_t initial_state, size_t arena_size);
67
68/* Destroy a GLR parser and free all resources. */
69void glr_parser_destroy(GLRParser *parser);
70
71/*
72** Feed a token to the GLR parser.
73**
74** For each active stack head, the action table is consulted. If a
75** shift/reduce conflict exists (both actions are valid), the head is
76** forked: one copy shifts, the other reduces. After processing all
77** heads, heads that arrived at the same state are merged.
78**
79** The action table arrays are the same ones generated by Lime into
80** the parser source file.
81**
82** Parameters:
83** parser - The GLR parser context
84** token - The terminal symbol code
85** yy_action - The generated action table
86** action_count - Number of entries in yy_action
87** yy_lookahead - The generated lookahead verification table
88** lookahead_count - Number of entries in yy_lookahead
89** yy_shift_ofst - Per-state shift offset into yy_action
90** yy_reduce_ofst - Per-state reduce offset into yy_action (for goto)
91** yy_default - Per-state default action
92** nstate - Total number of parser states
93** yy_rule_lhs - LHS nonterminal for each rule (yyRuleInfoLhs)
94** yy_rule_nrhs - Negative RHS length for each rule (yyRuleInfoNRhs)
95** nrule - Number of rules
96** min_shiftreduce - YY_MIN_SHIFTREDUCE constant
97** max_shiftreduce - YY_MAX_SHIFTREDUCE constant
98** min_reduce - YY_MIN_REDUCE constant
99** max_reduce - YY_MAX_REDUCE constant
100** error_action - YY_ERROR_ACTION constant
101** accept_action - YY_ACCEPT_ACTION constant
102** no_action - YY_NO_ACTION constant
103**
104** Returns 0 on success, -1 on unresolvable ambiguity, -2 on all-heads-dead.
105*/
106int glr_parser_feed(GLRParser *parser, uint16_t token,
107 const uint16_t *yy_action, uint32_t action_count,
108 const uint16_t *yy_lookahead, uint32_t lookahead_count,
109 const int16_t *yy_shift_ofst,
110 const int16_t *yy_reduce_ofst,
111 const uint16_t *yy_default,
112 uint32_t nstate,
113 const uint16_t *yy_rule_lhs,
114 const int8_t *yy_rule_nrhs,
115 uint32_t nrule,
116 uint16_t min_shiftreduce,
117 uint16_t max_shiftreduce,
118 uint16_t min_reduce,
119 uint16_t max_reduce,
120 uint16_t error_action,
121 uint16_t accept_action,
122 uint16_t no_action);
123
124/* Set the disambiguation callback. */
125void glr_parser_set_disambiguate(GLRParser *parser,
126 GLRDisambiguateFn fn, void *user_data);
127
128/* Return number of active parse heads (1 for unambiguous, >1 for ambiguous). */
129uint32_t glr_parser_head_count(const GLRParser *parser);
130
131/* Check if the parse completed successfully (single head in accept state). */
132bool glr_parser_accepted(const GLRParser *parser, uint16_t accept_action);
133
134/* GSSNode management */
135GSSNode *gss_node_create(LimeArena *arena, uint32_t state);
136void gss_node_add_predecessor(GSSNode *node, GSSNode *pred);
137GSSNode *gss_node_acquire(GSSNode *node);
138void gss_node_release(GSSNode *node);
139
140#ifdef __cplusplus
141}
142#endif
143
144#endif /* LIME_GLR_H */
GLR parser context, wrapping an underlying LALR(1) parser.
Definition glr.h:52
bool has_ambiguity
True if unresolved ambiguity detected.
Definition glr.h:61
void * disambiguate_data
User data for callback.
Definition glr.h:58
LimeArena * arena
Arena for GSSNode allocation.
Definition glr.h:56
uint32_t total_merges
Statistics: total merges.
Definition glr.h:60
uint32_t nheads
Number of active heads.
Definition glr.h:54
uint32_t max_heads
Capacity of active_heads.
Definition glr.h:55
GSSNode ** active_heads
Array of active stack top nodes.
Definition glr.h:53
GLRDisambiguateFn disambiguate
User disambiguation callback.
Definition glr.h:57
uint32_t total_forks
Statistics: total forks.
Definition glr.h:59
Graph-Structured Stack (GSS) node for GLR parsing.
Definition glr.h:22
uint32_t npred
Number of predecessors.
Definition glr.h:27
uint32_t refcount
Reference count.
Definition glr.h:29
struct GSSNode ** predecessors
Array of predecessor nodes.
Definition glr.h:26
uint32_t state
Parser state number.
Definition glr.h:23
uint32_t pred_capacity
Allocated predecessor slots.
Definition glr.h:28
union GSSNode::@0 value
Semantic value (one of int / pointer / double, by convention).
Arena allocator for AST nodes.
Definition lime_ast.h:17