librdesc
Loading...
Searching...
No Matches
boolean_algebra.h
Go to the documentation of this file.
1
19#ifndef BALG_H
20#define BALG_H
21
22#include "../../include/cfg.h"
23
25#define PREFIX_TK(tk) TK_ ## tk
27#define PREFIX_NT(nt) NT_ ## nt
28
29#include "../../include/bnf_dsl.h"
30
31
33#define BALG_TK_COUNT 14
34
39#define BALG_NT_COUNT 17
40
46#define BALG_NT_VARIANT_COUNT 6
47
53#define BALG_NT_BODY_LENGTH 5
54
56enum balg_tk {
57 TK_NOTOKEN,
58 /* Literals */
59 TK_TRUE, TK_FALSE, TK_IDENT,
60 /* Operators */
61 TK_PIPE, TK_AMP, TK_EXCL,
62 /* Punctuation */
63 TK_LPAREN, TK_RPAREN, TK_LCURLY, TK_RCURLY,
64 TK_EQ, TK_COMMA, TK_SEMI,
65};
66
68enum balg_nt {
69 NT_BIT, NT_IDENT, NT_CALL,
70 NT_CALL_OPTPARAMS,
71
72 /* Expression Hierarchy (Precedence) */
73 NT_EXPR, NT_EXPR_REST, // level 1: OR
74 NT_TERM, NT_TERM_REST, // level 2: AND
75 NT_FACTOR, NT_ATOM, // level 3: NOT and Atoms
76
77 /* statements */
78 NT_STMT, NT_STMTS, NT_ASGN,
79
80 /* list helpers */
81 NT_IDENT_LS, NT_IDENT_LS_REST,
82 NT_EXPR_LS, NT_EXPR_LS_REST,
83};
84
86const char balg_tks[BALG_TK_COUNT] = {
87 '\0',
88 '1', '0', 'w',
89 '|', '&', '!',
90 '(', ')', '{', '}',
91 '=', ',', ';',
92};
93
97const char *const balg_tk_names[BALG_TK_COUNT] = {
98 "\0",
99 "1", "0", "@ident",
100 "|", "&", "!",
101 "(", ")", "{", "}",
102 "=", ",", ";",
103};
104
110 "\0",
111 "1", "0", "@ident",
112 "\\|", "&", "!",
113 "(", ")", "\\{", "\\}",
114 "=", ",", ";",
115};
116
118const char *const balg_nt_names[BALG_NT_COUNT] = {
119 "bit", "ident", "call",
120 "call_optparams",
121
122 "expr", "expr_rest",
123 "term", "term_rest",
124 "factor", "atom",
125
126 "stmt", "stmts", "asgn",
127
128 "ident_ls", "ident_ls_rest",
129 "expr_ls", "expr_ls_rest",
130};
131
133static const struct rdesc_cfg_symbol
135 /* <bit> ::= */ r(
136 TK(TRUE),
137 alt TK(FALSE),
138 ),
139 /* <ident> ::= */ r(
140 TK(IDENT),
141 ),
142 /* <call> ::= */ r(
143 TK(IDENT), TK(LPAREN), NT(CALL_OPTPARAMS), TK(RPAREN),
144 ),
145 /* <call_optparams> ::= */
146 ropt(NT(EXPR_LS)),
147
148 /* <expr> ::= */
149 rrr(EXPR, NT(TERM), TK(PIPE)),
150
151 /* <term> ::= */
152 rrr(TERM, NT(FACTOR), TK(AMP)),
153 /* <factor> ::= */ r(
154 TK(EXCL), NT(ATOM),
155 alt NT(ATOM),
156 ),
157
158 /* Intentional ambiguity for stress testing. The grammar has two rules
159 * starting with '(':
160 * "(" <expr> ")" Parenthesized expression
161 * "(" asgn ")" Parenthesized assignment
162 *
163 * The parser must consume "(", try to parse <expr>, and if it fails
164 * (e.g., it encounters an "="), it must backtrack, restore the "(",
165 * and try parsing <asgn>. */
166 /* <atom> ::= */ r(
167 TK(LPAREN), NT(EXPR), TK(RPAREN),
168 alt TK(LPAREN), NT(ASGN), TK(RPAREN), // <-- Ambiguity trigger
169 alt NT(BIT),
170 alt NT(IDENT),
171 alt NT(CALL),
172 ),
173
174 /* <stmt> ::= */ r(
175 TK(SEMI),
176 alt NT(CALL), TK(SEMI),
177 alt NT(ASGN), TK(SEMI),
178 alt TK(LCURLY), NT(STMTS), TK(RCURLY),
179 ),
180
181 /* <stmts> ::= */ ropt(NT(STMT), NT(STMTS)),
182
183 /* <asgn> */ r(
184 NT(IDENT_LS), TK(EQ), NT(EXPR_LS),
185 ),
186
187 /* <ident_ls> ::= */
188 rrr(IDENT_LS, NT(IDENT), TK(COMMA)),
189
190 /* <expr_ls> ::= */
191 rrr(EXPR_LS, NT(EXPR), TK(COMMA)),
192};
193
194
195#endif
#define NT(nt)
Macro to create a non-terminal production symbol.
Definition: bnf_dsl.h:63
#define ropt(...)
Macro to define an optional grammar rule (epsilon production).
Definition: bnf_dsl.h:99
#define TK(tk)
Macro to create a terminal (token) production symbol.
Definition: bnf_dsl.h:61
#define r(...)
Macro to define a grammar rule. Adds end-of-body and construct sentinels to grammar rules.
Definition: bnf_dsl.h:72
#define rrr(head, listelem, delim)
Macro to define a pair of rules for a right-recursive list.
Definition: bnf_dsl.h:88
#define alt
Macro for syntactic sugar to separate non-terminal alternatives.
Definition: bnf_dsl.h:106
#define BALG_NT_BODY_LENGTH
Maximum number of symbols in a production body (Right-Hand Side) and +1 for end-of-body sentinel....
Definition: boolean_algebra.h:53
const char balg_tks[BALG_TK_COUNT]
token character mapping (for exblex)
Definition: boolean_algebra.h:86
#define BALG_NT_VARIANT_COUNT
Maximum number of production variants (alternatives) for a single non-terminal and +1 for end-of-cons...
Definition: boolean_algebra.h:46
#define BALG_TK_COUNT
total count of terminal symbols
Definition: boolean_algebra.h:33
balg_nt
non-terminal Symbols
Definition: boolean_algebra.h:68
const char *const balg_tk_names_escaped[BALG_TK_COUNT]
names of tokens that can be used in dotlang graph (special chars are escaped)
Definition: boolean_algebra.h:109
balg_tk
terminal symbols (tokens)
Definition: boolean_algebra.h:56
const char *const balg_tk_names[BALG_TK_COUNT]
names of tokens that used in BNF
Definition: boolean_algebra.h:97
#define BALG_NT_COUNT
Total count of non-terminal symbols defined in enum balg_nt. Determines the size of the first dimensi...
Definition: boolean_algebra.h:39
const char *const balg_nt_names[BALG_NT_COUNT]
non-terminal names (for debugging/printing CST)
Definition: boolean_algebra.h:118
A terminal/non-terminal to describe body (right side) of a production rule.
Definition: cfg.h:45