Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / parser_lustre.mly @ 52cfee34

History | View | Annotate | Download (14.3 KB)

1
/* ----------------------------------------------------------------------------
2
 * SchedMCore - A MultiCore Scheduling Framework
3
 * Copyright (C) 2009-2011, ONERA, Toulouse, FRANCE - LIFL, Lille, FRANCE
4
 *
5
 * This file is part of Prelude
6
 *
7
 * Prelude is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public License
9
 * as published by the Free Software Foundation ; either version 2 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * Prelude is distributed in the hope that it will be useful, but
13
 * WITHOUT ANY WARRANTY ; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this program ; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
 * USA
21
 *---------------------------------------------------------------------------- */
22

    
23
%{
24
open LustreSpec
25
open Corelang
26
open Dimension
27
open Utils
28

    
29
let mktyp x = mktyp (Location.symbol_rloc ()) x
30
let mkclock x = mkclock (Location.symbol_rloc ()) x
31
let mkvar_decl x = mkvar_decl (Location.symbol_rloc ()) x
32
let mkexpr x = mkexpr (Location.symbol_rloc ()) x
33
let mkeq x = mkeq (Location.symbol_rloc ()) x
34
let mkassert x = mkassert (Location.symbol_rloc ()) x
35
let mktop_decl x = mktop_decl (Location.symbol_rloc ()) x
36
let mkpredef_call x = mkpredef_call (Location.symbol_rloc ()) x
37
let mkpredef_unary_call x = mkpredef_unary_call (Location.symbol_rloc ()) x
38

    
39
let mkdim_int i = mkdim_int (Location.symbol_rloc ()) i
40
let mkdim_bool b = mkdim_bool (Location.symbol_rloc ()) b
41
let mkdim_ident id = mkdim_ident (Location.symbol_rloc ()) id
42
let mkdim_appl f args = mkdim_appl (Location.symbol_rloc ()) f args
43
let mkdim_ite i t e = mkdim_ite (Location.symbol_rloc ()) i t e
44

    
45
let add_node own msg hashtbl name value =
46
  try
47
    match (Hashtbl.find hashtbl name).top_decl_desc, value.top_decl_desc with
48
    | Node _        , ImportedNode _ when own
49
                        ->
50
       Hashtbl.add hashtbl name value
51
    | ImportedNode _, _ ->
52
       Hashtbl.add hashtbl name value
53
    | Node _        , _ -> 
54
       raise (Corelang.Error (Location.symbol_rloc (), Corelang.Already_bound_symbol msg))
55
    | _                 -> assert false
56
  with
57
    Not_found ->
58
       Hashtbl.add hashtbl name value
59

    
60
let add_symbol msg hashtbl name value =
61
 if Hashtbl.mem hashtbl name
62
 then raise (Corelang.Error (Location.symbol_rloc (), Corelang.Already_bound_symbol msg))
63
 else Hashtbl.add hashtbl name value
64

    
65
let check_symbol msg hashtbl name =
66
 if not (Hashtbl.mem hashtbl name)
67
 then raise (Corelang.Error (Location.symbol_rloc (), Corelang.Unbound_symbol msg))
68
 else ()
69

    
70
%}
71

    
72
%token <int> INT
73
%token <string> REAL
74
%token <float> FLOAT
75
%token AUTOMATON STATE UNTIL UNLESS RESTART RESUME LAST
76
%token STATELESS ASSERT OPEN QUOTE FUNCTION
77
%token <string> IDENT
78
%token <LustreSpec.expr_annot> ANNOT
79
%token <LustreSpec.node_annot> NODESPEC
80
%token LBRACKET RBRACKET LCUR RCUR LPAR RPAR SCOL COL COMMA COLCOL 
81
%token AMPERAMPER BARBAR NOT POWER
82
%token IF THEN ELSE
83
%token UCLOCK DCLOCK PHCLOCK TAIL
84
%token MERGE FBY WHEN WHENNOT EVERY
85
%token NODE LET TEL RETURNS VAR IMPORTED SENSOR ACTUATOR WCET TYPE CONST
86
%token STRUCT ENUM
87
%token TINT TFLOAT TREAL TBOOL TCLOCK
88
%token RATE DUE
89
%token EQ LT GT LTE GTE NEQ
90
%token AND OR XOR IMPL
91
%token MULT DIV MOD
92
%token MINUS PLUS UMINUS
93
%token PRE ARROW
94

    
95
%token EOF
96

    
97
%nonassoc COMMA
98
%left MERGE IF
99
%nonassoc ELSE
100
%right ARROW FBY
101
%left WHEN WHENNOT UCLOCK DCLOCK PHCLOCK
102
%right COLCOL
103
%right IMPL
104
%left OR XOR BARBAR
105
%left AND AMPERAMPER
106
%left NOT
107
%nonassoc INT
108
%nonassoc EQ LT GT LTE GTE NEQ
109
%left MINUS PLUS
110
%left MULT DIV MOD
111
%left UMINUS
112
%left POWER
113
%left PRE LAST
114
%nonassoc RBRACKET
115
%nonassoc LBRACKET
116

    
117
%start prog
118
%type <Corelang.top_decl list> prog
119
%start header
120
%type <bool -> Corelang.top_decl list> header
121

    
122
%%
123

    
124
prog:
125
 open_list typ_def_list top_decl_list EOF { $1 @ (List.rev $3) }
