diff --git a/Makefile.am b/Makefile.am
new file mode 100644
index 0000000000000000000000000000000000000000..38dcedb4e837475dbde892a01c3e9cb449eb84e7
--- /dev/null
+++ b/Makefile.am
@@ -0,0 +1,3 @@
+SUBDIRS = preprocessor doc
+
+ACLOCAL_AMFLAGS = -I m4
diff --git a/configure.ac b/configure.ac
index 1380c490f2ecf620f492661c909c62d18652ce84..2efefae2c912545164dd3cbc42f197b668f7f9b9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -20,7 +20,9 @@ dnl along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 AC_PREREQ([2.63])
 AC_INIT([dynare], [4.1-unstable])
 AC_CONFIG_SRCDIR([preprocessor/DynareMain.cc])
+AM_INIT_AUTOMAKE([-Wall -Werror foreign])
 
+AC_PROG_CC
 AC_PROG_CXX
 
 # Use C++ for testing headers
@@ -41,17 +43,15 @@ case $host_os in
     ;;
 esac
 
-AC_CHECK_PROGS([FLEX], [flex])
-if test -z "$FLEX"; then
-  AC_MSG_ERROR([No suitable flex found.])
-fi
+AC_PROG_RANLIB
+
+AM_PROG_LEX
+AX_PROG_FLEX([], AC_MSG_ERROR([No suitable flex found.]))
 
 AC_CHECK_HEADERS([FlexLexer.h], [], [AC_MSG_ERROR([Can't find FlexLexer.h])])
 
-AC_CHECK_PROGS([BISON], [bison])
-if test -z "$BISON"; then
-  AC_MSG_ERROR([No suitable bison found.])
-fi
+AC_PROG_YACC
+AX_PROG_BISON([], AC_MSG_ERROR([No suitable bison found.]))
 
 AC_CHECK_HEADERS([boost/graph/adjacency_list.hpp], [], [AC_MSG_ERROR([Can't find Boost Graph Library])])
 AC_CHECK_HEADERS([boost/graph/max_cardinality_matching.hpp], [], [AC_MSG_ERROR([Your Boost version is too old. Please install a newer one.])])
@@ -89,9 +89,8 @@ then
   AX_MATLAB_ARCH
 fi
 
-AX_DYNAREPP
-
 AC_CONFIG_FILES([Makefile
+                 preprocessor/macro/Makefile
                  preprocessor/Makefile
                  doc/Makefile
                  doc/preprocessor/Makefile
@@ -99,6 +98,7 @@ AC_CONFIG_FILES([Makefile
                  doc/userguide/Makefile
                  tests/Makefile
                  matlab/dynare_version.m
-                 windows/dynare-version.nsi])
+                 windows/dynare-version.nsi
+])
 
 AC_OUTPUT
diff --git a/m4/ax_prog_bison.m4 b/m4/ax_prog_bison.m4
new file mode 100644
index 0000000000000000000000000000000000000000..12eb9bf5efd187883d026286d4ad53aa3298dd2a
--- /dev/null
+++ b/m4/ax_prog_bison.m4
@@ -0,0 +1,62 @@
+# ===========================================================================
+#          http://www.nongnu.org/autoconf-archive/ax_prog_bison.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+#   AX_PROG_BISON(ACTION-IF-TRUE,ACTION-IF-FALSE)
+#
+# DESCRIPTION
+#
+#   Check whether bison is the parser generator. Run ACTION-IF-TRUE if
+#   successful, ACTION-IF-FALSE otherwise
+#
+# LICENSE
+#
+#   Copyright (c) 2009 Francesco Salvestrini <salvestrini@users.sourceforge.net>
+#
+#   This program is free software; you can redistribute it and/or modify it
+#   under the terms of the GNU General Public License as published by the
+#   Free Software Foundation; either version 2 of the License, or (at your
+#   option) any later version.
+#
+#   This program is distributed in the hope that it will be useful, but
+#   WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+#   Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License along
+#   with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+#   As a special exception, the respective Autoconf Macro's copyright owner
+#   gives unlimited permission to copy, distribute and modify the configure
+#   scripts that are the output of Autoconf when processing the Macro. You
+#   need not follow the terms of the GNU General Public License when using
+#   or distributing such scripts, even though portions of the text of the
+#   Macro appear in them. The GNU General Public License (GPL) does govern
+#   all other use of the material that constitutes the Autoconf Macro.
+#
+#   This special exception to the GPL applies to versions of the Autoconf
+#   Macro released by the Autoconf Archive. When you make and distribute a
+#   modified version of the Autoconf Macro, you may extend this special
+#   exception to the GPL to apply to your modified version as well.
+
+AC_DEFUN([AX_PROG_BISON], [
+  AC_REQUIRE([AC_PROG_YACC])
+  AC_REQUIRE([AC_PROG_SED])
+
+  AC_CACHE_CHECK([if bison is the parser generator],[ax_cv_prog_bison],[
+    AS_IF([test "`echo \"$YACC\" | $SED 's,^.*\(bison\).*$,\1,'`" = "bison" ],[
+      ax_cv_prog_bison=yes
+    ],[
+      ax_cv_prog_bison=no
+    ])
+  ])
+  AS_IF([test "$ax_cv_prog_bison" = yes],[
+    :
+    $1
+  ],[
+    :
+    $2
+  ])
+])
diff --git a/m4/ax_prog_flex.m4 b/m4/ax_prog_flex.m4
new file mode 100644
index 0000000000000000000000000000000000000000..cd723b2a7f65d61fe0ba1002580cdde28e143759
--- /dev/null
+++ b/m4/ax_prog_flex.m4
@@ -0,0 +1,62 @@
+# ===========================================================================
+#          http://www.nongnu.org/autoconf-archive/ax_prog_flex.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+#   AX_PROG_FLEX(ACTION-IF-TRUE,ACTION-IF-FALSE)
+#
+# DESCRIPTION
+#
+#   Check whether flex is the scanner generator. Run ACTION-IF-TRUE if
+#   successful, ACTION-IF-FALSE otherwise
+#
+# LICENSE
+#
+#   Copyright (c) 2009 Francesco Salvestrini <salvestrini@users.sourceforge.net>
+#
+#   This program is free software; you can redistribute it and/or modify it
+#   under the terms of the GNU General Public License as published by the
+#   Free Software Foundation; either version 2 of the License, or (at your
+#   option) any later version.
+#
+#   This program is distributed in the hope that it will be useful, but
+#   WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+#   Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License along
+#   with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+#   As a special exception, the respective Autoconf Macro's copyright owner
+#   gives unlimited permission to copy, distribute and modify the configure
+#   scripts that are the output of Autoconf when processing the Macro. You
+#   need not follow the terms of the GNU General Public License when using
+#   or distributing such scripts, even though portions of the text of the
+#   Macro appear in them. The GNU General Public License (GPL) does govern
+#   all other use of the material that constitutes the Autoconf Macro.
+#
+#   This special exception to the GPL applies to versions of the Autoconf
+#   Macro released by the Autoconf Archive. When you make and distribute a
+#   modified version of the Autoconf Macro, you may extend this special
+#   exception to the GPL to apply to your modified version as well.
+
+AC_DEFUN([AX_PROG_FLEX], [
+  AC_REQUIRE([AM_PROG_LEX])
+  AC_REQUIRE([AC_PROG_SED])
+
+  AC_CACHE_CHECK([if flex is the lexer generator],[ax_cv_prog_flex],[
+    AS_IF([test "`echo \"$LEX\" | $SED 's,^.*\(flex\).*$,\1,'`" = "flex"],[
+      ax_cv_prog_flex=yes
+    ],[
+      ax_cv_prog_flex=no
+    ])
+  ])
+  AS_IF([test "$ax_cv_prog_flex" = yes],[
+    :
+    $1
+  ],[
+    :
+    $2
+  ])
+])
diff --git a/m4/dynarepp.m4 b/m4/dynarepp.m4
deleted file mode 100644
index ad66610482e3ca57cf25bb3478b82cdf22290b1d..0000000000000000000000000000000000000000
--- a/m4/dynarepp.m4
+++ /dev/null
@@ -1,66 +0,0 @@
-dnl dynarepp.m4 --- check for Dynare++
-dnl
-dnl Copyright (C) 2009 Dynare Team
-dnl
-dnl This file is part of Dynare.
-dnl
-dnl Dynare is free software: you can redistribute it and/or modify
-dnl it under the terms of the GNU General Public License as published by
-dnl the Free Software Foundation, either version 3 of the License, or
-dnl (at your option) any later version.
-dnl
-dnl Dynare is distributed in the hope that it will be useful,
-dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
-dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-dnl GNU General Public License for more details.
-dnl
-dnl You should have received a copy of the GNU General Public License
-dnl along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
-dnl
-dnl Code:
-
-# AX_DYNAREPP
-# ---------
-# Check for Dynare++.
-AC_DEFUN([AX_DYNAREPP],
-[dnl
-AC_PREREQ([2.63])
-ax_enable_dynarepp=
-AC_ARG_WITH([dynarepp], AC_HELP_STRING([--with-dynarepp=ARG], [path to Dynare++]),
-[case $withval in
-  no)
-    # Explicitly enable or disable Dynare++ but determine
-    # Matlab prefix automatically.
-    ax_enable_dynarepp=no
-    ;;
-  *)
-    # Enable Dynare++ and use ARG as the Dynare++ prefix.
-    # ARG must be an existing directory.
-    ax_enable_dynarepp=yes
-    DYNAREPP=`cd "${withval-/}" > /dev/null 2>&1 && pwd`
-    if test -z "$DYNAREPP" ; then
-	AC_MSG_ERROR([invalid value '$withval' for --with-dynarepp])
-    fi
-    ;;
-esac])
-AC_MSG_CHECKING([for Dynare++])
-if test x$ax_enable_dynarepp != xno ; then
-    if test "${DYNAREPP+set}" = set && test -d "$DYNAREPP/kord" ; then
-	ax_enable_dynarepp=yes
-    elif test x$ax_enable_dynarepp = x ; then
-	ax_enable_dynarepp=no
-    else
-	# Fail if Matlab was explicitly enabled.
-	AC_MSG_RESULT([failure])
-	AC_MSG_ERROR([check your Dynare++ setup])
-    fi
-fi
-AC_MSG_RESULT([$ax_enable_dynarepp])
-AC_SUBST([DYNAREPP])
-])
-
-dnl dynarepp.m4 ends here
-
-dnl Local variables:
-dnl tab-width: 8
-dnl End:
diff --git a/preprocessor/Makefile.am b/preprocessor/Makefile.am
new file mode 100644
index 0000000000000000000000000000000000000000..cfd3ce8e8a94569223727481d0d27142c7608a73
--- /dev/null
+++ b/preprocessor/Makefile.am
@@ -0,0 +1,62 @@
+SUBDIRS = macro
+
+BUILT_SOURCES = DynareBison.hh stack.hh position.hh location.hh DynareBison.cc DynareFlex.cc
+
+matlabdir = $(libdir)/matlab
+
+matlab_PROGRAMS = dynare_m
+
+dynare_m_SOURCES = \
+	DynareFlex.ll \
+	DynareBison.yy \
+	ComputingTasks.cc \
+	ComputingTasks.hh \
+	ModelTree.cc \
+	ModelTree.hh \
+	StaticModel.cc \
+	StaticModel.hh \
+	StaticDllModel.cc \
+	StaticDllModel.hh \
+	DynamicModel.cc \
+	DynamicModel.hh \
+	NumericalConstants.cc \
+	NumericalConstants.hh \
+	NumericalInitialization.cc \
+	NumericalInitialization.hh \
+	Shocks.cc \
+	Shocks.hh \
+	SigmaeInitialization.cc \
+	SigmaeInitialization.hh \
+	SymbolTable.cc \
+	SymbolTable.hh \
+	SymbolList.cc \
+	SymbolList.hh \
+	ParsingDriver.cc \
+	ParsingDriver.hh \
+	DataTree.cc \
+	DataTree.hh \
+	ModFile.cc \
+	ModFile.hh \
+	Statement.cc \
+	Statement.hh \
+	ExprNode.cc \
+	ExprNode.hh \
+	MinimumFeedbackSet.cc \
+	MinimumFeedbackSet.hh \
+	IncidenceMatrix.cc \
+	IncidenceMatrix.hh \
+	BlockTriangular.cc \
+	BlockTriangular.hh \
+	DynareMain.cc \
+	DynareMain.hh \
+	DynareMain2.cc \
+	DynareMain2.hh \
+	CodeInterpreter.hh
+
+dynare_m_LDADD = macro/libmacro.a
+
+DynareFlex.cc: DynareFlex.ll
+	$(LEX) -oDynareFlex.cc DynareFlex.ll
+
+DynareBison.cc DynareBison.hh location.hh stack.hh position.hh: DynareBison.yy
+	$(YACC) -o DynareBison.cc DynareBison.yy
diff --git a/preprocessor/macro/Makefile.am b/preprocessor/macro/Makefile.am
new file mode 100644
index 0000000000000000000000000000000000000000..ca0e8344427327a01408e77ce2bbead74ada78d5
--- /dev/null
+++ b/preprocessor/macro/Makefile.am
@@ -0,0 +1,17 @@
+noinst_LIBRARIES = libmacro.a
+
+BUILT_SOURCES = MacroBison.hh stack.hh position.hh location.hh MacroBison.cc MacroFlex.cc
+
+libmacro_a_SOURCES = \
+	MacroFlex.ll \
+	MacroBison.yy \
+	MacroDriver.cc \
+	MacroDriver.hh \
+	MacroValue.cc \
+	MacroValue.hh
+
+MacroFlex.cc: MacroFlex.ll
+	$(LEX) -oMacroFlex.cc MacroFlex.ll
+
+MacroBison.cc MacroBison.hh location.hh stack.hh position.hh: MacroBison.yy
+	$(YACC) -o MacroBison.cc MacroBison.yy