diff --git a/DynareBison.yy b/DynareBison.yy
index 9ba11294c7c7ad7ba9ac5c1548d1107c14b50eb9..be5e33a1a5f6681367351919730a21379aa6afb0 100644
--- a/DynareBison.yy
+++ b/DynareBison.yy
@@ -55,8 +55,8 @@ class ParsingDriver;
 %locations
 %initial-action
 {
-  // Initialize the location filenames to NULL
-  @$.begin.filename = @$.end.filename = NULL;
+  // Initialize the locations' filenames to the filename maintained by the lexer
+  @$.begin.filename = @$.end.filename = &(driver.lexer->filename);
 }
 
 %debug
diff --git a/DynareFlex.ll b/DynareFlex.ll
index ec2b276930dda2dddfc256c69b788e244e3bd664..8924d796614710fb5c62fcba5d70e5728e91b31e 100644
--- a/DynareFlex.ll
+++ b/DynareFlex.ll
@@ -73,9 +73,7 @@ int sigma_e = 0;
  /* Rules for matching $line directives */
 <*>^@#line\ \"  { line_caller = YYSTATE; BEGIN(LINE1); }
 <LINE1>[^\"]*   {
-                  if (yylloc->begin.filename)
-                    delete yylloc->begin.filename;
-                  yylloc->begin.filename = yylloc->end.filename = new string(yytext);
+                  filename = string(yytext);
                   BEGIN(LINE2);
                 }
 <LINE2>\"       BEGIN(LINE3);
diff --git a/ParsingDriver.cc b/ParsingDriver.cc
index d9381a77631386d5fd69f0d4cdedbc6ea89f2649..06b87379a48a7e2b24759d3fdfeb579bb26db21d 100644
--- a/ParsingDriver.cc
+++ b/ParsingDriver.cc
@@ -72,12 +72,6 @@ ParsingDriver::parse(istream &in, bool debug)
 
   delete lexer;
 
-  /* Deleting filename in DynareFlex::yyterminate() is prematurate,
-     because if there is an unexpected end of file, the call to
-     ParsingDriver::error() needs filename */
-  /*if (location.begin.filename)
-    delete location.begin.filename;*/
-
   return mod_file;
 }
 
diff --git a/include/ParsingDriver.hh b/include/ParsingDriver.hh
index 97f790c45009df031dfcefc6675a878834bd4502..a752a867114a9ef9554659e3ba61342551b9f4de 100644
--- a/include/ParsingDriver.hh
+++ b/include/ParsingDriver.hh
@@ -60,6 +60,10 @@ public:
   Dynare::parser::token_type lex(Dynare::parser::semantic_type *yylval,
                                  Dynare::parser::location_type *yylloc,
                                  ParsingDriver &driver);
+
+  //! The filename being parsed
+  /*! The bison parser locations (begin and end) contain a pointer to that string */
+  string filename;
 };
 
 //! Drives the scanning and parsing of the .mod file, and constructs its abstract representation