126

    
127
header:
128
 open_list typ_def_list top_decl_header_list EOF { (fun own -> ($1 @ (List.rev ($3 own)))) }
129

    
130
open_list:
131
  { [] }
132
| open_lusi open_list { $1 :: $2 }
133

    
134
open_lusi:
135
  OPEN QUOTE IDENT QUOTE { mktop_decl (Open $3) }
136

    
137
top_decl_list:
138
  top_decl {[$1]}
139
| top_decl_list top_decl {$2::$1}
140

    
141

    
142
top_decl_header_list:
143
  top_decl_header {(fun own -> [$1 own]) }
144
| top_decl_header_list top_decl_header {(fun own -> ($2 own)::($1 own)) }
145

    
146
state_annot:
147
  FUNCTION { true }
148
| NODE { false }
149

    
150
top_decl_header:
151
  nodespec_list state_annot IDENT LPAR vdecl_list SCOL_opt RPAR RETURNS LPAR vdecl_list SCOL_opt RPAR SCOL
152
    {let nd = mktop_decl (ImportedNode
153
                            {nodei_id = $3;
154
                             nodei_type = Types.new_var ();
155
                             nodei_clock = Clocks.new_var true;
156
                             nodei_inputs = List.rev $5;
157
                             nodei_outputs = List.rev $10;
158
			     nodei_stateless = $2;
159
			     nodei_spec = $1})
160
    in
161
    (fun own -> add_node own ("node " ^ $3) node_table $3 nd; nd) }
162

    
163
top_decl:
164
| CONST cdecl_list { mktop_decl (Consts (List.rev $2)) }
165
| nodespec_list state_annot IDENT LPAR vdecl_list SCOL_opt RPAR RETURNS LPAR vdecl_list SCOL_opt RPAR SCOL_opt locals LET eq_list TEL 
166
    {let eqs, asserts, annots = $16 in
167
     let nd = mktop_decl (Node
168
                            {node_id = $3;
169
                             node_type = Types.new_var ();
170
                             node_clock = Clocks.new_var true;
171
                             node_inputs = List.rev $5;
172
                             node_outputs = List.rev $10;
173
                             node_locals = List.rev $14;
174
			     node_gencalls = [];
175
			     node_checks = [];
176
			     node_asserts = asserts; 
177
                             node_eqs = eqs;
178
			     node_dec_stateless = $2;
179
			     node_stateless = None;
180
			     node_spec = $1;
181
			     node_annot = match annots with [] -> None | _ -> Some annots})
182
    in
183
    add_node true ("node " ^ $3) node_table $3 nd; nd}
184

    
185
nodespec_list:
186
 { None }
187
| NODESPEC nodespec_list { (function None -> (fun s1 -> Some s1) | Some s2 -> (fun s1 -> Some (LustreSpec.merge_node_annot s1 s2))) $2 $1 }
188

    
189
typ_def_list:
190
    /* empty */ {}
191
| typ_def SCOL typ_def_list {$1;$3}
192

    
193
typ_def:
194
  TYPE IDENT EQ typeconst {
195
    try
196
      add_symbol ("type " ^ $2) type_table (Tydec_const $2) (Corelang.get_repr_type $4)
197
    with Not_found-> assert false }
198
| TYPE IDENT EQ ENUM LCUR tag_list RCUR { Hashtbl.add type_table (Tydec_const $2) (Tydec_enum ($6 (Tydec_const $2))) }
199
| TYPE IDENT EQ STRUCT LCUR field_list RCUR { Hashtbl.add type_table (Tydec_const $2) (Tydec_struct ($6 (Tydec_const $2))) }
200

    
201
array_typ_decl:
202
                            { fun typ -> typ }
