diff --git a/macro/MacroDriver.hh b/macro/MacroDriver.hh
index cb8a29e25ea59fe62ad37fe0015e9bafc1abd95b..1b95a9f364acc40a91bdf9451d1158cc0a3d93c3 100644
--- a/macro/MacroDriver.hh
+++ b/macro/MacroDriver.hh
@@ -81,6 +81,8 @@ private:
   string for_body_tmp;
   //! Temporary variable used in FOR_BODY mode
   Macro::parser::location_type for_body_loc_tmp;
+  //! Temporary variable used in FOR_BODY mode. Keeps track of the location of the @#for statement, for reporting messages
+  Macro::parser::location_type for_stmt_loc_tmp;
   //! Temporary variable used in FOR_BODY mode. Keeps track of number of nested @#for/@#endfor
   int nested_for_nb;
   //! Set to true while parsing a FOR statement (only the statement, not the loop body)
@@ -92,6 +94,8 @@ private:
   string then_body_tmp;
   //! Temporary variable used in THEN_BODY mode
   Macro::parser::location_type then_body_loc_tmp;
+  //! Temporary variable used in THEN_BODY mode. Keeps track of the location of the @#if statement, for reporting messages
+  Macro::parser::location_type if_stmt_loc_tmp;
   //! Temporary variable used in ELSE_BODY mode
   string else_body_tmp;
   //! Temporary variable used in ELSE_BODY mode
diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll
index 557b97330e6eaf54d69a9fedbc98f5dc927035ea..291d30df293247eb02dc16787a17b14be05a8351 100644
--- a/macro/MacroFlex.ll
+++ b/macro/MacroFlex.ll
@@ -92,6 +92,13 @@ CONT \\\\
 
 <STMT>{CONT}{SPC}*{EOL}     { yylloc->lines(1); yylloc->step(); }
 <STMT>{EOL}                 {
+                              /* If parsing a @#for or an @#if, keep the location
+                                 for reporting message in case of error */
+                              if (reading_for_statement)
+                                for_stmt_loc_tmp = *yylloc;
+                              else if (reading_if_statement)
+                                if_stmt_loc_tmp = *yylloc;
+
                               yylloc->lines(1);
                               yylloc->step();
                               if (reading_for_statement)
@@ -180,7 +187,7 @@ CONT \\\\
                               yylloc->step();
                             }
 <FOR_BODY>.                 { for_body_tmp.append(yytext); yylloc->step(); }
-<FOR_BODY><<EOF>>           { driver.error(*yylloc, "Unexpected end of file: @#for loop not matched by an @#endfor"); }
+<FOR_BODY><<EOF>>           { driver.error(for_stmt_loc_tmp, "@#for loop not matched by an @#endfor (unexpected end of file)"); }
 <FOR_BODY>^{SPC}*@#{SPC}*endfor{SPC}*{EOL} {
                               yylloc->lines(1);
                               yylloc->step();
@@ -212,7 +219,7 @@ CONT \\\\
                               yylloc->step();
                             }
 <THEN_BODY>.                { then_body_tmp.append(yytext); yylloc->step(); }
-<THEN_BODY><<EOF>>          { driver.error(*yylloc, "Unexpected end of file: @#if not matched by an @#endif"); }
+<THEN_BODY><<EOF>>          { driver.error(if_stmt_loc_tmp, "@#if not matched by an @#endif (unexpected end of file)"); }
 <THEN_BODY>^{SPC}*@#{SPC}*else{SPC}*{EOL} {
                               yylloc->lines(1);
                               yylloc->step();
@@ -254,7 +261,7 @@ CONT \\\\
                               yylloc->step();
                             }
 <ELSE_BODY>.                { else_body_tmp.append(yytext); yylloc->step(); }
-<ELSE_BODY><<EOF>>          { driver.error(*yylloc, "Unexpected end of file: @#if not matched by an @#endif"); }
+<ELSE_BODY><<EOF>>          { driver.error(if_stmt_loc_tmp, "@#if not matched by an @#endif (unexpected end of file)"); }
 
 <ELSE_BODY>^{SPC}*@#{SPC}*endif{SPC}*{EOL} {
                               yylloc->lines(1);