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
| %{
#include <stdio.h>
#include <stdlib.h>
#include "tree.h"
extern int yylex();
extern int yylineno;
extern char* yytext;
void yyerror( const char *s);
struct TreeNode *root = NULL;
%}
%union {
struct TreeNode *node;
char *value;
}
%token ZONE
%token LBRACE RBRACE SEMICOLON EQUALS
%type <node> config_file statements statement zone_option zone_options zone_block
%token <value> NAME STRING
%start config_file
%%
config_file:
statements
{
root = $1;
}
;
statements:
{
$$ = NULL;
}
| statements statement SEMICOLON
{
if ($1 == NULL) {
$$ = $2;
} else {
struct TreeNode *ptr = $1;
while (ptr->next_sibling != NULL) {
ptr = ptr->next_sibling;
}
ptr->next_sibling = $2;
$$ = $1;
}
}
;
statement:
zone_block
{
$$ = $1;
}
;
zone_block:
ZONE STRING LBRACE zone_options RBRACE {
$$ = createNode("zone", $2);
addChild($$, $4);
}
;
zone_options:
{
$$ = NULL;
}
| zone_options zone_option SEMICOLON
{
if ($1 == NULL) {
$$ = $2;
} else {
struct TreeNode *ptr = $1;
while (ptr->next_sibling != NULL) {
ptr = ptr->next_sibling;
}
ptr->next_sibling = $2;
$$ = $1;
}
}
;
zone_option:
NAME NAME {
$$ = createNode($1, $2);
}
|
NAME STRING {
$$ = createNode($1, $2);
}
;
%%
void
yyerror( const char *s)
{
fprintf(stderr,"error: %s on line %d\n", s, yylineno);
}
struct TreeNode* createNode(char* key, char* value) {
struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
node->key = key;
node->value = value;
node->parent = NULL;
node->first_child = NULL;
node->next_sibling = NULL;
return node;
}
struct TreeNode* addChild(struct TreeNode* parent, struct TreeNode* child) {
child->parent = parent;
if (parent->first_child == NULL) {
parent->first_child = child;
} else {
setChild(parent->first_child, child);
}
return child;
}
void setChild(struct TreeNode* parent, struct TreeNode* child) {
while (parent->next_sibling != NULL) {
parent = parent->next_sibling;
}
parent->next_sibling = child;
}
void printTree(struct TreeNode* node, int depth) {
if (node == NULL) {
return;
}
int i = 0;
while (i < depth) {
printf(" ");
i++;
}
printf("%s", node->key);
if (node->value != NULL) {
printf("[%s]", node->value);
}
printf("\n");
printTree(node->first_child, depth + 1);
printTree(node->next_sibling, depth);
}
extern FILE *yyin;
int main(int argc, char *argv[]) {
FILE *input_file;
if (argc < 2) {
fprintf(stderr, "Usage: %s filename\n", argv[0]);
return 1;
}
input_file = fopen(argv[1], "r");
if (input_file == NULL) {
fprintf(stderr, "Error: Cannot open file %s\n", argv[1]);
return 1;
}
yyin = input_file;
yyparse();
printTree(root, 0);
return 0;
}
|