203
 | POWER dim array_typ_decl { fun typ -> $3 (Tydec_array ($2, typ)) }
204

    
205
typeconst:
206
  TINT array_typ_decl  { $2 Tydec_int }
207
| TBOOL array_typ_decl { $2 Tydec_bool  }
208
| TREAL array_typ_decl { $2 Tydec_real  }
209
| TFLOAT array_typ_decl { $2 Tydec_float }
210
| IDENT array_typ_decl { check_symbol ("type " ^ $1) type_table (Tydec_const $1); $2 (Tydec_const $1) }
211
| TBOOL TCLOCK  { Tydec_clock Tydec_bool }
212
| IDENT TCLOCK  { Tydec_clock (Tydec_const $1) }
213

    
214
tag_list:
215
  IDENT
216
  { (fun t -> add_symbol ("tag " ^ $1) tag_table $1 t; $1 :: []) }
217
| tag_list COMMA IDENT
218
  { (fun t -> add_symbol ("tag " ^ $3)tag_table $3 t; $3 :: ($1 t)) }
219

    
220
field_list:
221
  { (fun t -> []) }
222
| field_list IDENT COL typeconst SCOL
223
  { (fun t -> add_symbol ("field " ^ $2) field_table $2 t; ($1 t) @ [ ($2, $4) ]) }
224

    
225
eq_list:
226
  { [], [], [] }
227
| eq eq_list {let eql, assertl, annotl = $2 in ($1::eql), assertl, annotl}
228
| assert_ eq_list {let eql, assertl, annotl = $2 in eql, ($1::assertl), annotl}
229
| ANNOT eq_list {let eql, assertl, annotl = $2 in eql, assertl, $1@annotl}
230
| automaton eq_list {let eql, assertl, annotl = $2 in ($1::eql), assertl, annotl}
231

    
232
automaton:
233
 AUTOMATON IDENT handler_list { failwith "not implemented" }
234

    
235
handler_list:
236
     { [] }
237
| handler handler_list { $1::$2 }
238

    
239
handler:
240
 STATE IDENT ARROW unless_list locals LET eq_list TEL until_list { () }
241

    
242
unless_list:
243
    { [] }
244
| unless unless_list { $1::$2 }
245

    
246
until_list:
247
    { [] }
248
| until until_list { $1::$2 }
249

    
250
unless:
251
  UNLESS expr RESTART IDENT { }
252
| UNLESS expr RESUME IDENT  { }
253

    
254
until:
255
  UNTIL expr RESTART IDENT { }
256
| UNTIL expr RESUME IDENT  { }
257

    
258
assert_:
259
| ASSERT expr SCOL {mkassert ($2)}
260

    
261
eq:
262
       ident_list      EQ expr SCOL {mkeq (List.rev $1,$3)}
263
| LPAR ident_list RPAR EQ expr SCOL {mkeq (List.rev $2,$5)}
264

    
265
tuple_expr:
266
    expr COMMA expr {[$3;$1]}
267
| tuple_expr COMMA expr {$3::$1}
268

    
269
// Same as tuple expr but accepting lists with single element
270
array_expr:
271
  expr {[$1]}
272
| expr COMMA array_expr {$1::$3}
273

    
274
dim_list:
275
  dim RBRACKET { fun base -> mkexpr (Expr_access (base, $1)) }
276
| dim RBRACKET LBRACKET dim_list { fun base -> $4 (mkexpr (Expr_access (base, $1))) }
277

    
278
expr:
279
/* constants */
280
  INT {mkexpr (Expr_const (Const_int $1))}
281
| REAL {mkexpr (Expr_const (Const_real $1))}
282
| FLOAT {mkexpr (Expr_const (Const_float $1))}
283
/* Idents or type enum tags */
284
| IDENT {
285
  if Hashtbl.mem tag_table $1
286
  then mkexpr (Expr_const (Const_tag $1))
287
  else mkexpr (Expr_ident $1)}
288
| LPAR ANNOT expr RPAR
289
    {update_expr_annot $3 $2}
290
| LPAR expr RPAR
291
    {$2}
292
| LPAR tuple_expr RPAR
293
    {mkexpr (Expr_tuple (List.rev $2))}
294

    
295
/* Array expressions */
296
| LBRACKET array_expr RBRACKET { mkexpr (Expr_array $2) }
297
| expr POWER dim { mkexpr (Expr_power ($1, $3)) }
298
| expr LBRACKET dim_list { $3 $1 }
299

    
300
/* Temporal operators */
301
| PRE expr 
302
    {mkexpr (Expr_pre $2)}
