-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.y
162 lines (113 loc) · 3.76 KB
/
parse.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
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
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "asdl.h"
static inline char *dash_to_underscore(char *ident) {
char *replaced = ident;
while (*replaced++)
*replaced = *replaced == '-' ? '_' : *replaced;
return ident;
}
extern int yylex(void);
int yyerror(const char *);
extern Field *fields;
extern Constructor *constructors;
extern Rule *rules;
extern size_t line_no;
%}
%union {
char *str_val;
Field *field_val;
Constructor *cons_val;
Rule *rule_val;
TypeId *typeid_val;
}
%token <str_val> VARIANT_NAME FIELD_TYPE FIELD_NAME RULE_NAME
%token ATTRIBUTES BOOL SIZE USIZE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 FLOAT32 FLOAT64 FLOAT80 CHAR UCHAR STRING IDENTIFIER BYTEARRAY
%token SUM_LPAREN SUM_RPAREN
%token REC_LPAREN REC_RPAREN
%token INIT_ASSIGN
%token REC_TERM SUM_TERM
%type <str_val> name_opt
%type <field_val> fields_attr fields_prod fields_sum_opt attrs
%type <cons_val> constructors
%type <rule_val> sum prod type
%type <typeid_val> type_id
%start asdl
%%
asdl : rules
|;
rules : rule
| rules rule
;
rule : RULE_NAME INIT_ASSIGN type term { $3->id = dash_to_underscore($1); symtable_insert($3->id, NULL); }
;
term : REC_TERM
| SUM_TERM
;
type : prod { $$ = $1; }
| sum { $$ = $1; }
;
prod : fields_prod { $$ = add_product_type($1); }
;
sum : constructors { $$ = add_sum_type($1, NULL); }
| constructors attrs { $$ = add_sum_type($1, $2); }
;
attrs : ATTRIBUTES fields_attr { $$ = fields; fields = NULL; }
;
constructors : variants { $$ = constructors; constructors = NULL; }
;
variants : variant
| variants '|' variant
;
variant : VARIANT_NAME fields_sum_opt { add_constructor(dash_to_underscore($1), $2, $2 == NULL); }
;
fields_sum_opt : SUM_LPAREN items SUM_RPAREN { $$ = fields; fields = NULL; }
| { $$ = NULL; }
;
fields_prod : REC_LPAREN items REC_RPAREN { $$ = fields; fields = NULL; }
;
fields_attr : SUM_LPAREN items SUM_RPAREN { $$ = fields; fields = NULL; }
;
items : item
| items ',' item
;
item : type_id name_opt { add_field($1, FIELD_NORMAL, $2); }
| type_id '*' name_opt { add_field($1, FIELD_SEQUENCE, $3); }
| type_id '?' name_opt { add_field($1, FIELD_OPTIONAL, $3); }
;
type_id : FIELD_TYPE { $$ = create_typeid(TYYNAME_ID, dash_to_underscore($1)); }
| BOOL { $$ = create_typeid(TYYNAME_BOOL, NULL); }
| INT8 { $$ = create_typeid(TYYNAME_INT8, NULL); }
| UINT8 { $$ = create_typeid(TYYNAME_UINT8, NULL); }
| INT16 { $$ = create_typeid(TYYNAME_INT16, NULL); }
| UINT16 { $$ = create_typeid(TYYNAME_UINT16, NULL); }
| INT32 { $$ = create_typeid(TYYNAME_INT32, NULL); }
| UINT32 { $$ = create_typeid(TYYNAME_UINT32, NULL); }
| INT64 { $$ = create_typeid(TYYNAME_INT64, NULL); }
| UINT64 { $$ = create_typeid(TYYNAME_UINT64, NULL); }
| FLOAT32 { $$ = create_typeid(TYYNAME_FLOAT32, NULL); }
| FLOAT64 { $$ = create_typeid(TYYNAME_FLOAT64, NULL); }
| FLOAT80 { $$ = create_typeid(TYYNAME_FLOAT80, NULL); }
| CHAR { $$ = create_typeid(TYYNAME_CHAR, NULL); }
| UCHAR { $$ = create_typeid(TYYNAME_UCHAR, NULL); }
| SIZE { $$ = create_typeid(TYYNAME_SIZE, NULL); }
| USIZE { $$ = create_typeid(TYYNAME_USIZE, NULL); }
| STRING { $$ = create_typeid(TYYNAME_STRING, NULL); }
| IDENTIFIER { $$ = create_typeid(TYYNAME_IDENTIFIER, NULL); }
| BYTEARRAY { $$ = create_typeid(TYYNAME_BYTEARRAY, NULL); }
;
name_opt : FIELD_NAME { $$ = dash_to_underscore($1); }
| { $$ = NULL; }
;
%%
int yyerror(const char *msg) {
fputs("Parsing error occurred: ", stderr);
fputs(msg, stderr);
fputs("\n", stderr);
fprintf(stderr, "At line: %lu\n", line_no);
exit(EXIT_FAILURE);
}