librdesc
Loading...
Searching...
No Matches
boolean_algebra.h
Go to the documentation of this file.
1
20#ifndef BALG_H
21#define BALG_H
22
23#include "../../include/grammar.h"
24#include "../../include/rule_macros.h"
25
26
31#define BALG_PRODUCTION_COUNT 17
32
38#define BALG_MAX_ALTERNATIVE_COUNT 5
39
45#define BALG_MAX_ALTERNATIVE_SIZE 4
46
52enum balg_tk {
53 /* Literals */
54 TK_TRUE, TK_FALSE, TK_IDENT,
55 /* Operators */
56 TK_PIPE, TK_AMP, TK_EXCL,
57 /* Punctuation */
58 TK_LPAREN, TK_RPAREN, TK_LCURLY, TK_RCURLY,
59 TK_EQ, TK_COMMA, TK_SEMI,
60};
61
63enum balg_nt {
64 NT_BIT, NT_IDENT, NT_CALL,
65 NT_CALL_OPTPARAMS,
66
67 /* Expression hierarchy (precedence) */
68 NT_EXPR, NT_EXPR_REST, // level 1: OR
69 NT_TERM, NT_TERM_REST, // level 2: AND
70 NT_FACTOR, NT_ATOM, // level 3: NOT and Atoms
71
72 /* Statements */
73 NT_STMT, NT_STMTS, NT_ASGN,
74
75 /* List helpers */
76 NT_IDENT_LS, NT_IDENT_LS_REST,
77 NT_EXPR_LS, NT_EXPR_LS_REST,
78};
79
81const char *const balg_tk_names[] = {
82 "1", "0", "@ident",
83 "|", "&", "!",
84 "(", ")", "{", "}",
85 "=", ",", ";",
86};
87
92const char *const balg_tk_names_escaped[] = {
93 "1", "0", "@ident",
94 "\\|", "&", "!",
95 "(", ")", "\\{", "\\}",
96 "=", ",", ";",
97};
98
100const char *const balg_nt_names[] = {
101 "bit", "ident", "call",
102 "call_optparams",
103
104 "expr", "expr_rest",
105 "term", "term_rest",
106 "factor", "atom",
107
108 "stmt", "stmts", "asgn",
109
110 "ident_ls", "ident_ls_rest",
111 "expr_ls", "expr_ls_rest",
112};
113
128static const struct rdesc_grammar_symbol
130 [BALG_MAX_ALTERNATIVE_COUNT + 1 /* +1 for end of production sentinel */]
131 [BALG_MAX_ALTERNATIVE_SIZE + 1 /* +1 for end of alternative sentinel */] = {
132 /* <bit> ::= */ r(
133 TK(TRUE)
134 alt TK(FALSE)
135 ),
136 /* <ident> ::= */ r(
137 TK(IDENT)
138 ),
139 /* <call> ::= */ r(
140 TK(IDENT), TK(LPAREN), NT(CALL_OPTPARAMS), TK(RPAREN)
141 ),
142 /* <call_optparams> ::= */
143 ropt(NT(EXPR_LS)),
144
145 /* <expr> ::= */
146 rrr(EXPR, (NT(TERM)), (TK(PIPE), NT(TERM))),
147
148 /* <term> ::= */
149 rrr(TERM, (NT(FACTOR)), (TK(AMP), NT(FACTOR))),
150 /* <factor> ::= */ r(
151 TK(EXCL), NT(ATOM)
152 alt NT(ATOM)
153 ),
154
155 /* Intentional ambiguity for stress testing. The grammar has two rules
156 * starting with '(':
157 * "(" <expr> ")" Parenthesized expression
158 * "(" asgn ")" Parenthesized assignment
159 *
160 * The parser must consume "(", try to parse <expr>, and if it fails
161 * (e.g., it encounters an "="), it must backtrack, restore the "(",
162 * and try parsing <asgn>. */
163 /* <atom> ::= */ r(
164 TK(LPAREN), NT(EXPR), TK(RPAREN)
165 alt TK(LPAREN), NT(ASGN), TK(RPAREN) // <-- Ambiguity trigger
166 alt NT(BIT)
167 alt NT(IDENT)
168 alt NT(CALL)
169 ),
170
171 /* <stmt> ::= */ r(
172 TK(SEMI)
173 alt NT(CALL), TK(SEMI)
174 alt NT(ASGN), TK(SEMI)
175 alt TK(LCURLY), NT(STMTS), TK(RCURLY)
176 ),
177
178 /* <stmts> ::= */ ropt(NT(STMT), NT(STMTS)),
179
180 /* <asgn> */ r(
181 NT(IDENT_LS), TK(EQ), NT(EXPR_LS)
182 ),
183
184 /* <ident_ls> ::= */
185 rrr(IDENT_LS, (NT(IDENT)), (TK(COMMA), NT(IDENT))),
186
187 /* <expr_ls> ::= */
188 rrr(EXPR_LS, (NT(EXPR)), (TK(COMMA), NT(EXPR)))
189};
190
191
192#endif
const char *const balg_tk_names[]
Names of tokens that are used in BNF.
Definition boolean_algebra.h:81
static const struct rdesc_grammar_symbol balg[BALG_PRODUCTION_COUNT][BALG_MAX_ALTERNATIVE_COUNT+1][BALG_MAX_ALTERNATIVE_SIZE+1]
Sample grammar.
Definition boolean_algebra.h:131
const char *const balg_nt_names[]
Nonterminal names (for debugging/printing CST).
Definition boolean_algebra.h:100
const char *const balg_tk_names_escaped[]
Names of tokens that can be used in dotlang graph (special chars are escaped).
Definition boolean_algebra.h:92
balg_nt
Nonterminal symbols.
Definition boolean_algebra.h:63
balg_tk
Terminal symbols (tokens).
Definition boolean_algebra.h:52
#define BALG_MAX_ALTERNATIVE_COUNT
Maximum number of alternatives for a single production and +1 for end of production sentinel....
Definition boolean_algebra.h:38
#define BALG_MAX_ALTERNATIVE_SIZE
Maximum number of symbols in an alternative (Right-Hand Side) and +1 for end-of-body sentinel....
Definition boolean_algebra.h:45
#define BALG_PRODUCTION_COUNT
Total count of nonterminal symbols defined in enum balg_nt. Determines the size of the first dimensio...
Definition boolean_algebra.h:31
#define rrr(head, base, suffix)
Defines right-recursive list rules. base and suffix parameters should wrapped with parenthesis (()).
Definition rule_macros.h:121
#define NT(nt)
Macro to create a nonterminal production symbol.
Definition rule_macros.h:58
#define ropt(...)
Macro to define an optional grammar rule (epsilon production).
Definition rule_macros.h:91
#define TK(tk)
Macro to create a terminal (token) production symbol.
Definition rule_macros.h:56
#define r(...)
Macro to define a grammar rule. Adds end of alternative and end of production body sentinels to gramm...
Definition rule_macros.h:72
#define alt
Separates grammar alternatives (alternative separator).
Definition rule_macros.h:133
A terminal or nonterminal representing the body (right side) of a production rule.
Definition grammar.h:87