stage1_2/iec.y
changeset 286 a4f4990d5c66
parent 279 c0453b7f99df
child 287 9df7fcb9bde5
equal deleted inserted replaced
284:64a4504633d4 286:a4f4990d5c66
   114 
   114 
   115 
   115 
   116 /* Macros used to pass the line and column locations when
   116 /* Macros used to pass the line and column locations when
   117  * creating a new object for the abstract syntax tree.
   117  * creating a new object for the abstract syntax tree.
   118  */
   118  */
   119 #define locloc(foo) foo.first_line, foo.first_column, foo.last_line, foo.last_column
   119 #define locloc(foo) foo.first_line, foo.first_column, foo.first_file, foo.last_line, foo.last_column, foo.last_file
   120 #define   locf(foo) foo.first_line, foo.first_column
   120 #define   locf(foo) foo.first_line, foo.first_column, foo.first_file
   121 #define   locl(foo) foo.last_line,  foo.last_column
   121 #define   locl(foo) foo.last_line,  foo.last_column, foo.last_file
   122 
   122 
       
   123 /* Redefine the default action to take for each rule, so that the filenames are correctly processed... */
       
   124 # define YYLLOC_DEFAULT(Current, Rhs, N)                                \
       
   125          do                                                                  \
       
   126            if (N)                                                            \
       
   127              {                                                               \
       
   128                (Current).first_line   = YYRHSLOC(Rhs, 1).first_line;         \
       
   129                (Current).first_column = YYRHSLOC(Rhs, 1).first_column;       \
       
   130                (Current).first_file   = YYRHSLOC(Rhs, 1).first_file;         \
       
   131                (Current).last_line    = YYRHSLOC(Rhs, N).last_line;          \
       
   132                (Current).last_column  = YYRHSLOC(Rhs, N).last_column;        \
       
   133                (Current).last_file    = YYRHSLOC(Rhs, 1).last_file;          \
       
   134              }                                                               \
       
   135            else                                                              \
       
   136              {                                                               \
       
   137                (Current).first_line   = (Current).last_line   =              \
       
   138                  YYRHSLOC(Rhs, 0).last_line;                                 \
       
   139                (Current).first_column = (Current).last_column =              \
       
   140                  YYRHSLOC(Rhs, 0).last_column;                               \
       
   141                (Current).first_file   = (Current).last_file   =              \
       
   142                  YYRHSLOC(Rhs, 0).last_file;                                 \
       
   143              }                                                               \
       
   144          while (0)
   123 
   145 
   124 
   146 
   125 /* A macro for printing out internal parser errors... */
   147 /* A macro for printing out internal parser errors... */
   126 #define ERROR error_exit(__FILE__,__LINE__)
   148 #define ERROR error_exit(__FILE__,__LINE__)
   127 /* function defined in main.cc */
   149 /* function defined in main.cc */
   180 /* ERROR_CHECK_END */
   202 /* ERROR_CHECK_END */
   181 
   203 
   182 /* print an error message */
   204 /* print an error message */
   183 void print_err_msg(int first_line,
   205 void print_err_msg(int first_line,
   184                    int first_column,
   206                    int first_column,
       
   207                    const char *first_filename,
   185                    int last_line,
   208                    int last_line,
   186                    int last_column,
   209                    int last_column,
       
   210                    const char *last_filename,
   187                    const char *additional_error_msg);
   211                    const char *additional_error_msg);
   188 %}
   212 %}
   189 
   213 
   190 
   214 
   191 
   215 
   192 
   216 
   193 // %glr-parser
   217 // %glr-parser
   194 // %expect-rr 1
   218 // %expect-rr 1
   195 
   219 
       
   220 
       
   221 /* The following definitions need to be inside a '%code requires' 
       
   222  * so that they are also included in the header files. If this were not the case,
       
   223  * YYLTYPE would be delcared as something in the iec.cc file, and another thing
       
   224  * (actually the default value of YYLTYPE) in the iec.y.hh heder file.
       
   225  */
       
   226 %code requires {
       
   227 /* define a new data type to store the locations, so we can also store
       
   228  * the filename in which the token is expressed.
       
   229  */
       
   230 /* NOTE: since this code will be placed in the iec.y.hh header file,
       
   231  * as well as the iec.cc file that also includes the iec.y.hh header file,
       
   232  * declaring the typedef struct yyltype__local here would result in a 
       
   233  * compilation error when compiling iec.cc, as this struct would be
       
   234  * declared twice.
       
   235  * We therefore use the #if !defined YYLTYPE ...
       
   236  * to make sure only the first declaration is parsed by the C++ compiler.
       
   237  *
       
   238  * At first glance it seems that what we really should do is delcare the
       
   239  * YYLTYPE directly as an anonymous struct, thus:
       
   240  * #define YYLTYPE struct{ ...}
       
   241  * however, this also results in compilation errors.
       
   242  *
       
   243  * I (Mario) think this is kind of a hack. If you know how to
       
   244  * do this re-declaration of YYLTYPE properly, please let me know!
       
   245  */
       
   246 #if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED
       
   247   typedef struct {
       
   248     int first_line;
       
   249     int first_column;
       
   250     const char *first_file;
       
   251     int last_line;
       
   252     int last_column;
       
   253     const char *last_file;
       
   254   } yyltype__local;
       
   255   #define YYLTYPE yyltype__local
       
   256 #endif
       
   257 }
   196 
   258 
   197 
   259 
   198 
   260 
   199 %union {
   261 %union {
   200     symbol_c 	*leaf;
   262     symbol_c 	*leaf;
  7827 /* ERROR_CHECK_END */
  7889 /* ERROR_CHECK_END */
  7828 
  7890 
  7829 
  7891 
  7830 void print_err_msg(int first_line,
  7892 void print_err_msg(int first_line,
  7831                    int first_column,
  7893                    int first_column,
       
  7894                    const char *first_filename,
  7832                    int last_line,
  7895                    int last_line,
  7833                    int last_column,
  7896                    int last_column,
       
  7897                    const char *last_filename,
  7834                    const char *additional_error_msg) {
  7898                    const char *additional_error_msg) {
  7835   if (full_token_loc)
  7899 
  7836   	fprintf(stderr, "%s:%d-%d..%d-%d: error : %s\n", current_filename, first_line, first_column, last_line, last_column, additional_error_msg);
  7900   const char *unknown_file = "<unknown_file>";
  7837   else
  7901   if (first_filename == NULL) first_filename = unknown_file;
  7838   	fprintf(stderr, "%s:%d: error : %s\n", current_filename, first_line, additional_error_msg);
  7902   if ( last_filename == NULL)  last_filename = unknown_file;
       
  7903 
       
  7904   if (full_token_loc) {
       
  7905     if (first_filename == last_filename)
       
  7906       fprintf(stderr, "%s:%d-%d..%d-%d: error : %s\n", first_filename, first_line, first_column, last_line, last_column, additional_error_msg);
       
  7907     else
       
  7908       fprintf(stderr, "%s:%d-%d..%s:%d-%d: error : %s\n", first_filename, first_line, first_column, last_filename, last_line, last_column, additional_error_msg);
       
  7909   } else {
       
  7910       fprintf(stderr, "%s:%d: error : %s\n", first_filename, first_line, additional_error_msg);
       
  7911   }
  7839   //fprintf(stderr, "error %d: %s\n", yynerrs /* a global variable */, additional_error_msg);
  7912   //fprintf(stderr, "error %d: %s\n", yynerrs /* a global variable */, additional_error_msg);
  7840   print_include_stack();
  7913   print_include_stack();
  7841   //fprintf(stderr, "%s(%d-%d): %s\n", current_filename, first_line, last_line, current_error_msg);
  7914   //fprintf(stderr, "%s(%d-%d): %s\n", current_filename, first_line, last_line, current_error_msg);
  7842 }
  7915 }
  7843 
  7916 
  7924     ERROR;
  7997     ERROR;
  7925 
  7998 
  7926   res = new identifier_c(strdup(name), 
  7999   res = new identifier_c(strdup(name), 
  7927                          il_operator->first_line,
  8000                          il_operator->first_line,
  7928                          il_operator->first_column,
  8001                          il_operator->first_column,
       
  8002                          il_operator->first_file,
  7929                          il_operator->last_line,
  8003                          il_operator->last_line,
  7930                          il_operator->last_column
  8004                          il_operator->last_column,
       
  8005                          il_operator->last_file
  7931                         );
  8006                         );
  7932   free(il_operator);
  8007   free(il_operator);
  7933   return res;
  8008   return res;
  7934 }
  8009 }
  7935 
  8010