/* Simple main() that demonstrates how to interact with parser generated
   by flex & bison.

   Reads and evaluates an arithmetic expression.

   If a filename is specified on the command line, the expression is 
   read from the file.

   If no filename is specified, the expression is read from standard 
   input (from the keyboard).

*/

#include "parser.h" // substitute for y.tab.h
#include "error.h"

#include <iostream>
#include <stdio.h> // for fopen()

extern int yylex();
extern int yyparse();

extern "C" int yywrap ( );
int yywrap(void)
{
  return true;
}

// this function is called by bison when it encounters an error
int yyerror(char *str)
{
  Error::error(Error::PARSE_ERROR, str);
  return 0;
}


int main(int argc, char **argv)
{
  // lexer generated by flex reads from yyin
   extern FILE *yyin;

  // if a command line argument is given, assume it is a filename
  // to read from
  if (argc == 2 && argv[1])
  {
    yyin = fopen(argv[1],"r");

    cout << "parser.cpp::main() reading file " << argv[1] << endl;
  }
  // else read from standard input (default for lex/flex)
  else
  {
    cout << "parser.cpp::main() reading standard input." << endl;
  }

  cout << "parser.cpp::main() about to call yyparse()" << endl
       << "type expression followed by control-D:" << endl
       << endl;

  int parse_result = yyparse();
  cout << endl
       << "parser.cpp::main() after call to yyparse()" << endl
       << endl;

  if (parse_result != 0)
    cout << "yyparse() found errors" << endl;
  else cout << "no errors found by yyparse()" << endl;

  cout << endl << "end of parser.cpp::main()" << endl;
}

