-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.mly
240 lines (199 loc) · 6.62 KB
/
parser.mly
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* Parseur pour le compilateur C */
%{
open Ast
open Analysis
let mk_loc e l = { info = l; node = e }
type pvar =
| I of ident
| P of pvar
let rec unvar t v =
match v with
| I i -> (t, i)
| P vv -> unvar (Tpointer t) vv
%}
%token EOF
%token <string> IDENT
%token <int64> NUM
%token <int64> UNSIGNED_NUM
%token <int64> UNSIGNED_LONG_NUM
%token <int64> LONG_NUM
%token <float> NUM_FLOAT
%token <string> CONST_STRING
%token <char> CONST_CHAR
%token VOID
%token CHAR SHORT INT LONG
%token DOUBLE
%token UNSIGNED
%token STRUCT EXTERN
%token SEMI STAR COMMA ARROW DOT
%token ASSIGN
%token LBRACKET RBRACKET LPAR RPAR L_SQ_BRACKET R_SQ_BRACKET
%token EQ NEQ
%token AND OR LT LTE GT GTE NOT BAND
%token PLUS MINUS DIV MOD
%token SIZEOF WHILE FOR IF ELSE RETURN
%token PLUSPLUS MINUSMINUS
/* Priorités */
(*
opérateur associativité précédence
-----------------------------------------------------------------
= à droite faible
|| à gauche
&& à gauche
== != à gauche
< <= > >= à gauche ↓
+ - à gauche
* / % à gauche
! ++ -- & *(unaire) +(unaire) -(unaire) à droite
) [ -> . à gauche forte
*)
%right ASSIGN
%left OR
%left AND
%left EQ NEQ
%left LT LTE GT GTE
%left PLUS MINUS
%left STAR DIV MOD
%right NOT PLUSPLUS MINUSMINUS BAND USTAR UPLUS UMINUS
%left RPAR L_SQ_BRACKET ARROW DOT
%nonassoc REDUCE (* Ref: http://docs.oracle.com/cd/E19504-01/802-5880/6i9k05dh3/index.html *)
%nonassoc ELSE
/*
UNSIGNED_NUM
UNSIGNED_LONG_NUM
STAR
SIZEOF
PLUSPLUS
PLUS
NUM_FLOAT
NUM
NOT
MINUSMINUS
MINUS
LPAR
LONG_NUM
IDENT
CONST_STRING
CONST_CHAR
BAND
*/
/* Point d'entrée */
%start file
%type <Ast.loc Ast.file> file
%%
file:
| l = list(declarations) EOF { l }
;
/* déclarations */
declarations:
(* Variable declarations *)
| decl_vars=variable_declaration SEMI { Dvar(decl_vars) }
(* Struct declarations *)
| STRUCT i=identifier LBRACKET decl_vars_list=list(terminated(variable_declaration, SEMI)) RBRACKET SEMI { Dstruct(i, decl_vars_list) }
(* Function declarations *)
| fun_type=c_type fun_identifier=c_variable LPAR fun_args=separated_list(COMMA, variable_declaration) RPAR fun_block=block { let t, i = unvar fun_type fun_identifier in Dfun(t, i, fun_args, Some fun_block, mk_fun_analysis false) }
| fun_type=c_type fun_identifier=c_variable LPAR fun_args=separated_list(COMMA, variable_declaration) RPAR SEMI { let t, i = unvar fun_type fun_identifier in Dfun(t, i, fun_args, None, mk_fun_analysis false) }
(* Extern declarations *)
| EXTERN fun_type=c_type fun_identifier=c_variable LPAR fun_args=separated_list(COMMA, variable_declaration) RPAR SEMI { let t, i = unvar fun_type fun_identifier in Dfun(t, i, fun_args, None, mk_fun_analysis true) }
;
block:
| LBRACKET; lb = list(terminated(variable_declaration, SEMI)); li = list(instruction); RBRACKET { (lb, li) }
;
instruction_:
| SEMI { Sskip }
| expr = expression SEMI { Sexpr expr }
| IF LPAR expr = expression RPAR instr1 = instruction ELSE instr2 = instruction { Sif(expr, instr1, Some instr2) }
| IF LPAR expr = expression RPAR instr = instruction %prec REDUCE { Sif(expr, instr, None) }
| WHILE LPAR cond = expression RPAR instr = instruction { Sfor(None, Some cond, None, instr) }
| FOR LPAR expr1 = l_expr SEMI cond = expression SEMI expr2 = l_expr RPAR instr = instruction { Sfor(Some expr1, Some cond, Some expr2, instr) }
| b = block { Sblock b }
| RETURN expr = expression SEMI { Sreturn (Some expr) }
| RETURN SEMI { Sreturn None }
;
instruction:
| i = instruction_ { mk_loc i ($startpos, $endpos) }
;
l_expr:
l = separated_list(COMMA, expression) { l }
;
expression_:
| n = NUM { Econst(Cint(Signed, Int, n)) }
| n = UNSIGNED_LONG_NUM { Econst(Cint(Unsigned, Long, n)) }
| n = LONG_NUM { Econst(Cint(Signed, Long, n)) }
| n = UNSIGNED_NUM { Econst(Cint(Unsigned, Int, n)) }
| n = NUM_FLOAT { Econst(Cdouble(n)) }
| c = CONST_CHAR { Econst(Cint(Unsigned, Char, Int64.of_int (Char.code c))) }
| c = CONST_STRING { Econst(Cstring(c)) }
| i = identifier { Eident(i) }
| STAR expr = expression %prec USTAR { Eunop(Deref, expr) }
| expr1 = expression L_SQ_BRACKET expr2 = expression R_SQ_BRACKET {Eunop(Deref, mk_loc (Ebinop(expr1, Add, expr2)) ($startpos, $endpos) ) }
| expr = expression DOT id = identifier SEMI { Ebinop(expr, Dot, mk_loc (Eident id) id.info) }
| expr = expression ARROW id = identifier SEMI { (Ebinop(expr, Dot, mk_loc (Eident id) id.info )) }
| expr1 = expression ASSIGN expr2 = expression { Eassign(expr1, expr2) }
| i = identifier LPAR lexpr = l_expr RPAR { Ecall( i, lexpr) }
| PLUSPLUS expr = expression { Eunop(PreInc, expr) }
| MINUSMINUS expr = expression { Eunop(PreDec, expr) }
| expr = expression PLUSPLUS { Eunop(PostInc, expr) }
| expr = expression MINUSMINUS { Eunop(PostDec, expr) }
| expr1 = expression op = binop expr2 = expression { Ebinop(expr1, op, expr2) }
| uop = unop expr = expression { Eunop(uop, expr) }
| MINUS expr = expression %prec UMINUS { Eunop(Neg, expr) }
| PLUS expr = expression %prec UPLUS { Eunop(Pos, expr) }
| SIZEOF LPAR cplxtype = cplx_type RPAR {(Esizeof cplxtype) }
| LPAR cplxtype = cplx_type RPAR expr = expression { Ecast(cplxtype, expr) }
| LPAR expr = expression_ RPAR { expr }
;
cplx_type:
| t = c_type { t }
;
expression:
| e = expression_ { mk_loc e ($startpos, $endpos) }
;
variable_declaration:
| ctype=c_type cvar=c_variable { unvar ctype cvar }
;
c_variable:
| i = identifier { I ( i ) }
| STAR; v = c_variable { P ( v ) }
;
c_type:
| VOID { Tvoid }
| cinttype=c_int_type { cinttype }
| DOUBLE { Tdouble }
| STRUCT i=identifier { Tstruct i}
;
identifier:
| i = IDENT { mk_loc i ($startpos, $endpos) }
;
signedness:
| { Signed }
| UNSIGNED { Unsigned }
;
c_int_type:
| s=signedness cit=c_int_types { Tinteger(s, cit) }
;
%inline c_int_types:
| CHAR { Char }
| SHORT { Short }
| INT { Int }
| LONG { Long }
;
%inline unop:
| BAND { Addr }
| NOT { Not }
;
%inline binop:
| PLUS { Add }
| MINUS { Minus }
| STAR { Mult }
| DIV { Div }
| MOD { Mod }
| AND { And }
| OR { Or }
| EQ { Eq }
| NEQ { Neq }
| LT { Lt }
| LTE { Le }
| GT { Gt }
| GTE { Ge }
;