303
| expr ARROW expr 
304
    {mkexpr (Expr_arrow ($1,$3))}
305
| expr FBY expr 
306
    {(*mkexpr (Expr_fby ($1,$3))*)
307
      mkexpr (Expr_arrow ($1, mkexpr (Expr_pre $3)))}
308
| expr WHEN IDENT 
309
    {mkexpr (Expr_when ($1,$3,tag_true))}
310
| expr WHENNOT IDENT
311
    {mkexpr (Expr_when ($1,$3,tag_false))}
312
| expr WHEN IDENT LPAR IDENT RPAR
313
    {mkexpr (Expr_when ($1, $5, $3))}
314
| MERGE IDENT handler_expr_list
315
    {mkexpr (Expr_merge ($2,$3))}
316

    
317
/* Applications */
318
| IDENT LPAR expr RPAR
319
    {mkexpr (Expr_appl ($1, $3, None))}
320
| IDENT LPAR expr RPAR EVERY IDENT
321
    {mkexpr (Expr_appl ($1, $3, Some ($6, tag_true)))}
322
| IDENT LPAR expr RPAR EVERY IDENT LPAR IDENT RPAR
323
    {mkexpr (Expr_appl ($1, $3, Some ($8, $6))) }
324
| IDENT LPAR tuple_expr RPAR
325
    {mkexpr (Expr_appl ($1, mkexpr (Expr_tuple (List.rev $3)), None))}
326
| IDENT LPAR tuple_expr RPAR EVERY IDENT
327
    {mkexpr (Expr_appl ($1, mkexpr (Expr_tuple (List.rev $3)), Some ($6, tag_true))) }
328
| IDENT LPAR tuple_expr RPAR EVERY IDENT LPAR IDENT RPAR
329
    {mkexpr (Expr_appl ($1, mkexpr (Expr_tuple (List.rev $3)), Some ($8, $6))) }
330

    
331
/* Boolean expr */
332
| expr AND expr 
333
    {mkpredef_call "&&" [$1;$3]}
334
| expr AMPERAMPER expr 
335
    {mkpredef_call "&&" [$1;$3]}
336
| expr OR expr 
337
    {mkpredef_call "||" [$1;$3]}
338
| expr BARBAR expr 
339
    {mkpredef_call "||" [$1;$3]}
340
| expr XOR expr 
341
    {mkpredef_call "xor" [$1;$3]}
342
| NOT expr 
343
    {mkpredef_unary_call "not" $2}
344
| expr IMPL expr 
345
    {mkpredef_call "impl" [$1;$3]}
346

    
347
/* Comparison expr */
348
| expr EQ expr 
349
    {mkpredef_call "=" [$1;$3]}
350
| expr LT expr 
351
    {mkpredef_call "<" [$1;$3]}
352
| expr LTE expr 
353
    {mkpredef_call "<=" [$1;$3]}
354
| expr GT expr 
355
    {mkpredef_call ">" [$1;$3]}
356
| expr GTE  expr 
357
    {mkpredef_call ">=" [$1;$3]}
358
| expr NEQ expr 
359
    {mkpredef_call "!=" [$1;$3]}
360

    
361
/* Arithmetic expr */
362
| expr PLUS expr 
363
    {mkpredef_call "+" [$1;$3]}
364
| expr MINUS expr 
365
    {mkpredef_call "-" [$1;$3]}
366
| expr MULT expr 
367
    {mkpredef_call "*" [$1;$3]}
368
| expr DIV expr 
369
    {mkpredef_call "/" [$1;$3]}
370
| MINUS expr %prec UMINUS
371
  {mkpredef_unary_call "uminus" $2}
372
| expr MOD expr 
373
    {mkpredef_call "mod" [$1;$3]}
374

    
375
/* If */
376
| IF expr THEN expr ELSE expr
377
    {mkexpr (Expr_ite ($2, $4, $6))}
378

    
379
handler_expr_list:
380
   { [] }
381
| handler_expr handler_expr_list { $1 :: $2 }
382

    
383
handler_expr:
384
 LPAR IDENT ARROW expr RPAR { ($2, $4) }
385

    
386
signed_const_array:
387
| signed_const { [$1] }
388
| signed_const COMMA signed_const_array { $1 :: $3 }
389

    
390
signed_const_struct:
391
| IDENT EQ signed_const { [ ($1, $3) ] }
392
| IDENT EQ signed_const COMMA signed_const_struct { ($1, $3) :: $5 }
393

    
394
signed_const:
395
  INT {Const_int $1}
