//      The C++ Scanner interface for Demeter System:
//                        By
//                   Chen-Dah Shen 
//
#include <osfcn.h>
#include "getsym_DEM.h"

__token _Token1;
__token* Token=&_Token1;

__token* __token::scanner(char* filename) {
  char cmd[100];
  int status;

  if ((fp = fopen(filename,"r")) == NULL) {
     cerr << "Scanner: Can't find the " << filename << "\n";
     exit(1);
  }
  //  Collect the command
  sprintf(cmd,"%s %s %s","DEM-generated-not-modifiable/lex_analyzer<",filename,">lexical_tokens");
  //  Split the process. The child will go into the sub-shell.
  if (fork() == 0)
     execlp("/bin/sh","sh","-c",cmd, (char *) 0);
  wait(&status);    //  return back to parent process
  if (status) {
     cerr << "Scanner: can't create the lex_tokens \n";
     exit(2);
  }
  fp = fopen("lexical_tokens","r");
  return(NULL);    //meaningless ! Just keep compiler quiet.
}

__token* __token::get_sym() {
  if (fscanf(fp,"%d %s",&kind,value) == EOF) {
     kind = END;
     fclose(fp); 
     return(NULL);
  }
  return(this);  //meaningless ! distinguished from the end of file
}



__token* __token::scanner_dh(char* filename) {
  char cmd[200];
  int status;

  if ((fp = fopen(filename,"r")) == NULL) {
     cerr << "Scanner: Can't find the " << filename << "\n";
     exit(1);
  }
  //  Collect the command
  sprintf(cmd,"%s %s %s","/eric/csgrads/qbian/generic-parser-c++/dh-complete/DEM-generated-not-modifiable/lex_analyzer<",filename,">lexical_tokens");
  //  Split the process. The child will go into the sub-shell.
  if (fork() == 0)
     execlp("/bin/sh","sh","-c",cmd, (char *) 0);
  wait(&status);    //  return back to parent process
  if (status) {
     cerr << "Scanner: can't create the lex_tokens \n";
     exit(2);
  }
  fp = fopen("lexical_tokens","r");
  return(NULL);    //meaningless ! Just keep compiler quiet.
}


