-
Notifications
You must be signed in to change notification settings - Fork 0
/
junos.y
117 lines (106 loc) · 2.17 KB
/
junos.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
%{
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define YYSTYPE char*
#define MAX_LEVEL 32
#define SET "set"
#define DEACTIVATE "deactivate"
void print_cfg(const char* str);
void print_deactive_cfg(const char* str);
typedef struct {
int cur_kw;
char* keywords[MAX_LEVEL];
} kwstack;
kwstack g_kws;
%}
%token WORD STRING O_BEGIN O_END SEMICOLON INACTIVE
%%
START: STATEMENTS;
STATEMENTS: STATEMENT | STATEMENTS STATEMENT;
STATEMENT: TERMLINE
| BLOCK_BEGIN STATEMENTS BLOCK_END
;
TERMLINE: VALUES SEMICOLON {
//printf ("Termline %s\n", $1);
print_cfg($1);
free($1);
}
| INACTIVE VALUES SEMICOLON {
print_deactive_cfg($2);
print_cfg($2);
free($2);
}
;
BLOCK_BEGIN: VALUES O_BEGIN {
//printf ("Block begins %s\n", $1);
g_kws.cur_kw++;
g_kws.keywords[g_kws.cur_kw] = $1;
}
| INACTIVE VALUES O_BEGIN {
//deactivaed config
g_kws.cur_kw++;
g_kws.keywords[g_kws.cur_kw] = $2;
print_deactive_cfg("");
}
;
BLOCK_END: O_END {
free(g_kws.keywords[g_kws.cur_kw]);
g_kws.keywords[g_kws.cur_kw] = NULL;
g_kws.cur_kw--;
}
;
VALUES: WORD {
//printf ("Word %s\n", $1);
$$=$1;
}
| STRING {
//printf ("String %s\n", $1);
$$=$1;
}
| VALUES WORD {
//printf ("Multi Values %s %s\n", $1, $2);
$$ = (char *)malloc(sizeof(char)*(strlen($1)+1+strlen($2)+1));
sprintf($$,"%s %s",$1,$2);
free($1);
free($2);
}
| VALUES STRING {
//printf ("Multi Values %s %s\n", $1, $2);
$$ = (char *)malloc(sizeof(char)*(strlen($1)+1+strlen($2)+1));
sprintf($$,"%s %s",$1,$2);
free($1);
free($2);
}
;
%%
int init()
{
g_kws.keywords[0] = strdup(SET);
g_kws.cur_kw = 0;
return 0;
}
int yywrap()
{
return 1;
}
int yyerror (char *err) {
fprintf(stderr, "%s", err);
}
void print_cfg(const char* str)
{
int i=0;
for (;i<=g_kws.cur_kw;i++) {
printf("%s ", g_kws.keywords[i]);
}
printf("%s\n", str);
}
void print_deactive_cfg(const char* str)
{
int i=1;
printf("%s", DEACTIVATE);
for (;i<=g_kws.cur_kw;i++) {
printf(" %s", g_kws.keywords[i]);
}
printf(" %s\n", str);
}