source: git/dyn_modules/modgen/mod_grammar.y @ b5d6f0

fieker-DuValspielwiese
Last change on this file since b5d6f0 was 6ce030f, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
removal of the $Id$ svn tag from everywhere NOTE: the git SHA1 may be used instead (only on special places) NOTE: the libraries Singular/LIB/*.lib still contain the marker due to our current use of svn
  • Property mode set to 100644
File size: 20.7 KB
Line 
1/*
2 */
3
4%{
5
6#include <stdio.h>
7#include <stddef.h>
8#include <stdlib.h>
9#include <stdarg.h>
10#include <string.h>
11
12#include "typmap.h"
13#include "stype.h"
14
15
16int sectnum = 1;
17int iseof = 0;
18int initdone = 0;
19extern moddef module_def;
20extern int yylineno;
21extern int do_create_makefile;
22extern char *sectname[];
23 
24extern int init_modgen(moddef *module_def, char *filename);
25extern int write_intro(moddefv module);
26extern void write_mod_init(moddefv module, FILE *fp);
27extern void enter_id(FILE *fp, char *name, char *value,
28                     int lineno, char *file);
29 
30procdef procedure_decl;
31
32 
33void yyerror(char * fmt)
34  {
35    if(!iseof) printf("%s at line %d\n", fmt, yylineno);
36  }
37
38%}
39
40/* %expect 22 */
41%pure_parser
42
43%token MCOLONCOLON
44%token MEQUAL
45
46/* special symbols */
47%token <i> MMODULE_CMD
48%token <i> MVERSION_CMD
49%token <i> MINFO_CMD
50%token <i> MINFOFILE_CMD
51%token <i> MHELP_CMD
52%token <i> MHELPFILE_CMD
53%token <i> MCXXSRC_CMD
54%token <i> MPROC_CMD
55
56%token SECTEND
57/* SECT2: Singular procedure declaration */
58%token SECT2START
59%token SECT2END
60/* SECT3: procedure declaration */
61%token SECT3START
62%token SECT3END
63/* SECT4: C/C++ text */
64%token SECT4START
65%token <name> SECT4END
66
67/*%token PROCEND*/
68%token PROCDECLTOK
69%token EXAMPLETOK
70%token STATICTOK
71
72/* BISON Declarations */
73
74%token VARNAMETOK
75/*%token MSTRINGTOK */
76%token <sv>   MSTRINGTOK
77%token <name> NAME
78%token <name> FILENAME
79%token <name> MCCODETOK
80%token <name> MCODETOK
81%token <name> CODEPART
82%token <name> CMTPART
83%token <name> PROCCMD
84%token <name> ANYTOK
85%token  <tp> VARTYPETOK
86%token <i>    NUMTOK
87%token <i>    BOOLTOK
88
89%type <i>    '='
90%type <name> identifier
91
92%nonassoc '='
93%%
94goal: part1 sect3 sect3end sect4
95      {
96          if(trace)printf("Finish modules 1\n");
97          return 0;
98      }
99      | part1 sect3full sect4
100      {
101          if(trace)printf("Finish modules 2\n");
102          return 0;
103      }
104      | part1 sect2full sect3full sect4
105      {
106          if(trace)printf("Finish modules 3\n");
107          return 0;
108      }
109      | part1 sect3full sect2full sect4
110      {
111          if(trace)printf("Finish modules 4\n");
112          return 0;
113      }
114;
115
116part1: initmod sect1 sect1end
117        {
118          if(do_create_makefile)mod_create_makefile(&module_def);
119          if(write_intro(&module_def)) {
120            return(myyyerror("Error while creating files\n"));
121          }
122        }
123        | sect1 sect1end
124        {
125          if(do_create_makefile)mod_create_makefile(&module_def);
126          if(write_intro(&module_def)) {
127            return(myyyerror("Error while creating files\n"));
128          }
129        }
130        | sect1end
131        {
132          if(do_create_makefile)mod_create_makefile(&module_def);
133          if(write_intro(&module_def)) {
134            return(myyyerror("Error while creating files\n"));
135          }
136        };
137
138initmod:
139        MCCODETOK
140        {
141          fprintf(module_def.fmtfp, "%s", $1);
142          fprintf(module_def.fmtfp, "#line %d \"%s\"\n",
143                                          yylineno, module_def.filename);
144          free($1);
145          write_mod_init(&module_def, module_def.fmtfp);
146          initdone = 1;
147        };
148
149sect1: expr ';'
150       | sect1 expr ';'
151;
152
153       
154sect1end:
155        codeline2 SECTEND
156        {
157          memset(&procedure_decl, 0, sizeof(procdef));
158          if(debug>2)printf("End of section 1 (new=%d)\n", sectnum);
159        } 
160        | SECTEND
161        {
162          memset(&procedure_decl, 0, sizeof(procdef));
163          if(debug>2)printf("End of section 1 (new=%d)\n", sectnum);
164        }
165        ;
166
167codeline2: CODEPART
168        {
169          if(initdone == 0) {
170            write_mod_init(&module_def, module_def.fmtfp);
171            initdone = 1;
172          }
173          fprintf(module_def.fmtfp, "#line %d \"%s\"\n",
174                                    yylineno, module_def.filename);
175          fprintf(module_def.fmtfp, "%s", $1);
176        }
177        | codeline2 CODEPART
178        {
179        fprintf(module_def.fmtfp, "%s", $2);
180        }
181        ;
182
183expr:   NAME '=' MSTRINGTOK
184        {
185          var_token vt;
186          int rc = 0;
187          void (*write_cmd)(moddefv module, var_token type,
188                            idtyp t, void *arg1, void *arg2);
189         
190          switch(sectnum) {
191              case 1: /* pass 1: */
192                if( (vt=checkvar($1, VAR_STRING, &write_cmd)) ) {
193                  if(write_cmd!=0)
194                    write_cmd(&module_def, vt, STRING_CMD, $1, $3.string);
195                  if(vt==VAR_VERSION)
196                    make_version($3.string, &module_def);
197                  if(vt==VAR_MODULE)
198                    make_module_name($3.string, &module_def);
199                }
200                else {
201                  rc=myyyerror("Line %d: Unknown variable '%s' in section %d\n",
202                            yylineno, $1, sectnum);
203                }
204                break;
205              case 3: /* pass 3: procedure declaration */
206                if( (vt=checkvar($1, VAR_STRING, &write_cmd)) ) {
207                  proc_set_var(&procedure_decl, VAR_STRING, vt, $1, $3.string);
208                }
209                else {
210                  rc=myyyerror("Line %d: Unknown variable '%s' in section %d\n",
211                            yylineno, $1, sectnum);
212                }
213                break;
214              default: break;
215               
216          }
217          free($1);
218          free($3.string);
219          if(rc)return(rc);
220        }
221        | NAME '=' FILENAME
222        { var_token vt;
223          void (*write_cmd)(moddefv module, var_token type,
224                            idtyp t, void *arg1, void *arg2);
225          switch(sectnum) {
226              case 1: /* pass 1: */
227                Add2files(&module_def, $3);
228                break;
229              case 3: /* pass 3: procedure declaration */
230                if( (vt=checkvar($1, VAR_FILE, &write_cmd)) ) {
231                  proc_set_var(&procedure_decl, VAR_FILE, vt, $1, $3);
232                }
233                break;
234              default: break;
235               
236          }
237          free($1);
238          free($3);
239        }
240        | NAME '=' files
241        { var_token vt;
242          void (*write_cmd)(moddefv module, var_token type,
243                            idtyp t, void *arg1, void *arg2);
244          switch(sectnum) {
245              case 1: /* pass 1: */
246                break;
247              case 3: /* pass 3: procedure declaration */
248                if( (vt=checkvar($1, VAR_FILES, &write_cmd)) ) {
249                  proc_set_var(&procedure_decl, VAR_FILES, vt, $1, &$3);
250                }
251                break;
252              default: break;
253               
254          }
255          free($1);
256          //free($3);
257        }
258        | NAME '=' NUMTOK
259        { var_token vt;
260          void (*write_cmd)(moddefv module, var_token type,
261                            idtyp t, void *arg1, void *arg2);
262          switch(sectnum) {
263              case 1: /* pass 1: */
264                break;
265              case 3: /* pass 3: procedure declaration */
266                if( (vt=checkvar($1, VAR_NUM, &write_cmd)) ) {
267                  proc_set_var(&procedure_decl, VAR_NUM, vt, $1, &$3);
268                }
269                break;
270              default: break;
271               
272          }
273          free($1);
274        }
275        | NAME '=' BOOLTOK
276        {
277          var_token vt;
278          int rc = 0;
279          void (*write_cmd)(moddefv module, var_token type,
280                            idtyp t, void *arg1, void *arg2);
281          switch(sectnum) {
282              case 1: /* pass 1: */
283                if( (vt=checkvar($1, VAR_BOOL, &write_cmd)) ) {
284                  proc_set_default_var(VAR_BOOL, vt, $1, &$3);
285                }
286                else {
287                  rc=myyyerror("Line %d: Unknown variable '%s' in section %d\n",
288                            yylineno, $1, sectnum);
289                }
290                break;
291              case 3: /* pass 3: procedure declaration */
292                if( (vt=checkvar($1, VAR_BOOL, &write_cmd)) ) {
293                  proc_set_var(&procedure_decl, VAR_BOOL, vt, $1, &$3);
294                }
295                else {
296                  rc=myyyerror("Line %d: Unknown variable '%s' in section %d\n",
297                            yylineno, $1, sectnum);
298                }
299                break;
300              default: break;
301               
302          }
303          free($1);
304          if(rc)return(rc);
305        }
306;
307
308files:  FILENAME ',' FILENAME
309        {
310          if(debug>2)printf(">>>>>>>>files '%s' , '%s'\n", $1, $3);
311          Add2files(&module_def, $1);
312          Add2files(&module_def, $3);
313          free($1);
314          free($3);
315        }
316;
317
318sect2full: SECT2START sect2 sect2end;
319
320sect2: procdefsg
321       | sect2 procdefsg
322       ;
323
324sect2end: SECT2END
325{
326          if(debug>2)printf("End of section 'Singular' 2 (%d)\n",
327                            sectnum);
328};
329
330
331sect3full: SECT3START sect3 sect3end;
332
333sect3:  procdef
334        | sect3 procdef
335        ;
336
337sect3end: SECT3END
338        {
339          write_finish_functions(&module_def, &procedure_decl);
340          if(debug>2)printf("End of section 'procedures' 3 (%d)\n",
341                            sectnum);
342        }
343        ;
344
345/*
346 */
347procdefsg: procdeclsg proccode
348        {
349          if(debug>2)printf("SG-PROCDEF:\n");
350          write_singular_end(&module_def, &procedure_decl, yylineno);
351          setup_proc(&module_def, &procedure_decl);
352          write_helpfile_help(&module_def, &procedure_decl);
353        }
354        | procdeclsg proccode procdeclexample
355        {
356          if(debug>2)printf("SG-PROCDEF mit example:\n");
357          fflush(module_def.fmtfp);
358          write_singular_end(&module_def, &procedure_decl, yylineno);
359          setup_proc(&module_def, &procedure_decl);
360          write_helpfile_help(&module_def, &procedure_decl);
361        }
362;
363
364procdeclsg: procdeclsg2 '{'
365        {
366        }
367        | procdeclsg2 procdeclhelp '{'
368        {
369        }
370        ;
371
372procdeclsg2: procdecl1 '(' sgtypelist ')'
373         {
374           procedure_decl.lineno_other = yylineno;
375         }
376        | procdecl1
377         {
378           procedure_decl.lineno_other = yylineno;
379         }
380        | procdecl1 '(' ')'
381         {
382           procedure_decl.lineno_other = yylineno;
383         }
384        ;
385
386procdecl1: PROCDECLTOK NAME
387        {
388          init_proc(&procedure_decl, $2, NULL, yylineno, LANG_SINGULAR);
389          free($2);
390          if(write_singular_procedures(&module_def, &procedure_decl))
391            return(myyyerror("Error while creating bin-file\n"));
392        }
393        | STATICTOK PROCDECLTOK NAME
394        {
395          init_proc(&procedure_decl, $3, NULL, yylineno, LANG_SINGULAR);
396          procedure_decl.is_static = TRUE;
397          free($3);
398          if(write_singular_procedures(&module_def, &procedure_decl))
399            return(myyyerror("Error while creating bin-file\n"));
400        };
401
402procdef: procdecl proccode
403        {
404          if(debug>2)printf("PROCDEF:\n");
405          write_helpfile_help(&module_def, &procedure_decl);
406        }
407        | procdecl proccode procdeclexample
408        {
409          if(debug>2)printf("PROCDEF mit example:\n");
410          write_helpfile_help(&module_def, &procedure_decl);
411          fflush(module_def.fmtfp);
412        }
413        ;
414
415procdecl: procdecl2 '{'
416        {
417          setup_proc(&module_def, &procedure_decl);
418        }
419        | procdecl2 procdeclhelp '{'
420        {
421          setup_proc(&module_def, &procedure_decl);
422        }
423        ;
424
425procdecl2: funcdecl1
426        | funcdecl1 '(' ')'
427        | funcdecl1 '(' typelist ')';
428
429funcdecl1: VARTYPETOK NAME
430        {
431          if(debug>2)printf("funcdecl1-2\n");
432          init_proc(&procedure_decl, $2, &$1, yylineno);
433          free($2);
434        }
435        | STATICTOK VARTYPETOK NAME
436        {
437          if(debug>2)printf("funcdecl1-4\n");
438          init_proc(&procedure_decl, $3, &$2, yylineno);
439          free($3);
440          procedure_decl.is_static = TRUE;
441        };
442
443 
444procdeclhelp: MSTRINGTOK
445        {
446          procedure_decl.help_string = $1.string;
447          procedure_decl.lineno_other = $1.lineno;
448          if(debug>2)printf("\t\thelp at %d\n", yylineno);
449          write_help(&module_def, &procedure_decl);
450        }
451        ;
452
453proccode: proccodeline MCODETOK
454          {
455            write_function_errorhandling(&module_def, &procedure_decl);
456          };
457
458
459proccodeline: CODEPART
460        {
461          printf(">>>>(%d) %s<<<<\n", procedure_decl.flags.start_of_code, $1);
462          write_codeline(&module_def, &procedure_decl, $1, yylineno-1);
463        }
464        | proccodeline CODEPART
465        {
466          printf(">>>>(%d) %s<<<<\n", procedure_decl.flags.start_of_code, $2);
467          write_codeline(&module_def, &procedure_decl, $2);
468        }
469        | proccodeline CMTPART
470        {
471          printf(">>>>(%d) %s<<<<\n", procedure_decl.flags.start_of_code, $2);
472          write_codeline(&module_def, &procedure_decl, $2);
473        }
474        | proccodeline proccmd
475        {
476        };
477
478procdeclexample: examplestart '{' examplecodeline MCODETOK
479        {
480          write_example(&module_def, &procedure_decl);
481        }
482        ;
483
484examplestart: EXAMPLETOK
485        {
486          if(procedure_decl.procname == NULL) {
487            return(myyyerror("example without proc-declaration at line %d\n",
488                     yylineno));
489          }
490          if(debug>2)printf("Example at %d\n", yylineno);
491          procedure_decl.lineno_other = yylineno;
492        };
493 
494examplecodeline: CODEPART
495        {
496          int len = strlen($1);
497          procedure_decl.example_len = len;
498          procedure_decl.example_string = (char *)malloc(len+1);
499          memset(procedure_decl.example_string, 0, len+1);
500          memcpy(procedure_decl.example_string, $1, len);
501        }
502        | examplecodeline CODEPART
503        {
504          long len = strlen($2);
505          long newlen = procedure_decl.example_len + len;
506         
507          procedure_decl.example_string =
508            (char *)realloc((void *)procedure_decl.example_string, newlen+1);
509          memset(procedure_decl.example_string+procedure_decl.example_len,
510                 0, len+1);
511          memcpy(procedure_decl.example_string+procedure_decl.example_len,
512                 $2, len);
513          procedure_decl.example_len = newlen;
514        };
515
516proccmd: '%' NAME ';'
517        { cmd_token vt;
518          void (*write_cmd)(moddefv module, procdefv pi, void *arg);
519         
520          switch(vt=checkcmd($2, &write_cmd, CMDT_SINGLE, 0)) {
521              case CMD_NONE:
522                return(myyyerror("Line %d: Unknown command '%s' in section %d\n",
523                          yylineno, $2, sectnum));
524                break;
525              case CMD_BADSYNTAX:
526                return(myyyerror("Line %d: bad syntax of command '%s' in section %d\n",
527                          yylineno, $2, sectnum));
528                break;
529
530              case CMD_DECL:
531              case CMD_CHECK:
532                procedure_decl.flags.auto_header = 0;
533              case CMD_NODECL:
534                write_cmd(&module_def, &procedure_decl,NULL);
535                break;
536
537              default:
538                write_function_header(&module_def, &procedure_decl);
539                write_cmd(&module_def, &procedure_decl,NULL);
540          }
541          free($2);
542        }
543        | '%' NAME '(' ')' ';'
544        {
545          cmd_token vt;
546          void (*write_cmd)(moddefv module, procdefv pi, void *arg);
547         
548          switch(vt=checkcmd($2, &write_cmd, CMDT_0, 1)) {
549              case CMD_NONE:
550                return(myyyerror("Line %d: Unknown command '%s' in section %d\n",
551                          yylineno, $2, sectnum));
552                break;
553              case CMD_BADSYNTAX:
554                return(myyyerror("Line %d: bad syntax of command '%s' in section %d\n",
555                          yylineno, $2, sectnum));
556                break;
557              default:
558                write_function_header(&module_def, &procedure_decl);
559              case CMD_DECL:
560              case CMD_CHECK:
561                write_cmd(&module_def, &procedure_decl,NULL);
562          }
563          free($2);
564        }
565        | '%' NAME '(' NAME ')' ';'
566        {
567          cmd_token vt;
568          void (*write_cmd)(moddefv module, procdefv pi, void *arg);
569         
570          switch(vt=checkcmd($2, &write_cmd, CMDT_ANY, 1)) {
571              case CMD_NONE:
572                return(myyyerror("Line %d: Unknown command '%s' in section %d\n",
573                          yylineno, $2, sectnum));
574                break;
575              case CMD_BADSYNTAX:
576                return(myyyerror("Line %d: bad syntax of command '%s' in section %d\n",
577                          yylineno, $2, sectnum));
578                break;
579              default:
580                write_function_header(&module_def, &procedure_decl);
581              case CMD_DECL:
582              case CMD_CHECK:
583                write_cmd(&module_def, &procedure_decl, $4);
584          }
585          free($2); free($4);
586        }
587        | '%' NAME '(' identifier '(' arglist ')' ')' ';'
588        {
589          cmd_token vt;
590          void (*write_cmd)(moddefv module, procdefv pi, void *arg);
591         
592          switch(vt=checkcmd($2, &write_cmd, CMDT_ANY, 1)) {
593              case CMD_NONE:
594                return(myyyerror("Line %d: Unknown command '%s' in section %d\n",
595                          yylineno, $2, sectnum));
596                break;
597              case CMD_BADSYNTAX:
598                return(myyyerror("Line %d: bad syntax of command '%s' in section %d\n",
599                          yylineno, $2, sectnum));
600                break;
601              default:
602                write_function_header(&module_def, &procedure_decl);
603              case CMD_DECL:
604              case CMD_CHECK:
605                write_cmd(&module_def, &procedure_decl, $4);
606          }
607          free($2);
608        }
609        | '%' NAME MEQUAL ANYTOK
610        {
611          cmd_token vt;
612          void (*write_cmd)(moddefv module, procdefv pi, void *arg);
613         
614          switch(vt=checkcmd($2, &write_cmd, CMDT_EQ, 0)) {
615              case CMD_NONE:
616                return(myyyerror("Line %d: Unknown command '%s' in section %d\n",
617                          yylineno, $2, sectnum));
618                break;
619              case CMD_BADSYNTAX:
620                return(myyyerror("Line %d: bad syntax of command '%s' in section %d\n",
621                          yylineno, $2, sectnum));
622                break;
623              default:
624                write_function_header(&module_def, &procedure_decl);
625              case CMD_DECL:
626              case CMD_CHECK:
627                write_cmd(&module_def, &procedure_decl, $4);
628          }
629          free($2);
630        };
631
632
633identifier: NAME
634        {
635          if(debug>2)printf("### ID ### Name %s\n", $1);
636          $$ = $1;
637        }
638        | identifier MCOLONCOLON NAME
639        {
640          int len = strlen($$) + strlen($3) + 2;
641          if(debug>2)printf("### ID ### Name %s\n", $3);
642          $$ = (char *)realloc($$, len);
643          strcat($$, "::");
644          strcat($$, $3);
645        };
646
647arglist: NAME
648        {
649          if(debug>2)printf("### ARGS %s\n", $1);
650        }
651        | arglist ',' NAME
652        {
653          if(debug>2)printf("### ARGS %s\n", $3);
654        };
655       
656sgtypelist: VARTYPETOK NAME
657        {
658          if(debug>2)printf("\tsgtypelist %s %s\n", $1.name, $2);
659          write_singular_parameter(&module_def, yylineno, $1.name, $2);
660          free($1.name);
661          free($2);
662        }
663        | VARTYPETOK '#'
664        {
665          if(debug>2)printf("\tsgtypelist %s %s\n", $1.name, $2);
666          write_singular_parameter(&module_def, yylineno, $1.name, "#");
667          free($1.name);
668        }
669        | sgtypelist ',' VARTYPETOK NAME
670        {
671          if(debug>2)printf("\tsgtypelist next  %s %s\n", $3.name, $4);
672          write_singular_parameter(&module_def, yylineno, $3.name, $4);
673          free($3.name);
674          free($4);
675        }
676        | sgtypelist ',' VARTYPETOK '#'
677        {
678          if(debug>2)printf("\tsgtypelist next  %s %s\n", $3.name, $4);
679          write_singular_parameter(&module_def, yylineno, $3.name, "#");
680          free($3.name);
681        }
682        ;
683
684typelist: VARTYPETOK
685        {
686          AddParam(&procedure_decl, &$1);
687          free($1.name);
688        }
689        | VARTYPETOK NAME
690        {
691          if(check_reseverd($2))
692            return(myyyerror("Line %d: variablename '%s' is reserved\n",
693                            yylineno, $2));
694          AddParam(&procedure_decl, &$1, $2);
695          free($1.name); free($2);
696        }
697        | typelist ',' VARTYPETOK
698        {
699          AddParam(&procedure_decl, &$3);
700          free($3.name);
701        }
702        | typelist ',' VARTYPETOK NAME
703        {
704          if(check_reseverd($4))
705            return(myyyerror("Line %d: variablename '%s' is reserved\n",
706                            yylineno, $4));
707          AddParam(&procedure_decl, &$3, $4);
708          free($3.name); free($4);
709        }
710        ;
711
712sect4:  SECT4START codeline SECT4END
713        {
714        };
715
716codeline: CODEPART
717        {
718          fprintf(module_def.modfp, "#line %d \"%s\"\n",
719                                          yylineno, module_def.filename);
720          fprintf(module_def.modfp, "%s", $1);
721        }
722        | codeline CODEPART
723        {
724          fprintf(module_def.modfp, "%s", $2);
725        }
726        ;
727
728
729%%
730
Note: See TracBrowser for help on using the repository browser.