-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.y
102 lines (86 loc) · 1.84 KB
/
parser.y
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
%require "3.0.1"
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Flex details
int yylex(void);
extern FILE * yyin;
FILE * yyerr;
extern int line_num;
void yyerror(const char * s);
%}
%code requires {
#include "readconfig.h"
rc_data * parsed_data;
}
%union {
char * text;
rc_data * node;
}
// Define the tokens
%token <text> PARAM
%token SEPARATOR
%token <text> VALUE
// Map out the types
%type <node> file
%type <node> parameters
%type <node> parameter
%initial-action {
if (parsed_data != NULL) {
rc_clear(parsed_data);
}
parsed_data = (rc_data *) malloc(sizeof (rc_data));
parsed_data->param = NULL;
parsed_data->value = NULL;
parsed_data->next = NULL;
}
%%
file:
parameters
{ rc_data * walker;
walker = $1;
while (walker != NULL) {
rc_register(parsed_data, walker->param, walker->value);
walker = walker->next;
}
rc_clear($1);
$$ = parsed_data;
}
parameters:
parameter { $$ = $1; }
| parameter parameters {
$$ = $1;
$$->next = $2;
}
parameter:
PARAM {
$$ = (rc_data *) malloc(sizeof (rc_data));
if ($$ == NULL) {
fprintf(yyerr, "Error allocating memory for %s\n", $1);
free($1);
return -1;
}
$$->param = strdup($1);
$$->value = strdup("true\0");
$$->next = NULL;
free($1);
}
| PARAM SEPARATOR VALUE {
$$ = (rc_data *) malloc(sizeof (rc_data));
if ($$ == NULL) {
fprintf(yyerr, "Error allocating memory for %s\n", $1);
free($1);
return -1;
}
$$->param = strdup($1);
$$->value = strdup($3);
$$->next = NULL;
free($1);
free($3);
}
%%
void yyerror(const char * s)
{
fprintf(yyerr, "Parsing error '%s' at line %d\n", s, line_num);
}