diff --git a/preprocessor/macro/MacroDriver.hh b/preprocessor/macro/MacroDriver.hh
index 1b95a9f364acc40a91bdf9451d1158cc0a3d93c3..87d26c3b65742dcbc3044ee1721d59abafc252d6 100644
--- a/preprocessor/macro/MacroDriver.hh
+++ b/preprocessor/macro/MacroDriver.hh
@@ -56,13 +56,15 @@ private:
     istream *input;
     struct yy_buffer_state *buffer;
     const Macro::parser::location_type yylloc;
+    const bool is_for_context;
     const string for_body;
     const Macro::parser::location_type for_body_loc;
     ScanContext(istream *input_arg, struct yy_buffer_state *buffer_arg,
-                Macro::parser::location_type &yylloc_arg, const string &for_body_arg,
+                Macro::parser::location_type &yylloc_arg, bool is_for_context_arg,
+                const string &for_body_arg,
                 Macro::parser::location_type &for_body_loc_arg) :
-      input(input_arg), buffer(buffer_arg), yylloc(yylloc_arg), for_body(for_body_arg),
-      for_body_loc(for_body_loc_arg) { }
+      input(input_arg), buffer(buffer_arg), yylloc(yylloc_arg), is_for_context(is_for_context_arg),
+      for_body(for_body_arg), for_body_loc(for_body_loc_arg) { }
   };
 
   //! The stack used to keep track of nested scanning contexts
@@ -72,7 +74,9 @@ private:
   /*! Kept for deletion at end of current scanning buffer */
   istream *input;
 
-  //! If current context is the body of a loop, contains the string of the loop body. Empty otherwise.
+  //! True iff current context is the body of a loop
+  bool is_for_context;
+  //! If current context is the body of a loop, contains the string of the loop body
   string for_body;
   //! If current context is the body of a loop, contains the location of the beginning of the body
   Macro::parser::location_type for_body_loc;
diff --git a/preprocessor/macro/MacroFlex.ll b/preprocessor/macro/MacroFlex.ll
index 85fd999b3859a63b5e69052ed51cdcaeb2dd13e1..59b94fdfe06e8332364e2bc89462dced19422f47 100644
--- a/preprocessor/macro/MacroFlex.ll
+++ b/preprocessor/macro/MacroFlex.ll
@@ -203,6 +203,7 @@ CONT \\\\
                                   // Save old buffer state and location
                                   save_context(yylloc);
 
+                                  is_for_context = true;
                                   for_body = for_body_tmp;
                                   for_body_loc = for_body_loc_tmp;
 
@@ -298,7 +299,7 @@ CONT \\\\
 
                               /* If we are not in a loop body, or if the loop has terminated,
                                  pop a context */
-                              if (for_body.empty() || !iter_loop(driver, yylloc))
+                              if (!is_for_context || !iter_loop(driver, yylloc))
                                 restore_context(yylloc);
                             }
 
@@ -326,7 +327,8 @@ MacroFlex::output_line(Macro::parser::location_type *yylloc) const
 void
 MacroFlex::save_context(Macro::parser::location_type *yylloc)
 {
-  context_stack.push(ScanContext(input, YY_CURRENT_BUFFER, *yylloc, for_body, for_body_loc));
+  context_stack.push(ScanContext(input, YY_CURRENT_BUFFER, *yylloc, is_for_context,
+                                 for_body, for_body_loc));
 }
 
 void
@@ -335,6 +337,7 @@ MacroFlex::restore_context(Macro::parser::location_type *yylloc)
   input = context_stack.top().input;
   yy_switch_to_buffer(context_stack.top().buffer);
   *yylloc = context_stack.top().yylloc;
+  is_for_context = context_stack.top().is_for_context;
   for_body = context_stack.top().for_body;
   for_body_loc = context_stack.top().for_body_loc;
   // Remove top of stack
@@ -357,6 +360,7 @@ MacroFlex::create_include_context(string *filename, Macro::parser::location_type
   yylloc->begin.line = yylloc->end.line = 1;
   yylloc->begin.column = yylloc->end.column = 0;
   // We are not in a loop body
+  is_for_context = false;
   for_body.clear();
   // Output @#line information
   output_line(yylloc);
@@ -371,6 +375,7 @@ MacroFlex::create_then_context(Macro::parser::location_type *yylloc)
   input = new stringstream(then_body_tmp);
   *yylloc = then_body_loc_tmp;
   yylloc->begin.filename = yylloc->end.filename = new string(*then_body_loc_tmp.begin.filename);
+  is_for_context = false;
   for_body.clear();
   output_line(yylloc);
   yy_switch_to_buffer(yy_create_buffer(input, YY_BUF_SIZE));
@@ -383,6 +388,7 @@ MacroFlex::create_else_context(Macro::parser::location_type *yylloc)
   input = new stringstream(else_body_tmp);
   *yylloc = else_body_loc_tmp;
   yylloc->begin.filename = yylloc->end.filename = new string(*else_body_loc_tmp.begin.filename);
+  is_for_context = false;
   for_body.clear();
   output_line(yylloc);
   yy_switch_to_buffer(yy_create_buffer(input, YY_BUF_SIZE));