-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.y
81 lines (64 loc) · 1.9 KB
/
json.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
/*
* json语法解析规则
*
* author:fengye
* date: 2021-10-21
* email: [email protected]
*/
%{
# include <stdio.h>
# include <stdlib.h>
# include "myjson.h"
%}
%parse-param { struct json_state *pstate }
%code requires {
struct json_state;
}
%union {
Object *obj;
long intval;
double floatval;
char *strval;
}
/* declare tokens */
%token <intval> INTNUM
%token <floatval> APPROXNUM
%token <intval> BOOL
%token <strval> STRING NAME
%token NUL
%token START_OBJECT
%token END_OBJECT
%token START_ARRAY
%token END_ARRAY
%token COMMA
%token COLON
%type <obj> json pairs pair elements element dict array
%start json
%%
json: /* nothing */
| json element { emit(pstate, $2); }
;
element: dict { $$ = $1; }
| array { $$ = $1; }
| STRING { $$ = new_string($1); }
| APPROXNUM { $$ = new_double($1); }
| INTNUM { $$ = new_int($1); }
| BOOL { $$ = new_boolean($1); }
| NUL { $$ = new_nil(); }
;
dict: START_OBJECT END_OBJECT { $$ = new_dict(NULL); }
| START_OBJECT pairs END_OBJECT { $$ = $2; }
;
pairs: pair { $$ = new_dict((Pair *)$1); }
| pairs COMMA pair { $$ = add_pair((Dict *)$1, (Pair *)$3); }
;
pair: STRING COLON element { $$ = new_pair($1, $3); }
| NAME COLON element { $$ = new_pair($1, $3); }
;
array: START_ARRAY END_ARRAY { $$ = new_array(NULL); }
| START_ARRAY elements END_ARRAY { $$ = $2; }
;
elements: element { $$ = new_array($1); }
| elements COMMA element { $$ = add_element((Array *)$1, $3); }
;
%%