Commit 41a18b1d authored by sebastien's avatar sebastien
Browse files

v4 parser: reindentation of all C++ code, and conversion to UNIX newline convention


git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1031 ac1d8469-bf42-47a9-8791-bf33cf982152
parent c50f757b
This diff is collapsed.
/*! \file /*! \file
\version 1.0 \version 1.0
\date 04/09/2004 \date 04/09/2004
\par This file implements the DataTree class methodes. \par This file implements the DataTree class methodes.
*/ */
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <map> #include <map>
//#include <ext/hash_map> //#include <ext/hash_map>
using namespace std; using namespace std;
//using __gnu_cxx::hash; //using __gnu_cxx::hash;
//using __gnu_cxx::hash_map; //using __gnu_cxx::hash_map;
#include <time.h> #include <time.h>
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#include "DynareBison.hh" #include "DynareBison.hh"
#include "VariableTable.hh" #include "VariableTable.hh"
#include "NumericalConstants.hh" #include "NumericalConstants.hh"
#include "DataTree.hh" #include "DataTree.hh"
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
const int DataTree::NoOpCode = -1; const int DataTree::NoOpCode = -1;
const NodeID DataTree::NullID = NULL; const NodeID DataTree::NullID = NULL;
const NodeID DataTree::Zero = new MetaToken(reinterpret_cast <NodeID> (0), eNumericalConstant, NULL, -1); const NodeID DataTree::Zero = new MetaToken(reinterpret_cast <NodeID> (0), eNumericalConstant, NULL, -1);
const NodeID DataTree::One = new MetaToken(reinterpret_cast <NodeID> (1), eNumericalConstant, NULL, -1); const NodeID DataTree::One = new MetaToken(reinterpret_cast <NodeID> (1), eNumericalConstant, NULL, -1);
const NodeID DataTree::MinusOne = new MetaToken(One, eTempResult, NULL, UMINUS); const NodeID DataTree::MinusOne = new MetaToken(One, eTempResult, NULL, UMINUS);
const NodeID DataTree::ZeroEqZero = new MetaToken(Zero, eTempResult, Zero, EQUAL); const NodeID DataTree::ZeroEqZero = new MetaToken(Zero, eTempResult, Zero, EQUAL);
int DataTree::offset = 1; int DataTree::offset = 1;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
DataTree::DataTree() DataTree::DataTree()
{ {
current_order = 0; current_order = 0;
//Here "0" and "1" have been added to NumericalConstants class //Here "0" and "1" have been added to NumericalConstants class
SymbolTable::AddSymbolDeclar("0.0",eNumericalConstant, ""); SymbolTable::AddSymbolDeclar("0.0",eNumericalConstant, "");
SymbolTable::AddSymbolDeclar("1.0",eNumericalConstant, ""); SymbolTable::AddSymbolDeclar("1.0",eNumericalConstant, "");
Zero->op_name = ""; Zero->op_name = "";
Zero->reference_count.resize(current_order+1,2); Zero->reference_count.resize(current_order+1,2);
Zero->idx = 0; Zero->idx = 0;
mModelTree.push_back(Zero); mModelTree.push_back(Zero);
mIndexOfTokens[Zero->Key()]=Zero; mIndexOfTokens[Zero->Key()]=Zero;
One->op_name = "";
One->op_name = ""; One->reference_count.resize(current_order+1,1);
One->reference_count.resize(current_order+1,1); One->idx = 1;
One->idx = 1; mModelTree.push_back(One);
mModelTree.push_back(One); mIndexOfTokens[One->Key()]=One;
mIndexOfTokens[One->Key()]=One;
MinusOne->op_name = operator_table.str(UMINUS);
MinusOne->op_name = operator_table.str(UMINUS); MinusOne->reference_count.resize(current_order+1,1);
MinusOne->reference_count.resize(current_order+1,1); MinusOne->idx = 2;
MinusOne->idx = 2; mModelTree.push_back(MinusOne);
mModelTree.push_back(MinusOne); mIndexOfTokens[MinusOne->Key()]=MinusOne;
mIndexOfTokens[MinusOne->Key()]=MinusOne;
// Pushing "0=0" into mModelTree
// Pushing "0=0" into mModelTree ZeroEqZero->op_name = operator_table.str(EQUAL);
ZeroEqZero->op_name = operator_table.str(EQUAL); ZeroEqZero->reference_count.resize(current_order+1,1);
ZeroEqZero->reference_count.resize(current_order+1,1); ZeroEqZero->idx = 3;
ZeroEqZero->idx = 3; mModelTree.push_back(ZeroEqZero);
mModelTree.push_back(ZeroEqZero); mIndexOfTokens[ZeroEqZero->Key()]=ZeroEqZero;
mIndexOfTokens[ZeroEqZero->Key()]=ZeroEqZero;
BeginModel = mModelTree.end();
BeginModel = mModelTree.end(); BeginModel--;
BeginModel--; }
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
DataTree::~DataTree() DataTree::~DataTree()
{ {
for (TreeIterator it = mModelTree.begin(); it != mModelTree.end(); it++) for (TreeIterator it = mModelTree.begin(); it != mModelTree.end(); it++)
{ {
if (*it != NullID) delete *it; if (*it != NullID) delete *it;
} }
} }
/*! \file DynareMain.cc /*! \file DynareMain.cc
\version 1.0 \version 1.0
\date 04/26/2004 \date 04/26/2004
\par Main file of Dynare Parser. \par Main file of Dynare Parser.
*/ */
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <stdio.h> #include <stdio.h>
using namespace std; using namespace std;
#include "DynareParser.hh" #include "DynareParser.hh"
#include "DynareScanner.hh" #include "DynareScanner.hh"
#include "OutputFile.hh" #include "OutputFile.hh"
#include "Interface.hh" #include "Interface.hh"
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
/*! main function /*! main function
\brief Main function of Dynare. \brief Main function of Dynare.
\param argc Number of command line argumetns from runtime system \param argc Number of command line argumetns from runtime system
\param argv Command line arguments from runtime system \param argv Command line arguments from runtime system
*/ */
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
OutputFile output_file; OutputFile output_file;
ostringstream output; ostringstream output;
int retval = 0; int retval = 0;
try { try
if (argc <2) {
{ if (argc <2)
cout << "Missing model file\n"; {
cout << "Dynare usage : dynare model_file [debug]\n"; cout << "Missing model file\n";
exit(-1); cout << "Dynare usage : dynare model_file [debug]\n";
} exit(-1);
}
// create a new scan buffer if a file name was passed to the program
ylmm::basic_buffer* input = 0; // create a new scan buffer if a file name was passed to the program
input = new ylmm::basic_buffer(argv[1],false); ylmm::basic_buffer* input = 0;
input = new ylmm::basic_buffer(argv[1],false);
// Create the scanner and parser
dynare::scanner s(input); // Create the scanner and parser
dynare::parser p(s); dynare::scanner s(input);
dynare::parser p(s);
// Create the messenger and set it in the parser and scanner
ylmm::basic_messenger<ylmm::basic_lock> out; // Create the messenger and set it in the parser and scanner
s.messenger(out); ylmm::basic_messenger<ylmm::basic_lock> out;
p.messenger(out); s.messenger(out);
// Sets the file name in parser instance p.messenger(out);
p.set_file_name(argv[1]); // Sets the file name in parser instance
// Sets string output of scanner p.set_file_name(argv[1]);
s.setoutput(&output); // Sets string output of scanner
// Sets string output of parser s.setoutput(&output);
p.setoutput(&output); // Sets string output of parser
for (int arg=2; arg < argc ; arg++) p.setoutput(&output);
{ for (int arg=2; arg < argc ; arg++)
if (string(argv[arg]) == string("debug")) {
p.tracing(true); if (string(argv[arg]) == string("debug"))
else if (string(argv[arg]) == string("noclearall")) p.tracing(true);
output_file.clear_all = false; else if (string(argv[arg]) == string("noclearall"))
} output_file.clear_all = false;
// Parse the input }
cout << "Starting Dynare ...\n"; // Parse the input
cout << "Parsing your model file ...\n"; cout << "Starting Dynare ...\n";
retval = p.parse(0); cout << "Parsing your model file ...\n";
if (retval != 0) exit(-1); retval = p.parse(0);
// Execute final instructions if (retval != 0) exit(-1);
p.finish(); // Execute final instructions
string name = argv[1]; p.finish();
name.erase(name.size()-4,4); string name = argv[1];
// Opening and init main Output file (.m or .sci file) name.erase(name.size()-4,4);
output_file.Open(name); // Opening and init main Output file (.m or .sci file)
// Writing remaining string output to output file output_file.Open(name);
output_file.Save(output); // Writing remaining string output to output file
output_file.Save(output);
}
// Handeling parser and scanner exeptions }
catch (std::exception& e) { // Handeling parser and scanner exeptions
cout << e.what() << std::endl; catch (std::exception& e)
return 0; {
} cout << e.what() << std::endl;
cout << "Parsing done\n"; return 0;
cout << "Starting Matlab computing ...\n"; }
return retval; cout << "Parsing done\n";
} cout << "Starting Matlab computing ...\n";
//------------------------------------------------------------------------------ return retval;
}
//------------------------------------------------------------------------------
This diff is collapsed.
/*! \file /*! \file
\version 1.0 \version 1.0
\date 04/09/2004 \date 04/09/2004
\par This file implements the Expression class methodes. \par This file implements the Expression class methodes.
*/ */
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#include <stack> #include <stack>
using namespace std; using namespace std;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#include "Expression.hh" #include "Expression.hh"
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
ostringstream Expression::output; ostringstream Expression::output;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
Expression::Expression() Expression::Expression()
{ {
// Empty // Empty
} }
//------------------------------------------------------------------------------
Expression::~Expression() //------------------------------------------------------------------------------
{ Expression::~Expression()
// Empty {
} // Empty
//------------------------------------------------------------------------------ }
int Expression::AddToken(int id1,Type type1,int id2,Type type2,int op_code)
{ //------------------------------------------------------------------------------
Token token; int Expression::AddToken(int id1,Type type1,int id2,Type type2,int op_code)
// Making token structure {
token.id1 = id1; Token token;
token.type1 = type1; // Making token structure
token.id2 = id2; token.id1 = id1;
token.type2 = type2; token.type1 = type1;
token.op_code = op_code; token.id2 = id2;
token.op_name = operator_table.str(op_code); token.type2 = type2;
// Inserting token into expression_list token.op_code = op_code;
expression_list.push_back(token); token.op_name = operator_table.str(op_code);
return expression_list.size() -1; // Inserting token into expression_list
} expression_list.push_back(token);
//------------------------------------------------------------------------------ return expression_list.size() -1;
int Expression::AddToken(int id1,Type type1,int op_code) }
{
Token token; //------------------------------------------------------------------------------
// Making token structure int Expression::AddToken(int id1,Type type1,int op_code)
token.id1 = id1; {
token.type1 = type1; Token token;
token.id2 = -1; // Making token structure
token.type2 = eUNDEF; token.id1 = id1;
token.op_code = op_code; token.type1 = type1;
token.op_name = operator_table.str(op_code);; token.id2 = -1;
// Inserting token into expression_list token.type2 = eUNDEF;
expression_list.push_back(token); token.op_code = op_code;
return expression_list.size() -1; token.op_name = operator_table.str(op_code);;
} // Inserting token into expression_list
//------------------------------------------------------------------------------ expression_list.push_back(token);
int Expression::AddToken(int id1,Type type1, string ufunction) return expression_list.size() -1;
{ }
Token token;
// Making token structure //------------------------------------------------------------------------------
token.id1 = id1; int Expression::AddToken(int id1,Type type1, string ufunction)
token.type1 = type1; {
token.id2 = -1; Token token;
token.type2 = eUNDEF; // Making token structure
token.op_code = NAME; token.id1 = id1;
token.op_name = ufunction; token.type1 = type1;
// Inserting token into expression_list token.id2 = -1;
expression_list.push_back(token); token.type2 = eUNDEF;
return expression_list.size() -1; token.op_code = NAME;
} token.op_name = ufunction;
//------------------------------------------------------------------------------ // Inserting token into expression_list
void Expression::set(void) expression_list.push_back(token);
{ return expression_list.size() -1;
// Stack of temporary tokens }
stack <int, vector<Token> > stack_token;
// Dtack of temporary expressions //------------------------------------------------------------------------------
stack <int, vector<string> > stack_expression; void Expression::set(void)
// Temporary output {
ostringstream exp; // Stack of temporary tokens
// temporary variables for saving arguments and name oparator stack <int, vector<Token> > stack_token;
string argument1, argument2, op_name; // Dtack of temporary expressions
// Define type for type operator (binary or unary) stack <int, vector<string> > stack_expression;
enum OperatorType // Temporary output
{ ostringstream exp;
unary, // temporary variables for saving arguments and name oparator
binary string argument1, argument2, op_name;
}; // Define type for type operator (binary or unary)
OperatorType op_type; enum OperatorType
int current_op, last_op; {
unary,
// Clearing output string binary
output.str(""); };
// Starting from the end of list OperatorType op_type;
stack_token.push(expression_list.back()); int current_op, last_op;
// Main loop :
// Repeat for last token from the stack // Clearing output string
// (1) if argument is temporary result, and not yet followed, output.str("");
// set it as followed (flag) and push corresponding token // Starting from the end of list
// on the token stack stack_token.push(expression_list.back());
// (2) argument followed, or final argument // Main loop :
// (2.1) if argument is followed // Repeat for last token from the stack
// - set argument1 (or argument2) by last expression on // (1) if argument is temporary result, and not yet followed,
// expression tack // set it as followed (flag) and push corresponding token
// - pop last expression from expression stack // on the token stack
// (2.2) if final argument // (2) argument followed, or final argument
// set argument1 (or argument2) by final argument // (2.1) if argument is followed
// (3) set op_name by last token from the token stack // - set argument1 (or argument2) by last expression on
// (3) pop last token from the token stack // expression tack
// (4) write temporary expression (using argument1, argument2 // - pop last expression from expression stack
// and op_name) and push it on the expression stack // (2.2) if final argument
// (5) // set argument1 (or argument2) by final argument
// (3) set op_name by last token from the token stack
while (stack_token.size() > 0) // (3) pop last token from the token stack
{ // (4) write temporary expression (using argument1, argument2
// First argument is a temporary result, // and op_name) and push it on the expression stack
// pushing token on token stack and setting that argument to be followed // (5)
if ((stack_token.top().type1 == eTempResult) &&
(stack_token.top().followed1 == false)) while (stack_token.size() > 0)
{ {
stack_token.top().followed1 = true; // First argument is a temporary result,
stack_token.push(expression_list[stack_token.top().id1]); // pushing token on token stack and setting that argument to be followed
} if ((stack_token.top().type1 == eTempResult) &&
// Second argument is a temporary result, (stack_token.top().followed1 == false))
// pushing token on stack and setting that argument to be followed {
else if ((stack_token.top().type2 == eTempResult) && stack_token.top().followed1 = true;
(stack_token.top().followed2 == false)) stack_token.push(expression_list[stack_token.top().id1]);
{ }
stack_token.top().followed2 = true; // Second argument is a temporary result,
stack_token.push(expression_list[stack_token.top().id2]); // pushing token on stack and setting that argument to be followed
} else if ((stack_token.top().type2 == eTempResult) &&
// Writing expression (stack_token.top().followed2 == false))
else {
{ stack_token.top().followed2 = true;
// Final token, no argment followed stack_token.push(expression_list[stack_token.top().id2]);
if ((stack_token.top().followed1 == false) && }
(stack_token.top().followed2 == false)) // Writing expression
{ else
argument1 = getArgument(stack_token.top().type1,stack_token.top().id1); {
current_op = stack_token.top().op_code; // Final token, no argment followed
// Testing if unary or binary token if ((stack_token.top().followed1 == false) &&
if (stack_token.top().id2 >= 0) (stack_token.top().followed2 == false))
{ {
argument2 = getArgument(stack_token.top().type2,stack_token.top().id2); argument1 = getArgument(stack_token.top().type1,stack_token.top().id1);
op_type = binary; current_op = stack_token.top().op_code;
} // Testing if unary or binary token
else if (stack_token.top().id2 >= 0)
{ {
op_type = unary; argument2 = getArgument(stack_token.top().type2,stack_token.top().id2);
} op_type = binary;
} }
// Both arguments are followed, writing stacked expressions else
else if ((stack_token.top().followed1 == true) && {
(stack_token.top().followed2 == true)) op_type = unary;
{ }
// Testing if unary or binary token }
if (stack_token.top().id2 >= 0) // Both arguments are followed, writing stacked expressions
{ else if ((stack_token.top().followed1 == true) &&
argument2 = stack_expression.top(); (stack_token.top().followed2 == true))
stack_expression.pop(); {
op_type = binary; // Testing if unary or binary token
if (stack_token.top().id2 >= 0)
} {
else argument2 = stack_expression.top();
{ stack_expression.pop();
op_type = unary; op_type = binary;
}
argument1 = stack_expression.top(); }
current_op = stack_token.top().op_code; else
stack_expression.pop(); {
op_type = unary;
}