stage1_2/iec.y
changeset 177 697562a5da7c
parent 175 dadda1b5401e
child 183 fb240c8d1f66
equal deleted inserted replaced
176:bef3cc16c064 177:697562a5da7c
   131  *       would get a new variable defined each time iec.y.hh is included!).
   131  *       would get a new variable defined each time iec.y.hh is included!).
   132  *       Even though the variables are declared 'extern' over here, they will in
   132  *       Even though the variables are declared 'extern' over here, they will in
   133  *       fact be defined towards the end of this same file (i.e. in the prologue)
   133  *       fact be defined towards the end of this same file (i.e. in the prologue)
   134  */
   134  */
   135 
   135 
       
   136 
       
   137 /* NOTE: These variable are really parameters we would like the stage2__ function to pass
       
   138  *       to the yyparse() function. However, the yyparse() function is created automatically
       
   139  *       by bison, so we cannot add parameters to this function. The only other
       
   140  *       option is to use global variables! yuck!
       
   141  */
       
   142 
   136 /* A global flag used to tell the parser if overloaded funtions should be allowed.
   143 /* A global flag used to tell the parser if overloaded funtions should be allowed.
   137  * The IEC 61131-3 standard allows overloaded funtions in the standard library,
   144  * The IEC 61131-3 standard allows overloaded funtions in the standard library,
   138  * but disallows them in user code...
   145  * but disallows them in user code...
   139  */
   146  */
   140 extern bool allow_function_overloading;
   147 extern bool allow_function_overloading;
       
   148 
       
   149 /* A global flag used to tell the parser whether to include the full variable location
       
   150  * when printing out error messages...
       
   151  */
       
   152 extern bool full_token_loc;
   141 
   153 
   142 /* A pointer to the root of the parsing tree that will be generated 
   154 /* A pointer to the root of the parsing tree that will be generated 
   143  * by bison.
   155  * by bison.
   144  */
   156  */
   145 extern symbol_c *tree_root;
   157 extern symbol_c *tree_root;
  1245 %token UNTIL
  1257 %token UNTIL
  1246 %token END_REPEAT
  1258 %token END_REPEAT
  1247 
  1259 
  1248 %token EXIT
  1260 %token EXIT
  1249 
  1261 
       
  1262 
       
  1263 /******************************************************/
       
  1264 /* Symbols defined in                                 */
       
  1265 /* "Safety Software Technical Specification,          */
       
  1266 /*  Part 1: Concepts and Function Blocks,             */
       
  1267 /*  Version 1.0 – Official Release"                   */
       
  1268 /* by PLCopen - Technical Committee 5 - 2006-01-31    */
       
  1269 /******************************************************/
       
  1270 %token SAFEBOOL
       
  1271 
  1250 %%
  1272 %%
  1251 
  1273 
  1252 
  1274 
  1253 
  1275 
  1254 
  1276 
  2216 | BOOL		{$$ = new bool_type_name_c(locloc(@$));}
  2238 | BOOL		{$$ = new bool_type_name_c(locloc(@$));}
  2217 /* NOTE: see note under the B 1.2.1 section of token
  2239 /* NOTE: see note under the B 1.2.1 section of token
  2218  * and grouping type definition for reason why BOOL
  2240  * and grouping type definition for reason why BOOL
  2219  * was added to this definition.
  2241  * was added to this definition.
  2220  */
  2242  */
       
  2243     /******************************************************/
       
  2244     /* Symbols defined in                                 */
       
  2245     /* "Safety Software Technical Specification,          */
       
  2246     /*  Part 1: Concepts and Function Blocks,             */
       
  2247     /*  Version 1.0 – Official Release"                   */
       
  2248     /* by PLCopen - Technical Committee 5 - 2006-01-31    */
       
  2249     /******************************************************/
       
  2250 | SAFEBOOL	{$$ = new safebool_type_name_c(locloc(@$));}
  2221 ;
  2251 ;
  2222 
  2252 
  2223 numeric_type_name:
  2253 numeric_type_name:
  2224   integer_type_name
  2254   integer_type_name
  2225 | real_type_name
  2255 | real_type_name
  7407 extern FILE *yyin;
  7437 extern FILE *yyin;
  7408 extern int yylineno;
  7438 extern int yylineno;
  7409 extern tracking_t* current_tracking;
  7439 extern tracking_t* current_tracking;
  7410 
  7440 
  7411 
  7441 
       
  7442 
       
  7443 
       
  7444 /*************************************************************************************************/
       
  7445 /* NOTE: These variables are really parameters we would like the stage2__ function to pass       */
       
  7446 /*       to the yyparse() function. However, the yyparse() function is created automatically     */
       
  7447 /*       by bison, so we cannot add parameters to this function. The only other                  */
       
  7448 /*       option is to use global variables! yuck!                                                */ 
       
  7449 /*************************************************************************************************/
       
  7450 
  7412 /* A global flag used to tell the parser if overloaded funtions should be allowed.
  7451 /* A global flag used to tell the parser if overloaded funtions should be allowed.
  7413  * The IEC 61131-3 standard allows overloaded funtions in the standard library,
  7452  * The IEC 61131-3 standard allows overloaded funtions in the standard library,
  7414  * but disallows them in user code...
  7453  * but disallows them in user code...
       
  7454  *
       
  7455  * In essence, a parameter we would like to pass to the yyparse() function but
       
  7456  * have to do it using a global variable, as the yyparse() prototype is fixed by bison.
  7415  */
  7457  */
  7416 bool allow_function_overloading = false;
  7458 bool allow_function_overloading = false;
       
  7459 
       
  7460 /* A global flag used to tell the parser whether to include the full variable location
       
  7461  * when printing out error messages...
       
  7462  */
       
  7463 bool full_token_loc;
  7417 
  7464 
  7418 /* A pointer to the root of the parsing tree that will be generated 
  7465 /* A pointer to the root of the parsing tree that will be generated 
  7419  * by bison.
  7466  * by bison.
  7420  */
  7467  */
  7421 symbol_c *tree_root;
  7468 symbol_c *tree_root;
  7435   current_error_msg = error_msg;
  7482   current_error_msg = error_msg;
  7436 /* fprintf(stderr, "error %d: %s\n", yynerrs // global variable //, error_msg); */
  7483 /* fprintf(stderr, "error %d: %s\n", yynerrs // global variable //, error_msg); */
  7437 /*  print_include_stack(); */
  7484 /*  print_include_stack(); */
  7438 }
  7485 }
  7439 
  7486 
  7440 bool full_error;
       
  7441 
  7487 
  7442 bool is_current_syntax_token() {
  7488 bool is_current_syntax_token() {
  7443 	switch (yychar) {
  7489 	switch (yychar) {
  7444   	case ';':
  7490   	case ';':
  7445   	case ',':
  7491   	case ',':
  7472 void print_err_msg(int first_line,
  7518 void print_err_msg(int first_line,
  7473                    int first_column,
  7519                    int first_column,
  7474                    int last_line,
  7520                    int last_line,
  7475                    int last_column,
  7521                    int last_column,
  7476                    const char *additional_error_msg) {
  7522                    const char *additional_error_msg) {
  7477   if (full_error)
  7523   if (full_token_loc)
  7478   	fprintf(stderr, "%s:%d-%d..%d-%d: error : %s\n", current_filename, first_line, first_column, last_line, last_column, additional_error_msg);
  7524   	fprintf(stderr, "%s:%d-%d..%d-%d: error : %s\n", current_filename, first_line, first_column, last_line, last_column, additional_error_msg);
  7479   else
  7525   else
  7480   	fprintf(stderr, "%s:%d: error : %s\n", current_filename, first_line, additional_error_msg);
  7526   	fprintf(stderr, "%s:%d: error : %s\n", current_filename, first_line, additional_error_msg);
  7481   //fprintf(stderr, "error %d: %s\n", yynerrs /* a global variable */, additional_error_msg);
  7527   //fprintf(stderr, "error %d: %s\n", yynerrs /* a global variable */, additional_error_msg);
  7482   print_include_stack();
  7528   print_include_stack();
  7601 #define LIBFILE "ieclib.txt"
  7647 #define LIBFILE "ieclib.txt"
  7602 #define DEF_LIBFILENAME LIBDIRECTORY "/" LIBFILE
  7648 #define DEF_LIBFILENAME LIBDIRECTORY "/" LIBFILE
  7603 
  7649 
  7604 extern const char *INCLUDE_DIRECTORIES[];
  7650 extern const char *INCLUDE_DIRECTORIES[];
  7605 
  7651 
  7606 int stage1_2__(const char *filename, const char *includedir, symbol_c **tree_root_ref, bool full) {
  7652 
       
  7653 
       
  7654 int stage2__(const char *filename, 
       
  7655              const char *includedir,     /* Include directory, where included files will be searched for... */
       
  7656              symbol_c **tree_root_ref,
       
  7657              bool full_token_loc_        /* error messages specify full token location */
       
  7658             ) {
       
  7659 
  7607   FILE *in_file = NULL, *lib_file = NULL;
  7660   FILE *in_file = NULL, *lib_file = NULL;
  7608   char *libfilename = NULL;
  7661   char *libfilename = NULL;
  7609 	
  7662 	
  7610 	full_error = full;
  7663 
  7611 	
       
  7612   if((in_file = fopen(filename, "r")) == NULL) {
  7664   if((in_file = fopen(filename, "r")) == NULL) {
  7613     char *errmsg = strdup2("Error opening main file ", filename);
  7665     char *errmsg = strdup2("Error opening main file ", filename);
  7614     perror(errmsg);
  7666     perror(errmsg);
  7615     free(errmsg);
  7667     free(errmsg);
  7616     return -1;
  7668     return -1;
  7638   }
  7690   }
  7639 
  7691 
  7640   /* first parse the standard library file... */
  7692   /* first parse the standard library file... */
  7641   yyin = lib_file;
  7693   yyin = lib_file;
  7642   allow_function_overloading = true;
  7694   allow_function_overloading = true;
       
  7695   full_token_loc = full_token_loc_;
  7643   current_filename = libfilename;
  7696   current_filename = libfilename;
  7644   current_tracking = GetNewTracking(yyin);
  7697   current_tracking = GetNewTracking(yyin);
  7645   if (yyparse() != 0)
  7698   if (yyparse() != 0)
  7646       ERROR;
  7699       ERROR;
  7647 
  7700 
  7670 #endif
  7723 #endif
  7671 
  7724 
  7672   /* now parse the input file... */
  7725   /* now parse the input file... */
  7673   yyin = in_file;
  7726   yyin = in_file;
  7674   allow_function_overloading = false;
  7727   allow_function_overloading = false;
       
  7728   full_token_loc = full_token_loc_;
  7675   current_filename = filename;
  7729   current_filename = filename;
  7676   current_tracking = GetNewTracking(yyin);
  7730   current_tracking = GetNewTracking(yyin);
  7677   {int res;
  7731   {int res;
  7678     if ((res = yyparse()) != 0) {
  7732     if ((res = yyparse()) != 0) {
  7679       fprintf (stderr, "\nParsing failed because of too many consecutive syntax errors. Bailing out!\n");
  7733       fprintf (stderr, "\nParsing failed because of too many consecutive syntax errors. Bailing out!\n");