Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / parser_lustre.mly @ 54ae8ac7

History | View | Annotate | Download (14.6 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
    | ImportedNode _, _ ->
51
       Hashtbl.add hashtbl name value
52
    | Node _        , _ -> 
53
       raise (Corelang.Error (Location.symbol_rloc (), Corelang.Already_bound_symbol msg))
54
    | _                 -> assert false
55
  with
56
    Not_found ->
57
       Hashtbl.add hashtbl name value
58

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

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

    
69
%}
70

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

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

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

    
121
%%
122

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

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

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

    
133
open_lusi:
134
| OPEN QUOTE IDENT QUOTE { mktop_decl (Open (true, $3))}
135
| OPEN LT IDENT GT { mktop_decl (Open (false, $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
| CONST cdecl_list { fun _ -> mktop_decl (Consts (List.rev $2)) }
152
| nodespec_list state_annot IDENT LPAR vdecl_list SCOL_opt RPAR RETURNS LPAR vdecl_list SCOL_opt RPAR  prototype_opt in_lib_opt SCOL
153
    {let nd = mktop_decl (ImportedNode
154
                            {nodei_id = $3;
155
                             nodei_type = Types.new_var ();
156
                             nodei_clock = Clocks.new_var true;
157
                             nodei_inputs = List.rev $5;
158
                             nodei_outputs = List.rev $10;
159
			     nodei_stateless = $2;
160
			     nodei_spec = $1;
161
			     nodei_prototype = $13;
162
			     nodei_in_lib = $14;})
163
    in
164
    (fun own -> add_node own ("node " ^ $3) node_table $3 nd; nd) }
165

    
166
prototype_opt:
167
 { None }
168
| PROTOTYPE IDENT { Some $2}
169

    
170
in_lib_opt:
171
{ None }
172
| IN IDENT {Some $2} 
173

    
174
top_decl:
175
| CONST cdecl_list { mktop_decl (Consts (List.rev $2)) }
176
| 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 
177
    {let eqs, asserts, annots = $16 in
178
     let nd = mktop_decl (Node
179
                            {node_id = $3;
180
                             node_type = Types.new_var ();
181
                             node_clock = Clocks.new_var true;
182
                             node_inputs = List.rev $5;
183
                             node_outputs = List.rev $10;
184
                             node_locals = List.rev $14;
185
			     node_gencalls = [];
186
			     node_checks = [];
187
			     node_asserts = asserts; 
188
                             node_eqs = eqs;
189
			     node_dec_stateless = $2;
190
			     node_stateless = None;
191
			     node_spec = $1;
192
			     node_annot = match annots with [] -> None | _ -> Some annots})
193
    in
194
    add_node true ("node " ^ $3) node_table $3 nd; nd}
195

    
196
nodespec_list:
197
 { None }
198
| NODESPEC nodespec_list { (function None -> (fun s1 -> Some s1) | Some s2 -> (fun s1 -> Some (LustreSpec.merge_node_annot s1 s2))) $2 $1 }
199

    
200
typ_def_list:
201
    /* empty */ {}
202
| typ_def SCOL typ_def_list {$1;$3}
203

    
204
typ_def:
205
  TYPE IDENT EQ typeconst {
206
    try
207
      add_symbol ("type " ^ $2) type_table (Tydec_const $2) (Corelang.get_repr_type $4)
208
    with Not_found-> assert false }
209
| TYPE IDENT EQ ENUM LCUR tag_list RCUR { Hashtbl.add type_table (Tydec_const $2) (Tydec_enum ($6 (Tydec_const $2))) }
210
| TYPE IDENT EQ STRUCT LCUR field_list RCUR { Hashtbl.add type_table (Tydec_const $2) (Tydec_struct ($6 (Tydec_const $2))) }
211

    
212
array_typ_decl:
213
                            { fun typ -> typ }
214
 | POWER dim array_typ_decl { fun typ -> $3 (Tydec_array ($2, typ)) }
215

    
216
typeconst:
217
  TINT array_typ_decl  { $2 Tydec_int }
218
| TBOOL array_typ_decl { $2 Tydec_bool  }
219
| TREAL array_typ_decl { $2 Tydec_real  }
220
| TFLOAT array_typ_decl { $2 Tydec_float }
221
| IDENT array_typ_decl { check_symbol ("type " ^ $1) type_table (Tydec_const $1); $2 (Tydec_const $1) }
222
| TBOOL TCLOCK  { Tydec_clock Tydec_bool }
223
| IDENT TCLOCK  { Tydec_clock (Tydec_const $1) }
224

    
225
tag_list:
226
  IDENT
227
  { (fun t -> add_symbol ("tag " ^ $1) tag_table $1 t; $1 :: []) }
228
| tag_list COMMA IDENT
229
  { (fun t -> add_symbol ("tag " ^ $3)tag_table $3 t; $3 :: ($1 t)) }
230

    
231
field_list:
232
  { (fun t -> []) }
233
| field_list IDENT COL typeconst SCOL
234
  { (fun t -> add_symbol ("field " ^ $2) field_table $2 t; ($1 t) @ [ ($2, $4) ]) }
235

    
236
eq_list:
237
  { [], [], [] }
238
| eq eq_list {let eql, assertl, annotl = $2 in ($1::eql), assertl, annotl}
239
| assert_ eq_list {let eql, assertl, annotl = $2 in eql, ($1::assertl), annotl}
240
| ANNOT eq_list {let eql, assertl, annotl = $2 in eql, assertl, $1@annotl}
241
| automaton eq_list {let eql, assertl, annotl = $2 in ($1::eql), assertl, annotl}
242

    
243
automaton:
244
 AUTOMATON IDENT handler_list { failwith "not implemented" }
245

    
246
handler_list:
247
     { [] }
248
| handler handler_list { $1::$2 }
249

    
250
handler:
251
 STATE IDENT ARROW unless_list locals LET eq_list TEL until_list { () }
252

    
253
unless_list:
254
    { [] }
255
| unless unless_list { $1::$2 }
256

    
257
until_list:
258
    { [] }
259
| until until_list { $1::$2 }
260

    
261
unless:
262
  UNLESS expr RESTART IDENT { }
263
| UNLESS expr RESUME IDENT  { }
264

    
265
until:
266
  UNTIL expr RESTART IDENT { }
267
| UNTIL expr RESUME IDENT  { }
268

    
269
assert_:
270
| ASSERT expr SCOL {mkassert ($2)}
271

    
272
eq:
273
       ident_list      EQ expr SCOL {mkeq (List.rev $1,$3)}
274
| LPAR ident_list RPAR EQ expr SCOL {mkeq (List.rev $2,$5)}
275

    
276
tuple_expr:
277
    expr COMMA expr {[$3;$1]}
278
| tuple_expr COMMA expr {$3::$1}
279

    
280
// Same as tuple expr but accepting lists with single element
281
array_expr:
282
  expr {[$1]}
283
| expr COMMA array_expr {$1::$3}
284

    
285
dim_list:
286
  dim RBRACKET { fun base -> mkexpr (Expr_access (base, $1)) }
287
| dim RBRACKET LBRACKET dim_list { fun base -> $4 (mkexpr (Expr_access (base, $1))) }
288

    
289
expr:
290
/* constants */
291
  INT {mkexpr (Expr_const (Const_int $1))}
292
| REAL {mkexpr (Expr_const (Const_real $1))}
293
| FLOAT {mkexpr (Expr_const (Const_float $1))}
294
/* Idents or type enum tags */
295
| IDENT {
296
  if Hashtbl.mem tag_table $1
297
  then mkexpr (Expr_const (Const_tag $1))
298
  else mkexpr (Expr_ident $1)}
299
| LPAR ANNOT expr RPAR
300
    {update_expr_annot $3 $2}
301
| LPAR expr RPAR
302
    {$2}
303
| LPAR tuple_expr RPAR
304
    {mkexpr (Expr_tuple (List.rev $2))}
305

    
306
/* Array expressions */
307
| LBRACKET array_expr RBRACKET { mkexpr (Expr_array $2) }
308
| expr POWER dim { mkexpr (Expr_power ($1, $3)) }
309
| expr LBRACKET dim_list { $3 $1 }
310

    
311
/* Temporal operators */
312
| PRE expr 
313
    {mkexpr (Expr_pre $2)}
314
| expr ARROW expr 
315
    {mkexpr (Expr_arrow ($1,$3))}
316
| expr FBY expr 
317
    {(*mkexpr (Expr_fby ($1,$3))*)
318
      mkexpr (Expr_arrow ($1, mkexpr (Expr_pre $3)))}
319
| expr WHEN IDENT 
320
    {mkexpr (Expr_when ($1,$3,tag_true))}
321
| expr WHENNOT IDENT
322
    {mkexpr (Expr_when ($1,$3,tag_false))}
323
| expr WHEN IDENT LPAR IDENT RPAR
324
    {mkexpr (Expr_when ($1, $5, $3))}
325
| MERGE IDENT handler_expr_list
326
    {mkexpr (Expr_merge ($2,$3))}
327

    
328
/* Applications */
329
| IDENT LPAR expr RPAR
330
    {mkexpr (Expr_appl ($1, $3, None))}
331
| IDENT LPAR expr RPAR EVERY IDENT
332
    {mkexpr (Expr_appl ($1, $3, Some ($6, tag_true)))}
333
| IDENT LPAR expr RPAR EVERY IDENT LPAR IDENT RPAR
334
    {mkexpr (Expr_appl ($1, $3, Some ($8, $6))) }
335
| IDENT LPAR tuple_expr RPAR
336
    {mkexpr (Expr_appl ($1, mkexpr (Expr_tuple (List.rev $3)), None))}
337
| IDENT LPAR tuple_expr RPAR EVERY IDENT
338
    {mkexpr (Expr_appl ($1, mkexpr (Expr_tuple (List.rev $3)), Some ($6, tag_true))) }
339
| IDENT LPAR tuple_expr RPAR EVERY IDENT LPAR IDENT RPAR
340
    {mkexpr (Expr_appl ($1, mkexpr (Expr_tuple (List.rev $3)), Some ($8, $6))) }
341

    
342
/* Boolean expr */
343
| expr AND expr 
344
    {mkpredef_call "&&" [$1;$3]}
345
| expr AMPERAMPER expr 
346
    {mkpredef_call "&&" [$1;$3]}
347
| expr OR expr 
348
    {mkpredef_call "||" [$1;$3]}
349
| expr BARBAR expr 
350
    {mkpredef_call "||" [$1;$3]}
351
| expr XOR expr 
352
    {mkpredef_call "xor" [$1;$3]}
353
| NOT expr 
354
    {mkpredef_unary_call "not" $2}
355
| expr IMPL expr 
356
    {mkpredef_call "impl" [$1;$3]}
357

    
358
/* Comparison expr */
359
| expr EQ expr 
360
    {mkpredef_call "=" [$1;$3]}
361
| expr LT expr 
362
    {mkpredef_call "<" [$1;$3]}
363
| expr LTE expr 
364
    {mkpredef_call "<=" [$1;$3]}
365
| expr GT expr 
366
    {mkpredef_call ">" [$1;$3]}
367
| expr GTE  expr 
368
    {mkpredef_call ">=" [$1;$3]}
369
| expr NEQ expr 
370
    {mkpredef_call "!=" [$1;$3]}
371

    
372
/* Arithmetic expr */
373
| expr PLUS expr 
374
    {mkpredef_call "+" [$1;$3]}
375
| expr MINUS expr 
376
    {mkpredef_call "-" [$1;$3]}
377
| expr MULT expr 
378
    {mkpredef_call "*" [$1;$3]}
379
| expr DIV expr 
380
    {mkpredef_call "/" [$1;$3]}
381
| MINUS expr %prec UMINUS
382
  {mkpredef_unary_call "uminus" $2}
383
| expr MOD expr 
384
    {mkpredef_call "mod" [$1;$3]}
385

    
386
/* If */
387
| IF expr THEN expr ELSE expr
388
    {mkexpr (Expr_ite ($2, $4, $6))}
389

    
390
handler_expr_list:
391
   { [] }
392
| handler_expr handler_expr_list { $1 :: $2 }
393

    
394
handler_expr:
395
 LPAR IDENT ARROW expr RPAR { ($2, $4) }
396

    
397
signed_const_array:
398
| signed_const { [$1] }
399
| signed_const COMMA signed_const_array { $1 :: $3 }
400

    
401
signed_const_struct:
402
| IDENT EQ signed_const { [ ($1, $3) ] }
403
| IDENT EQ signed_const COMMA signed_const_struct { ($1, $3) :: $5 }
404

    
405
signed_const:
406
  INT {Const_int $1}
407
| REAL {Const_real $1}
408
| FLOAT {Const_float $1}
409
| IDENT {Const_tag $1}
410
| MINUS INT {Const_int (-1 * $2)}
411
| MINUS REAL {Const_real ("-" ^ $2)}
412
| MINUS FLOAT {Const_float (-1. *. $2)}
413
| LCUR signed_const_struct RCUR { Const_struct $2 }
414
| LBRACKET signed_const_array RBRACKET { Const_array $2 }
415

    
416
dim:
417
   INT { mkdim_int $1 }
418
| LPAR dim RPAR { $2 }
419
| IDENT { mkdim_ident $1 }
420
| dim AND dim 
421
    {mkdim_appl "&&" [$1;$3]}
422
| dim AMPERAMPER dim 
423
    {mkdim_appl "&&" [$1;$3]}
424
| dim OR dim 
425
    {mkdim_appl "||" [$1;$3]}
426
| dim BARBAR dim 
427
    {mkdim_appl "||" [$1;$3]}
428
| dim XOR dim 
429
    {mkdim_appl "xor" [$1;$3]}
430
| NOT dim 
431
    {mkdim_appl "not" [$2]}
432
| dim IMPL dim 
433
    {mkdim_appl "impl" [$1;$3]}
434

    
435
/* Comparison dim */
436
| dim EQ dim 
437
    {mkdim_appl "=" [$1;$3]}
438
| dim LT dim 
439
    {mkdim_appl "<" [$1;$3]}
440
| dim LTE dim 
441
    {mkdim_appl "<=" [$1;$3]}
442
| dim GT dim 
443
    {mkdim_appl ">" [$1;$3]}
444
| dim GTE  dim 
445
    {mkdim_appl ">=" [$1;$3]}
446
| dim NEQ dim 
447
    {mkdim_appl "!=" [$1;$3]}
448

    
449
/* Arithmetic dim */
450
| dim PLUS dim 
451
    {mkdim_appl "+" [$1;$3]}
452
| dim MINUS dim 
453
    {mkdim_appl "-" [$1;$3]}
454
| dim MULT dim 
455
    {mkdim_appl "*" [$1;$3]}
456
| dim DIV dim 
457
    {mkdim_appl "/" [$1;$3]}
458
| MINUS dim %prec UMINUS
459
  {mkdim_appl "uminus" [$2]}
460
| dim MOD dim 
461
    {mkdim_appl "mod" [$1;$3]}
462
/* If */
463
| IF dim THEN dim ELSE dim
464
    {mkdim_ite $2 $4 $6}
465

    
466
locals:
467
  {[]}
468
| VAR vdecl_list SCOL {$2}
469

    
470
vdecl_list:
471
    vdecl {$1}
472
| vdecl_list SCOL vdecl {$3 @ $1}
473

    
474
vdecl:
475
/* Useless no ?*/    ident_list
476
    {List.map mkvar_decl 
477
        (List.map (fun id -> (id, mktyp Tydec_any, mkclock Ckdec_any, false)) $1)}
478

    
479
| ident_list COL typeconst clock 
480
    {List.map mkvar_decl (List.map (fun id -> (id, mktyp $3, $4, false)) $1)}
481
| CONST ident_list COL typeconst /* static parameters don't have clocks */
482
    {List.map mkvar_decl (List.map (fun id -> (id, mktyp $4, mkclock Ckdec_any, true)) $2)}
483

    
484
cdecl_list:
485
  cdecl SCOL { [$1] }
486
| cdecl_list cdecl SCOL { $2::$1 }
487

    
488
cdecl:
489
    IDENT EQ signed_const {
490
      let c = {
491
	const_id = $1;
492
	const_loc = Location.symbol_rloc ();
493
        const_type = Types.new_var ();
494
	const_value = $3;
495
      } in
496
      Hashtbl.add consts_table $1 c; c
497
    }
498

    
499
clock:
500
    {mkclock Ckdec_any}
501
| when_list
502
    {mkclock (Ckdec_bool (List.rev $1))}
503

    
504
when_cond:
505
    WHEN IDENT {($2, tag_true)}
506
| WHENNOT IDENT {($2, tag_false)}
507
| WHEN IDENT LPAR IDENT RPAR {($4, $2)}
508

    
509
when_list:
510
    when_cond {[$1]}
511
| when_list when_cond {$2::$1}
512

    
513
ident_list:
514
  IDENT {[$1]}
515
| ident_list COMMA IDENT {$3::$1}
516

    
517
SCOL_opt:
518
    SCOL {} | {}