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
|
%define api.pure full
%locations
%define parse.error verbose
%param { yyscan_t scanner }
%code top {
#include <stdio.h>
}
%code requires {
typedef void* yyscan_t;
}
%code {
int yylex(YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_t scanner);
int yyerror(YYLTYPE *yyllocp, yyscan_t unused, const char *msg);
}
%union {
long lval;
double dval;
char *sval;
char cval;
}
%token PROLOG
%token S_TAG_OPEN E_TAG_OPEN TAG_CLOSE EMPTY_TAG_CLOSE
%token <sval> NAME
%token <sval> ATTR
%token <sval> TEXT
%token <lval> INTEGER
%token <dval> DOUBLE
%token DATE;
%%
document: PROLOG element;
elements:
element
| elements element
;
element:
empty_tag
| start_tag end_tag
| start_tag content end_tag
;
content:
elements
| integers
| doubles
| DATE
;
empty_tag:
S_TAG_OPEN NAME attributes EMPTY_TAG_CLOSE { printf("empty tag: %s\n", $2); }
;
start_tag:
S_TAG_OPEN NAME attributes TAG_CLOSE { printf("enter tag: %s\n", $2); }
;
end_tag:
E_TAG_OPEN NAME TAG_CLOSE { printf("exit tag: %s\n", $2); }
;
attributes:
| attribute
| attributes attribute;
;
attribute:
ATTR '=' '"' TEXT '"' { printf("attribute: %s=%s\n", $1, $4); }
;
integers:
INTEGER { printf("%d\n", $1); }
| integers INTEGER { printf("%d\n", $2); }
;
doubles:
DOUBLE { printf("%f\n", $1); }
| doubles DOUBLE { printf("%f\n", $2); }
;
%%
int yyerror(YYLTYPE *yyllocp, yyscan_t unused, const char *msg);
{
fprintf(
stderr, "[%d:%d]: %s\n",
yyllocp->first_line, yyllocp->first_column, msg
);
return 1;
}
|