1 %{
2 #include "visutypes.h"
3 #include "gramm_fctn.h"
4 #include "grammy.h"
5 #include "gtk_windows.h"
6 #include "gtk_properties.h"
7 #include "gtk_domain.h"
8 #include "domains.h"
9 #include "gtk_context.h"
10
11 int reinit = FALSE;
12 gint row = -1;
13 Domain * domain_cur = NULL;
14
15
16
17 %}
18
19
20 %union {
21 char *Chain;
22 Value Val;
23 int Op;
24 struct {
25 IndList Mline;
26 Value Const;
27 } A_Exp;
28 IndList Mat_line;
29 ChainList List;
30 }
31
32
33 %token ID HELP INTER UNION DIFF VALUE IMAGE PREIMAGE INITVISU VISU PRINT PLPRINT PLCONS PLRAYS DD LIST EHRHART VERTICES CONVEX DELETE QUIT LT GT LE LG EQ AFF '(' ')' '{' '}' ',' '.' ';' '+' '-' '*' PIPE
34
35 %type <Chain> ID
36 %type <Val> VALUE VAL
37 %type <List> L_VAR
38 %type <A_Exp> ARTHM_EXP INEG EG
39 %type <Mat_line> L_VALUE
40 %type <Op> OPREL
41
42 %left LE GE GT LT
43 %left '+' '-'
44 %left '*'
45
46
47 %start COMMANDS
48
49
50
51
52
53 %%
54
55 COMMANDS : L_INSTR {}
56 ;
57
58 /*********************/
59 /* instructions list */
60 /*********************/
61 L_INSTR : L_INSTR INSTR {}
62 | INSTR {}
63 ;
64
65 /*********************/
66 /* shortcuts */
67 /*********************/
68 Union: UNION | '+' ;
69 Inter: INTER | '.' ;
70 Diff: DIFF | '-' ;
71
72 /**************************/
73 /**************************/
74 /****** INSTRUCTIONS ******/
75 /**************************/
76 /**************************/
77
78 /**************************/
79 /* polyhedron declaration */
80 /**************************/
81
82 // empty polyhedron
83 INSTR:
84 ID AFF '{' PIPE '}' ';'
85 {
86 G_LOCK (verrou_domains);
87 DomainList = polyhedron_declaration ($1, NULL, DomainList);
88 //graphic_actualize();
89 G_UNLOCK (verrou_domains);
90 }
91
92 | ID AFF '{' L_VAR PIPE
93 { curr_matrix = Matrix_Alloc (WS, dim+1); }
94 L_CONSTRAINTS '}' ';'
95 {
96 G_LOCK (verrou_domains);
97 DomainList = polyhedron_declaration ($1, VarList, DomainList);
98 graphic_actualize();
99 G_UNLOCK (verrou_domains);
100 }
101
102 | ID AFF '{' L_VAR PIPE error ';'
103 {
104 yyclearin; //discards lookahead
105 yyerrok;
106 init_curr_vars();
107 printf (">> error in constraints \n");
108 }
109 | ID AFF '{' L_VAR error ';'
110 {
111 yyclearin; //discards lookahead
112 yyerrok;
113 init_curr_vars();
114 printf (">> '|' missing\n");
115 }
116 | ID AFF '{' error ';'
117 {
118 yyclearin; //discards lookahead
119 yyerrok;
120 init_curr_vars();
121 printf (">> error in variable list\n");
122 }
123
124 /* matrix declaration */
125 | ID AFF '(' MATRIX ')' ';'
126 { MatrixList = add_matrix_to_list ($1, MatrixList); }
127
128
129 /*************************************/
130 /* INSTR : operations on polyhedrons */
131 /*************************************/
132
133 | ID AFF ID Union ID ';'
134 {
135 G_LOCK (verrou_domains);
136 DomainList = gen_binop_pol ($1, $3, $5, "union", DomainList);
137 graphic_actualize();
138 G_UNLOCK (verrou_domains);
139 }
140 | ID AFF ID Union error ';'
141 { fprintf (stdout, "> Syntax : 'res' := 'P1' union 'P2' ;\n");}
142
143
144 | ID AFF ID Inter ID ';'
145 {
146 G_LOCK (verrou_domains);
147 DomainList = gen_binop_pol ($1, $3, $5, "inter", DomainList);
148 graphic_actualize();
149 G_UNLOCK (verrou_domains);
150 }
151 | ID AFF ID Inter error ';'
152 { fprintf (stdout, "> Syntax : 'res' := 'P1' inter 'P2' ;\n");}
153
154
155 | ID AFF ID Diff ID ';'
156 {
157 G_LOCK (verrou_domains);
158 DomainList = gen_binop_pol ($1, $3, $5, "diff", DomainList);
159 graphic_actualize();
160 G_UNLOCK (verrou_domains);
161 }
162 | ID AFF ID Diff error ';'
163 { fprintf (stdout, "> Syntax : 'res' := 'P1' diff 'P2' ;\n");}
164
165 | ID AFF IMAGE '(' ID ',' ID ')' ';'
166 {
167 G_LOCK (verrou_domains);
168 DomainList = gen_image ($1, $5, $7, "image", DomainList);
169 graphic_actualize();
170 G_UNLOCK (verrou_domains);
171 }
172 | ID AFF IMAGE error ';'
173 { fprintf (stdout, "> Syntax : 'name' := image ('domain', 'matrix') ;\n");}
174
175
176 | ID AFF PREIMAGE '(' ID ',' ID ')' ';'
177 {
178 G_LOCK (verrou_domains);
179 DomainList = gen_image ($1, $5, $7, "preimage", DomainList);
180 graphic_actualize();
181 G_UNLOCK (verrou_domains);
182 }
183 | ID AFF PREIMAGE error ';'
184 { fprintf (stdout, "> Syntax : 'name' := preimage ('domain', 'matrix') ;\n");}
185
186
187 | ID AFF DD ID ';'
188 {
189 G_LOCK (verrou_domains);
190 DomainList = gen_disjoint ($1, $4, DomainList);
191 graphic_actualize();
192 G_UNLOCK (verrou_domains);
193 }
194
195 | ID AFF CONVEX ID ';'
196 {
197 G_LOCK (verrou_domains);
198 DomainList = gen_convex ($1, $4, DomainList);
199 graphic_actualize();
200 G_UNLOCK (verrou_domains);
201 }
202 | ID AFF CONVEX error ';'
203 { fprintf (stdout, "> Syntax : 'name' := convex 'domain' ;\n");}
204
205
206 | VERTICES '(' ID ',' ID ')' ';'
207 {
208 G_LOCK (verrou_domains);
209 gen_vert ($3, $5, DomainList);
210 G_UNLOCK (verrou_domains);
211 }
212 | VERTICES error ';'
213 { fprintf (stdout, "> Syntax : vertices ('domain', 'context') ;\n");}
214
215 | EHRHART '(' ID ',' ID ')' ';'
216 {
217 G_LOCK (verrou_domains);
218 gen_ehrhart ($3, $5, DomainList);
219 G_UNLOCK (verrou_domains);
220 }
221 | EHRHART error ';'
222 { fprintf (stdout, "> Syntax : ehrhart ('domain', 'context') ;\n");}
223
224 | INITVISU '(' VALUE ',' ID ')' ';'
225 {
226
227 DimNP = (int)$3;
228 if (TmpDomainName)
229 free (TmpDomainName);
230 TmpDomainName = (char *)malloc ((strlen($5)+1)*sizeof(char));
231 strcpy(TmpDomainName, $5);
232 gdk_threads_enter ();
233 init_visu ();
234 gdk_threads_leave ();
235 }
236 | INITVISU error ';'
237 { fprintf (stdout, "> Syntax : initvisu ('dimension', 'context') ;\n");}
238
239 | VISU '(' ID ')' ';'
240 {
241 if (TmpDomainName)
242 free (TmpDomainName);
243 TmpDomainName = (char*)malloc((strlen($3)+1)*sizeof(char));
244 strcpy (TmpDomainName, $3);
245 gdk_threads_enter ();
246 /* Recherche du domaine dans la liste des domaines visualisables */
247 G_LOCK (verrou_domains);
248 for( domain_cur = DomainList; domain_cur; domain_cur = domain_cur->next )
249 {
250 if (strcmp (domain_cur->name, TmpDomainName) == 0)
251 {
252 row = gtk_clist_find_row_from_data (
253 GTK_CLIST (clist),
254 (gpointer) domain_cur);
255 break;
256 }
257 }
258 G_UNLOCK (verrou_domains);
259 if (domain_cur == NULL)
260 {
261 char errmsg[100];
262 sprintf (errmsg, " '%s' undeclared - visu: command ignored", $3);
263 yyerror (errmsg);
264 }
265 else
266 { /* Afficher/Masquer */
267 if (row != -1)
268 reinit = PropertiesActions (GTK_CLIST (clist), row, 1, NULL, NULL);
269 else
270 Warn ("Can't visualize this domain (probably wrong dimension)!");
271 }
272 gdk_threads_leave ();
273
274 }
275 | VISU error ';'
276 { fprintf (stdout, "> Syntax : visu (domain) ;\n"); }
277
278 | LIST ';'
279 {
280 Domain *tmp;
281 ChainMatrix *mat;
282 char **pname;
283 fprintf(stdout, "> Domain List: \n");
284 for(tmp=DomainList; tmp ; tmp=tmp->next )
285 {
286 pname = chaintochar (tmp->v_list);
287 fprintf (stdout, "> %s := ", tmp->name);
288 VP_Print_Domain (stdout, tmp->Pdomain, pname);
289 }
290 fprintf(stdout, "\n");
291 fprintf(stdout, "> Matrix List : \n\t");
292 for( mat = MatrixList; mat ; mat=mat->next )
293 {
294 if (mat->next)
295 fprintf(stdout, "%s, ", mat->name);
296 else
297 fprintf(stdout, "%s\n", mat->name);
298 }
299 printf ("\n");
300 }
301
302 | PRINT ID ';' {
303 char **pname; // param name
304 Domain *dom;
305 Matrix *mat;
306 char errmsg[100];
307
308 dom = search_domain_in_list ($2, DomainList);
309 if (dom)
310 {
311 // conversion de v_list en char**
312 pname = chaintochar (dom->v_list);
313 fprintf (stdout, "> %s := ", $2);
314 VP_Print_Domain (stdout, dom->Pdomain, pname);
315 }
316 else
317 {
318 mat = search_matrix_in_list ($2, MatrixList);
319 if (mat == NULL)
320 {
321 sprintf (errmsg, " '%s' undeclared - print: command ignored", $2);
322 yyerror (errmsg);
323 }
324 else
325 {
326 fprintf(stdout, "> %s := ", $2);
327 VP_Print_Matrix (stdout, mat);
328 }
329 }
330 }
331 | PRINT error ';' { fprintf (stdout, "> Syntax: print 'name' ;\n");}
332
333
334 | PLPRINT ID ';' { /* print domain, polylib format */
335 Domain *tmp;
336 Matrix *mat;
337 char errmsg[100];
338 tmp = search_domain_in_list ($2, DomainList);
339 if (tmp )
340 {
341 fprintf (stdout, "> Domain '%s':\n", $2);
342 Polyhedron_Print (stdout, "%3d", tmp->Pdomain);
343 printf("\n");
344 }
345 else
346 {
347 mat = search_matrix_in_list ($2, MatrixList);
348 if (mat)
349 {
350 fprintf(stdout, "> Matrix '%s':\n", $2);
351 Matrix_Print (stdout, "%4d ", mat);
352 }
353 else
354 {
355 sprintf (errmsg, " '%s' undeclared - PLprint: command ignored", $2);
356 yyerror (errmsg);
357 }
358 }
359 }
360 | PLPRINT error ';' { fprintf (stdout, "> Syntax: PLprint 'name' ;\n"); }
361
362 | PLCONS ID ';' { /* print constraints matrix(-ces), polylib format */
363 Domain *tmp;
364 char errmsg[100];
365 tmp = search_domain_in_list ($2, DomainList);
366 if (tmp == NULL)
367 {
368 sprintf (errmsg, " '%s' undeclared - PLcons: command ignored\n", $2);
369 yyerror (errmsg);
370 }
371 else
372 {
373 fprintf (stdout, "> Domain '%s' - Constraints matrix:\n", $2);
374 Constraints_Print (stdout, "%3d", tmp->Pdomain);
375 printf("\n");
376 }
377 }
378 | PLCONS error ';' { fprintf (stdout, "> Syntax: PLcons 'domain' ;\n"); }
379
380 | PLRAYS ID ';' { /* print rays matrix(-ces), polylib format */
381 Domain *tmp;
382 char errmsg[100];
383 tmp = search_domain_in_list ($2, DomainList);
384 if (tmp == NULL)
385 {
386 sprintf (errmsg, " '%s' undeclared - PLrays: command ignored\n", $2);
387 yyerror (errmsg);
388 }
389 else
390 {
391 fprintf (stdout, "> Domain '%s' - Rays matrix:\n", $2);
392 Rays_Print (stdout, "%3d", tmp->Pdomain);
393 printf("\n");
394 }
395 }
396 | PLRAYS error ';' { fprintf (stdout, "> Syntax: PLrays 'domain' ;\n"); }
397
398
399
400 //erase domain
401 | DELETE ID ';' { Domain *tmp;
402 Matrix *mat;
403 char errmsg[100];
404
405 tmp = search_domain_in_list ($2, DomainList);
406 if (tmp)
407 {
408 DomainList = delete_domain_in_list ($2, DomainList);
409 fprintf(stdout, "> Succeded ! '%s' is deleted from DomainList\n", $2);
410 }
411 else
412 {
413 mat = search_matrix_in_list ($2, MatrixList);
414 if (mat)
415 {
416 MatrixList = delete_matrix_in_list ($2, MatrixList);
417 fprintf(stdout, "> Succeded ! '%s' is deleted from MatrixList\n", $2);
418 }
419 else
420 {
421 sprintf (errmsg, "> '%s' undeclared - del : command ignored\n", $2);
422 yyerror (errmsg);
423 }
424 }
425 }
426
427 | DELETE error ';' { fprintf (stdout, "> Syntax : del 'domain' ;\n"); }
428
429
430
431 | HELP ';' { help_print (); }
432
433
434 // quit the application
435 | QUIT { if (DEBUG == 1)
436 {
437 print_domain_list (DomainList);
438 print_matrix_list (MatrixList);
439 }
440 printf ("\n bye...\n\n");
441 return (-1);
442 }
443
444 | ID AFF error ';' { yyclearin; //discards lookahead
445 yyerrok;
446 printf (">> bad command - type 'help' for informations about syntax \n");
447 }
448
449
450 | ID error ';' { yyclearin; //discards lookahead
451 yyerrok;
452 printf (">> there must be a ':=' after identifier ! \n");
453 }
454
455
456 | error ';' { printf (">> bad command - type 'help' for informations about syntax\n");
457 yyclearin; yyerrok;
458 init_curr_vars();
459 }
460
461 ;
462
463
464
465 /***************/
466 /* matrix form */
467 /***************/
468 MATRIX : MATRIX ',' '(' L_VALUE ')' {
469 IndList tmp = $4;
470 if (curr_matrix)
471 {
472 if (curr_matrix->NbColumns == dim)
473 {
474 while (tmp)
475 {
476 curr_matrix->p[curr_constr_num][tmp->pos] = tmp->val;
477 tmp = tmp->suiv;
478 }
479 curr_constr_num ++;
480 dim = 1;
481 }
482 else
483 {
484 input_error = true;
485 Matrix_Free (curr_matrix);
486 curr_matrix = NULL;
487 fprintf (stdout, "> Matrix declaration : error in dimension\n");
488 }
489 }
490 }
491
492 | '(' L_VALUE ')' { IndList tmp = $2;
493 curr_matrix = Matrix_Alloc (100, dim);
494 while (tmp)
495 {
496 curr_matrix->p[curr_constr_num][tmp->pos] = tmp->val;
497 tmp = tmp->suiv;
498 }
499 curr_constr_num ++;
500 dim = 1;
501 }
502 ;
503
504 /*****************************/
505 /* variables and value lists */
506 /*****************************/
507
508 // value list for matrix declaration
509 L_VALUE : L_VALUE ',' VAL { $$ = (IndList)malloc(sizeof(t_IndList));
510 $$->pos = dim;
511 $$->val = $3;
512 $$->suiv = $1;
513 dim ++;
514 }
515
516
517
518 | VAL { $$ = (IndList)malloc(sizeof(t_IndList));
519 $$->pos = 0;
520 $$->val = $1;
521 $$->suiv = NULL;
522 }
523 ;
524
525 VAL : VALUE { $$ = $1; }
526 | '-' VALUE { $$ = - $2; }
527 | '+' VALUE { $$ = $2; }
528 ;
529
530
531 //liste variables et parametres
532 L_VAR : L_VAR ',' ID { VarList = add_var_to_list ($3, VarList); }
533 | ID { VarList = add_var_to_list ($1, VarList); }
534 ;
535
536 // constraints list
537 L_CONSTRAINTS : L_CONSTRAINTS ',' CONSTRAINT {}
538 | CONSTRAINT {}
539 | {}
540 ;
541
542 /*******************/
543 /* constraint form */
544 /*******************/
545 CONSTRAINT : INEG {}
546 | EG {}
547 ;
548
549
550 // constraint : inequality
551 INEG : INEG OPREL INEG { if (curr_matrix)
552 {
553 if (DEBUG == 1) printf("REDUCE INEG : INEG OP INEG\n");
554 $$.Mline = $1.Mline;
555 $$.Const = $1.Const;
556 if (DEBUG == 1)
557 {
558 printf("Membre gauche : ");
559 Affiche_listeInd ($$.Mline);
560 printf("Membre droit : ");
561 Affiche_listeInd ($3.Mline);
562 }
563 curr_matrix->p[curr_constr_num][0] = 1;
564 switch ($2) {
565 case 1 : /* <= */
566 while ($1.Mline)
567 {
568 curr_matrix->p[curr_constr_num][($1.Mline)->pos] += - ($1.Mline)->val;
569 $1.Mline = ($1.Mline)->suiv;
570 }
571 while ($3.Mline)
572 {
573 curr_matrix->p[curr_constr_num][($3.Mline)->pos] += ($3.Mline)->val;
574 $3.Mline = ($3.Mline)->suiv;
575 }
576
577 /* constante */
578 curr_matrix->p[curr_constr_num][dim] = $3.Const - $1.Const;
579 break;
580
581 case 2 : /* >= */
582 while ($1.Mline)
583 {
584 curr_matrix->p[curr_constr_num][($1.Mline)->pos] += ($1.Mline)->val;
585 $1.Mline = ($1.Mline)->suiv;
586 }
587 while ($3.Mline)
588 {
589 curr_matrix->p[curr_constr_num][($3.Mline)->pos] += - ($3.Mline)->val;
590 $3.Mline = ($3.Mline)->suiv;
591 }
592 curr_matrix->p[curr_constr_num][dim] = $1.Const - $3.Const;
593 break;
594
595 case 3 : /* < */
596 while ($1.Mline)
597 {
598 curr_matrix->p[curr_constr_num][($1.Mline)->pos] += - ($1.Mline)->val;
599 $1.Mline = ($1.Mline)->suiv;
600 }
601 while ($3.Mline)
602 {
603 curr_matrix->p[curr_constr_num][($3.Mline)->pos] += ($3.Mline)->val;
604 $3.Mline = ($3.Mline)->suiv;
605 }
606
607 /* constante */
608 curr_matrix->p[curr_constr_num][dim] = $3.Const - $1.Const - 1;
609 break;
610
611 case 4 : /* > */
612 while ($1.Mline)
613 {
614 curr_matrix->p[curr_constr_num][($1.Mline)->pos] += ($1.Mline)->val;
615 $1.Mline = ($1.Mline)->suiv;
616 }
617 while ($3.Mline)
618 {
619 curr_matrix->p[curr_constr_num][($3.Mline)->pos] += - ($3.Mline)->val;
620 $3.Mline = ($3.Mline)->suiv;
621 }
622 curr_matrix->p[curr_constr_num][dim] = $1.Const - $3.Const - 1;
623 break;
624
625 default : fprintf (stderr, "ineg : ineg op ineg : C'EST PAS NORMAL DETRE LA\n");
626 break;
627 } //switch
628
629 curr_constr_num ++;
630 }
631 }
632
633
634 | ARTHM_EXP { if (DEBUG == 1) printf("REDUCE INEG : ARTHM_EXP\n");
635 $$.Mline = $1.Mline;
636 $$.Const = $1.Const;
637 }
638 ;
639
640
641 // constraint : equality
642 EG : ARTHM_EXP EQ ARTHM_EXP { if (curr_matrix)
643 {
644 if (DEBUG == 1) printf ("REDUCE EG : E = E\n");
645 curr_matrix->p[curr_constr_num][0] = 0;
646 while ($1.Mline)
647 {
648 curr_matrix->p[curr_constr_num][($1.Mline)->pos] += ($1.Mline)->val;
649 $1.Mline = ($1.Mline)->suiv;
650 }
651 while ($3.Mline)
652 {
653 curr_matrix->p[curr_constr_num][($3.Mline)->pos] += - ($3.Mline)->val;
654 $3.Mline = ($3.Mline)->suiv;
655 }
656
657 /* constante */
658 curr_matrix->p[curr_constr_num][dim] = $1.Const - $3.Const;
659 curr_constr_num ++;
660 }
661 }
662 ;
663
664
665 /* operateurs relationnels */
666 OPREL : LT { $$ = 3; } /* < */
667 | GT { $$ = 4; } /* > */
668 | LE { $$ = 1; } /* <= */
669 | GE { $$ = 2; } /* >= */
670 ;
671
672
673
674 /*****************************/
675 /* expressions arithmetiques */
676 /*****************************/
677
678 ARTHM_EXP : ARTHM_EXP '+' ARTHM_EXP { if (DEBUG == 1)
679 printf ("E1 + E2\n");
680 $$.Mline = concat ($1.Mline, $3.Mline);
681 $$.Const = $1.Const + $3.Const;
682 if (DEBUG == 1)
683 Affiche_listeInd ($$.Mline); }
684
685 | ARTHM_EXP '-' ARTHM_EXP { $$.Mline = concat ($1.Mline, neg($3.Mline));
686 $$.Const = $1.Const - $3.Const; }
687
688 | VALUE '*' ARTHM_EXP { $$.Mline = mult ($3.Mline, $1);
689 $$.Const = $1 * $3.Const; }
690
691 | '(' ARTHM_EXP ')' { $$.Mline = $2.Mline;
692 $$.Const = $2.Const; }
693
694 | '-' ARTHM_EXP { $$.Mline = neg($2.Mline);
695 $$.Const = - $2.Const; }
696
697 | VALUE ID { $$.Mline = (IndList)malloc (sizeof(t_IndList));
698 $$.Mline = mult (search_var_in_list ($2, VarList), $1);
699 $$.Const = 0; }
700
701 | ID { $$.Mline = (IndList)malloc (sizeof(t_IndList));
702 $$.Mline = search_var_in_list ($1, VarList);
703 $$.Const = 0;
704 if (DEBUG == 1)
705 Affiche_listeInd ($$.Mline); }
706
707 | VALUE { if (DEBUG == 1) printf ("ValueConst = " P_VALUE_FMT "\n", $1);
708 $$.Mline = NULL;
709 $$.Const = $1; }
710
711 ;
712
713
714 %%
715
716
717
718 ChainMatrix *MatrixList = NULL; /* Matrix List */
719 Domain *DomainList = NULL; /* Domain List */
720 Matrix *curr_matrix; /* current matrix of constraints */
721 int compteur = 1; /* line counter */
722 ChainList VarList = NULL; /* current variables list */
723 unsigned dim = 1; /* dimension of current polyhedron */
724 int curr_constr_num = 0; /* current constraint number */
725 int input_error = false; /* memorize if an error occurs during declaration */
726
727
728 //old
yyerror2(char * s)729 void yyerror2(char *s)
730 {
731 printf("> Error - line %d : %s \n", compteur, s);
732 }
733
734
735