396
| REAL {Const_real $1}
397
| FLOAT {Const_float $1}
398
| IDENT {Const_tag $1}
399
| MINUS INT {Const_int (-1 * $2)}
400
| MINUS REAL {Const_real ("-" ^ $2)}
401
| MINUS FLOAT {Const_float (-1. *. $2)}
402
| LCUR signed_const_struct RCUR { Const_struct $2 }
403
| LBRACKET signed_const_array RBRACKET { Const_array $2 }
404

    
405
dim:
406
   INT { mkdim_int $1 }
407
| LPAR dim RPAR { $2 }
408
| IDENT { mkdim_ident $1 }
409
| dim AND dim 
410
    {mkdim_appl "&&" [$1;$3]}
411
| dim AMPERAMPER dim 
412
    {mkdim_appl "&&" [$1;$3]}
413
| dim OR dim 
414
    {mkdim_appl "||" [$1;$3]}
415
| dim BARBAR dim 
416
    {mkdim_appl "||" [$1;$3]}
417
| dim XOR dim 
418
    {mkdim_appl "xor" [$1;$3]}
419
| NOT dim 
420
    {mkdim_appl "not" [$2]}
421
| dim IMPL dim 
422
    {mkdim_appl "impl" [$1;$3]}
423

    
424
/* Comparison dim */
425
| dim EQ dim 
426
    {mkdim_appl "=" [$1;$3]}
427
| dim LT dim 
428
    {mkdim_appl "<" [$1;$3]}
429
| dim LTE dim 
430
    {mkdim_appl "<=" [$1;$3]}
431
| dim GT dim 
432
    {mkdim_appl ">" [$1;$3]}
433
| dim GTE  dim 
434
    {mkdim_appl ">=" [$1;$3]}
435
| dim NEQ dim 
436
    {mkdim_appl "!=" [$1;$3]}
437

    
438
/* Arithmetic dim */
439
| dim PLUS dim 
440
    {mkdim_appl "+" [$1;$3]}
441
| dim MINUS dim 
442
    {mkdim_appl "-" [$1;$3]}
443
| dim MULT dim 
444
    {mkdim_appl "*" [$1;$3]}
445
| dim DIV dim 
446
    {mkdim_appl "/" [$1;$3]}
447
| MINUS dim %prec UMINUS
448
  {mkdim_appl "uminus" [$2]}
449
| dim MOD dim 
450
    {mkdim_appl "mod" [$1;$3]}
451
/* If */
452
| IF dim THEN dim ELSE dim
453
    {mkdim_ite $2 $4 $6}
454

    
455
locals:
456
  {[]}
457
| VAR vdecl_list SCOL {$2}
458

    
459
vdecl_list:
460
    vdecl {$1}
461
| vdecl_list SCOL vdecl {$3 @ $1}
462

    
463
vdecl:
464
/* Useless no ?*/    ident_list
465
    {List.map mkvar_decl 
466
        (List.map (fun id -> (id, mktyp Tydec_any, mkclock Ckdec_any, false)) $1)}
467

    
468
| ident_list COL typeconst clock 
469
    {List.map mkvar_decl (List.map (fun id -> (id, mktyp $3, $4, false)) $1)}
470
| CONST ident_list COL typeconst /* static parameters don't have clocks */
471
    {List.map mkvar_decl (List.map (fun id -> (id, mktyp $4, mkclock Ckdec_any, true)) $2)}
472

    
473
cdecl_list:
474
  cdecl SCOL { [$1] }
475
| cdecl_list cdecl SCOL { $2::$1 }
476

    
477
cdecl:
478
    IDENT EQ signed_const {
479
      let c = {
480
	const_id = $1;
481
	const_loc = Location.symbol_rloc ();
482
        const_type = Types.new_var ();
483
	const_value = $3;
484
      } in
485
      Hashtbl.add consts_table $1 c; c
486
    }
487

    
488
clock:
489
    {mkclock Ckdec_any}
490
| when_list
491
    {mkclock (Ckdec_bool (List.rev $1))}
492

    
493
when_cond:
494
    WHEN IDENT {($2, tag_true)}
495
| WHENNOT IDENT {($2, tag_false)}
496
| WHEN IDENT LPAR IDENT RPAR {($4, $2)}
497

    
498
when_list:
499
    when_cond {[$1]}
500
| when_list when_cond {$2::$1}
501

    
502
ident_list:
503
  IDENT {[$1]}
504
| ident_list COMMA IDENT {$3::$1}
505

    
506
SCOL_opt:
507
    SCOL {} | {}