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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/*
* Command format string parser.
*
* Turns a command definition into a DFA that together with the functions
* provided in command_match.c may be used to map command line input to a
* function.
*
* @author Quentin Young <qlyoung@cumulusnetworks.com>
*/
%{
extern int yylex(void);
extern void yyerror(const char *);
// compile with debugging facilities
#define YYDEBUG 1
%}
%code requires {
#include "command.h"
#include "command_graph.h"
#include "memory.h"
}
%code provides {
extern void
set_buffer_string(const char *);
struct graph_node *
parse_command_format(struct graph_node *, struct cmd_element *);
}
/* valid types for tokens */
%union{
int integer;
char *string;
struct graph_node *node;
}
/* some helpful state variables */
%{
struct graph_node *startnode, // command root node
*currnode, // current node
*seqhead; // sequence head
struct graph_node *optnode_start, // start node for option set
*optnode_end; // end node for option set
struct graph_node *selnode_start, // start node for selector set
*selnode_end; // end node for selector set
struct cmd_element *command; // command we're parsing
%}
%token <string> WORD
%token <string> IPV4
%token <string> IPV4_PREFIX
%token <string> IPV6
%token <string> IPV6_PREFIX
%token <string> VARIABLE
%token <string> RANGE
%token <integer> NUMBER
%type <node> start
%type <node> sentence_root
%type <node> literal_token
%type <node> placeholder_token
%type <node> option
%type <node> option_token
%type <node> option_token_seq
%type <node> selector
%type <node> selector_element_root
%type <node> selector_token
%type <node> selector_token_seq
%defines "command_parse.h"
%output "command_parse.c"
/* grammar proper */
%%
start: sentence_root
cmd_token_seq
{
// create leaf node
struct graph_node *end = new_node(END_GN);
end->element = command;
// add node
if (add_node(currnode, end) != end)
{
yyerror("Duplicate command.");
YYABORT;
}
fprintf(stderr, "Added END_GN with active children: %d\n", vector_active(end->children));
}
sentence_root: WORD
{
struct graph_node *root = new_node(WORD_GN);
root->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
currnode = add_node(startnode, root);
if (currnode != root)
free (root);
free ($1);
$$ = currnode;
};
/* valid top level tokens */
cmd_token:
placeholder_token
{
currnode = add_node(currnode, $1);
if (currnode != $1)
free_node ($1);
}
| literal_token
{
currnode = add_node(currnode, $1);
if (currnode != $1)
free_node ($1);
}
/* selectors and options are subgraphs with start and end nodes */
| selector
{
add_node(currnode, $1);
currnode = selnode_end;
selnode_start = selnode_end = NULL;
}
| option
{
add_node(currnode, $1);
currnode = optnode_end;
optnode_start = optnode_end = NULL;
}
;
cmd_token_seq:
%empty
| cmd_token_seq cmd_token
;
placeholder_token:
IPV4
{
$$ = new_node(IPV4_GN);
$$->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
free ($1);
}
| IPV4_PREFIX
{
$$ = new_node(IPV4_PREFIX_GN);
$$->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
free ($1);
}
| IPV6
{
$$ = new_node(IPV6_GN);
$$->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
free ($1);
}
| IPV6_PREFIX
{
$$ = new_node(IPV6_PREFIX_GN);
$$->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
free ($1);
}
| VARIABLE
{
$$ = new_node(VARIABLE_GN);
$$->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
free ($1);
}
| RANGE
{
$$ = new_node(RANGE_GN);
$$->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
// get the numbers out
strsep(&yylval.string, "(-)");
$$->min = atoi( strsep(&yylval.string, "(-)") );
$$->max = atoi( strsep(&yylval.string, "(-)") );
free ($1);
}
;
literal_token:
WORD
{
$$ = new_node(WORD_GN);
$$->text = XSTRDUP(MTYPE_CMD_TOKENS, $1);
free ($1);
}
| NUMBER
{
$$ = new_node(NUMBER_GN);
$$->value = yylval.integer;
}
;
/* <selector|set> productions */
selector:
'<' selector_part '|' selector_element '>'
{
// all the graph building is done in selector_element,
// so just return the selector subgraph head
$$ = selnode_start;
};
selector_part:
selector_part '|' selector_element
| selector_element
;
selector_element:
selector_element_root selector_token_seq
{
// if the selector start and end do not exist, create them
if (!selnode_start || !selnode_end) { // if one is null
assert(!selnode_start && !selnode_end); // both should be null
selnode_start = new_node(SELECTOR_GN); // diverging node
selnode_end = new_node(NUL_GN); // converging node
selnode_start->end = selnode_end; // duh
}
// add element head as a child of the selector
add_node(selnode_start, $1);
if ($2->type != NUL_GN) {
add_node($1, seqhead);
add_node($2, selnode_end);
}
else
add_node($1, selnode_end);
seqhead = NULL;
}
selector_token_seq:
%empty { $$ = new_node(NUL_GN); }
| selector_token_seq selector_token
{
// if the sequence component is NUL_GN, this is a sequence start
if ($1->type == NUL_GN) {
assert(!seqhead); // sequence head should always be null here
seqhead = $2;
}
else // chain on new node
add_node($1, $2);
$$ = $2;
}
;
selector_element_root:
literal_token
| placeholder_token
;
selector_token:
selector_element_root
| option
;
/* [option|set] productions */
option: '[' option_part ']'
{
// add null path
struct graph_node *nullpath = new_node(NUL_GN);
add_node(optnode_start, nullpath);
add_node(nullpath, optnode_end);
$$ = optnode_start;
};
option_part:
option_part '|' option_element
| option_element
;
option_element:
option_token_seq
{
if (!optnode_start || !optnode_end) {
assert(!optnode_start && !optnode_end);
optnode_start = new_node(OPTION_GN);
optnode_end = new_node(NUL_GN);
}
add_node(optnode_start, seqhead);
add_node($1, optnode_end);
}
option_token_seq:
option_token
{ $$ = seqhead = $1; }
| option_token_seq option_token
{ $$ = add_node($1, $2); }
;
option_token:
literal_token
| placeholder_token
;
%%
void yyerror(char const *message) {
// fail on bad parse
fprintf(stderr, "Grammar error: %s\n", message);
fprintf(stderr, "Token on error: ");
if (yylval.string) fprintf(stderr, "%s\n", yylval.string);
else if (yylval.node) fprintf(stderr, "%s\n", yylval.node->text);
else fprintf(stderr, "%d\n", yylval.integer);
}
struct graph_node *
parse_command_format(struct graph_node *start, struct cmd_element *cmd)
{
fprintf(stderr, "parsing: %s\n", cmd->string);
/* clear state pointers */
startnode = start;
currnode = seqhead = NULL;
selnode_start = selnode_end = NULL;
optnode_start = optnode_end = NULL;
// trace parser
yydebug = 0;
// command string
command = cmd;
// make flex read from a string
set_buffer_string(command->string);
// parse command into DFA
yyparse();
// startnode points to command DFA
return startnode;
}
|