Initial version of donated sources by Avertec, 3.4p5.
[tas-yagle.git] / distrib / sources / api / api / gen_library_parser.y
1 /****************************************************************************/
2 /* */
3 /* Chaine de CAO & VLSI Alliance */
4 /* */
5 /* Produit : GENIUS v1.00 */
6 /* Fichier : gen_library_parser.yac */
7 /* */
8 /* (c) copyright 1999 Laboratoire MASI equipe CAO & VLSI */
9 /* Tous droits reserves */
10 /* Support : e-mail alliance-support@asim.lip6.fr */
11 /* */
12 /* Auteur(s) : Francois DONNET le : 04/05/1999 */
13 /* */
14 /* Modifie par : le : ../../.... */
15 /* Modifie par : le : ../../.... */
16 /* Modifie par : le : ../../.... */
17 /* */
18 /****************************************************************************/
19
20
21 %{
22 #include <limits.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include MUT_H
29 //#include MLO_H
30 #define API_HIDE_TOKENS
31 #include API_H
32
33 #define yytext gen_library_parsertext
34 #define yyin gen_library_parserin
35
36 #define MAXENTRY 100
37
38
39 /*global variable*/
40 static char *PATH; /*path for files*/
41 static char *file, *subfig, *bbox; /*name of the library*/
42 static int order, format, keep, addaction, paramset;
43 /* syntax error schedule */
44 extern int yylex(); /* parse ahead (by step of one char)*/
45 extern int lineno_library; /* line number to give user's information */
46 extern char *yytext; /* current parsed string */
47 extern FILE *yyin; /* flow for LEX */
48 static inline int yyerror(); /* display error message */
49 static void forceexit();
50
51 /*for internal use*/
52 static lib_entry *put_inlist_warn(char *elem, lib_entry *liste, int warn);
53 lib_entry *TakeDependancies(char *elem, lib_entry *liste);
54 static lib_entry *head;
55
56 %}
57
58
59 %union {
60 char *string;
61 char name[400];
62 int integer;
63 ptype_list *type;
64 chain_list *chain_l;
65 lib_entry *le;
66 }
67
68
69 %type <name> file_name
70 %type <le> files_list template files_list_sub .rule. .actions.
71 %type <chain_l> .define.
72 %start liste
73
74 %token _STOP _YES _NO _KEEP _FORMAT _PRIORITY _VHDL _SPICE _RULE _ACTIONS
75 %token <string> _IDENT
76 %token <integer> _DIGIT
77 %%
78
79
80 /****************************************************************************/
81 /* A library file can contain several model files */
82 /****************************************************************************/
83
84 liste:
85 files_list { head=$1; }
86 | { head=NULL; }
87 ;
88
89 files_list:
90 file_name {order=lineno_library; format=keep=0; bbox=subfig=NULL;} .params.ka. ';' {$$=put_inlist_warn($1,NULL, 1);}
91 |
92 '{' file_name '}' {order=lineno_library; format=keep=0; bbox=subfig=NULL;} .params.ka. ';'
93 {
94 char temp[512];
95 char temp2[512];
96 sprintf(temp, "%s/%s", getdistpath_2(temp2), $2);
97 $$=put_inlist_warn(temp,NULL, 1);
98 }
99 |
100 template { $$=$1; }
101 |
102 files_list file_name {order=lineno_library; format=keep=0; bbox=subfig=NULL; } .params.ka. ';' {$$=put_inlist_warn($2,$1, 1);}
103 |
104 files_list template { $$=(lib_entry *)append((chain_list *)$1, (chain_list *)$2); }
105 |
106 files_list '{' file_name '}' {order=lineno_library; format=keep=0; bbox=subfig=NULL;} .params.ka. ';'
107 {
108 char temp[512];
109 char temp2[512];
110 sprintf(temp, "%s/%s", getdistpath_2(temp2), $3);
111 $$=put_inlist_warn(temp,$1, 1);
112 }
113 ;
114
115 template:
116 _IDENT _IDENT '{' .rule. .actions. .define. '}' {order=-1; format=keep=-1; bbox=subfig=NULL; } .params.ka. ';'
117 {
118 chain_list *cl;
119 lib_entry *le, *lepack;
120 char temp[1024];
121
122 for (le=$4; le!=NULL; le=le->NEXT)
123 {
124 if (keep!=-1) le->keep=keep;
125 if (order!=-1) le->priority=order;
126 le->model=namealloc($1);
127 }
128
129 if ($4==NULL)
130 {
131 if (order==-1) order=lineno_library;
132 if (keep==-1) keep=0;
133 format=0;
134 sprintf(temp,"%s.vhd", $1);
135 $4=put_inlist_warn(temp,NULL, 1);
136 $4->model=namealloc($1);
137 }
138
139 if ($5==NULL)
140 {
141 order=lineno_library; format=keep=0;
142 sprintf(temp,"%s.c", $1);
143 $5=put_inlist_warn(temp,NULL, 1);
144 }
145
146 lepack=(lib_entry *)append((chain_list *)$4, (chain_list *)$5);
147
148 cl=addchain($6, create_adt($1, $2));
149 for (le=lepack; le!=NULL; le=le->NEXT)
150 {
151 if (le==lepack)
152 le->paramset=cl;
153 else
154 le->paramset=dup_adt_list(cl);
155 if (gen_find_template_corresp(NULL, le->paramset, "exec_function", temp)==0)
156 {
157 if (strcmp(temp,"exec_function")!=0)
158 le->model=namealloc(temp);
159 }
160 }
161 $$=lepack;
162 mbkfree($1);
163 mbkfree($2);
164 }
165 ;
166
167 .rule.:
168 _RULE '=' '{' files_list_sub '}' { $$=$4; }
169 |
170 _RULE '=' file_name {order=lineno_library; format=keep=0; bbox=subfig=NULL;} .params.ka. ';' {$$=put_inlist_warn($3, NULL, 1);}
171 |
172 { $$ =NULL; }
173 ;
174
175 .actions.:
176 _ACTIONS '=' '{' files_list_sub '}' { $$=$4; }
177 |
178 _ACTIONS '=' file_name {order=lineno_library; format=keep=0; bbox=subfig=NULL;} .params.ka. ';' {$$=put_inlist_warn($3, NULL, 1);}
179 |
180 { $$=NULL; }
181 ;
182
183 files_list_sub:
184 files_list_sub file_name {order=lineno_library; format=keep=0; bbox=subfig=NULL;} .params.ka. ';' {$$=put_inlist_warn($2, $1, 1);}
185 |
186 file_name {order=lineno_library; format=keep=0; bbox=subfig=NULL;} .params.ka. ';' {$$=put_inlist_warn($1, NULL, 1);}
187 ;
188
189 .define.:
190 _IDENT '=' _IDENT
191 {
192 $$=addchain(NULL, create_adt($1, $3));
193 mbkfree($1);
194 mbkfree($3);
195 }
196 |
197 .define. ',' _IDENT '=' _IDENT
198 {
199 $$=addchain($1, create_adt($3, $5));
200 mbkfree($5);
201 mbkfree($3);
202 }
203 |
204 {
205 $$=NULL;
206 }
207 ;
208
209 .params.ka.:
210 ':' .params.
211 |
212 ;
213
214 .params.:
215 .order. .more.params.
216 |
217 ;
218
219 .more.params.:
220 ',' .params.
221 |
222 ;
223
224 .order.:
225 _IDENT '=' _DIGIT
226 {
227 if (strcasecmp($1,"priority")==0) order =INT_MIN+$3;
228 else { yyerror(); YYABORT; }
229 mbkfree($1);
230 }
231 |
232 _IDENT '=' _IDENT
233 {
234 if (strcasecmp($1,"format")==0)
235 {
236 if (strcasecmp($3, "spice")==0) format = 1;
237 else if (strcasecmp($3, "vhdl")==0) format = 0;
238 else if (strcasecmp($3, "spice_hr")==0) format = 2;
239 else { yyerror(); YYABORT; }
240 }
241 else if (strcasecmp($1,"keep")==0)
242 {
243 if (strcasecmp($3, "yes")==0) keep = 1;
244 else if (strcasecmp($3, "no")==0) keep = 0;
245 else { yyerror(); YYABORT; }
246 }
247 else if (strcasecmp($1,"subfigure")==0)
248 {
249 subfig=mbkstrdup($3);
250 }
251 else if (strcasecmp($1,"match")==0)
252 {
253 bbox=mbkstrdup($3);
254 }
255 else
256 { yyerror(); YYABORT; }
257 mbkfree($1);
258 mbkfree($3);
259 }
260 ;
261
262 .action_type.:
263 '(' _VHDL ')' { addaction=1; }
264 |
265 ;
266
267 file_name:
268 file_name '.' _IDENT { sprintf($$,"%s.%s",$1,$3); mbkfree($3); }
269 |
270 file_name '/' _IDENT { sprintf($$,"%s/%s",$1,$3); mbkfree($3); }
271 |
272 _IDENT
273 {
274 strcpy($$, $1);
275 // sprintf($$,"%s%s",PATH,$1);
276 mbkfree($1);
277 /*char *name = (char*)mbkalloc(strlen(PATH)+strlen($1)+1);
278 name=strcpy(name, PATH);
279 name=strcat(name, $1);
280 mbkfree($1);
281 $$=name;*/
282 }
283 |
284 '/' _IDENT
285 {
286 sprintf($$, "/%s", $2);
287 // sprintf($$,"%s%s",PATH,$1);
288 mbkfree($2);
289 }
290 ;
291
292
293
294 %%
295
296
297 /****************************************************************************/
298 /* user's message for syntaxical error */
299 /****************************************************************************/
300 static inline int yyerror()
301 {
302 switch ((int)yychar) {
303 case _STOP:
304 fprintf(stderr,"%s:%d: unexpected end of file\n",file,lineno_library);
305 break;
306 default:
307 fprintf(stderr,"%s:%d: syntax error happened at '%s'\n",file,lineno_library,yytext);
308 break;
309 }
310 Inc_Error();
311 return lineno_library;
312 }
313
314
315
316 /****************************************************************************/
317 /* return the liste sorted by its order contained in TYPE field */
318 /* the first(or with the lowest digit) in first place in the list */
319 /****************************************************************************/
320 static lib_entry* sort_by_order(lib_entry *liste)
321 {
322 int current_priority;
323 lib_entry *p,*q,*pred,*first,*ret;
324
325 if (!liste) return NULL;
326
327 /*bubble sort*/
328 current_priority=liste->priority;
329 q=NULL;
330 pred=NULL;
331 for (p=liste;p;p=p->NEXT) {
332 if (p->priority<current_priority) {
333 current_priority=p->priority;
334 pred=q;
335 }
336 q=p;
337 }
338
339 if (!pred) {/*first in liste to put on first place*/
340 ret=sort_by_order(liste->NEXT);
341 liste->NEXT=ret;
342 return liste;
343 }
344 else {/*an another than the first*/
345 first=pred->NEXT;
346 pred->NEXT=first->NEXT; /*put over the first*/
347 ret=sort_by_order(liste);
348 first->NEXT=ret;
349 return first;
350 }
351 }
352
353 /****************************************************************************/
354 /* to get started */
355 /* return list of model files sorted by order:first as the highest priority */
356 /* if number is attached the lowest digit for the highest priority */
357 /****************************************************************************/
358 lib_entry *APIReadLibrary(char *library, char *path, FILE *debug)
359 {
360 lib_entry *res;
361 if (!library || !path) {
362 fprintf(stderr,"Read_All: NULL pointer\n");
363 EXIT(1);
364 }
365
366 PATH=path; /* var. glob */
367 file=library; /*var. glob. */
368 /* model file exists? */
369 if (!(yyin = mbkfopen(library, NULL, READ_TEXT))) {
370 fprintf(stderr, "Cannot open library file %s (error n°%d)\n",
371 library,errno);
372 EXIT(-2);
373 }
374 if (debug)
375 fprintf(debug, "Opening library file %s....\n",library);
376 paramset=0;
377 yyparse();
378
379 res = head;
380
381 res= sort_by_order(res);
382
383 fclose(yyin);
384
385 if (debug)
386 fprintf(debug, "%s Closed\n",library);
387
388 return res;
389 }
390
391
392 /****************************************************************************/
393 /* to get started */
394 /****************************************************************************/
395 static lib_entry *put_inlist_warn(char *elem, lib_entry *liste, int warn)
396 {
397 lib_entry *le,*le1;
398
399 if (!elem) {
400 fprintf(stderr,"put_inlist_warn: NULL pointer\n");
401 EXIT(1);
402 }
403
404 // sprintf(temp,"%s%s",path, elem);
405
406 for (le=liste;le!=NULL;le=le->NEXT)
407 {
408 if (warn && !strcmp(le->name,elem))
409 {
410 fprintf(stderr,"%s:%d: %s named several times. this one is ignored\n",file,lineno_library-1,elem);
411 return liste;
412 }
413 if (!le->NEXT)
414 {
415 le1=(lib_entry *)mbkalloc(sizeof(lib_entry));
416 le->NEXT=le1;
417 le1->name=mbkstrdup(elem);
418 le1->subfigurename=subfig;
419 le1->match=bbox;
420 le1->entity="?";
421 le1->priority=order;
422 le1->format=format;
423 le1->paramset=NULL;
424 le1->model=NULL;
425 le1->keep=keep;
426 le1->tree=NULL;
427 le1->NEXT=NULL;
428 return liste;
429 }
430 }
431
432 le=(lib_entry *)mbkalloc(sizeof(lib_entry));
433 le->name=mbkstrdup(elem);
434 le->subfigurename=subfig;
435 le->match=bbox;
436 le->entity="?";
437 le->priority=order;
438 le->format=format;
439 le->paramset=NULL;
440 le->model=NULL;
441 le->keep=keep;
442 le->tree=NULL;
443 le->NEXT=NULL;
444 return le;
445 }
446
447
448
449 /****************************************************************************/
450 /*************************** END of LEX&YACC ********************************/
451 /****************************************************************************/
452
453 void APIFreeLibrary(lib_entry *le)
454 {
455 lib_entry *nle;
456 while (le!=NULL)
457 {
458 nle=le->NEXT;
459 free(le->name);
460 if (le->subfigurename!=NULL) free(le->subfigurename);
461 if (le->match!=NULL) free(le->match);
462 free_adt_list(le->paramset);
463 mbkfree(le);
464 le=nle;
465 }
466 }