My program is suppose to read in numbers and tokens. However, it will only read in the first number and go into a loop. Never reads the next number or does the computations. Can't figure out what's wrong.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
//____________________________________________________________________________________
using namespace std;
//____________________________________________________________________________________
#include "Stack03.h"
#include "Scan03.h"
//____________________________________________________________________________________
void PostfixMgr(FILE* i, ostream& o);
//____________________________________________________________________________________
int main(int argc, char* argv[])
{
try
{
char infile[255]; // input file
char outfile[255]; // output file
if (argc == 1)
{
cout << " Enter the input file name. " << endl;
cin >> infile;
cout << " Enter the output file name. " << endl;
cin >> outfile;
cout << endl;
}
else if (argc == 2)
{
strcpy(infile, argv[1]);
cout << " Enter the output file name. " << endl;
cin >> outfile;
cout << endl;
}
else if ( argc == 3)
{
strcpy(infile, argv[1]);
strcpy(outfile, argv[2]);
}
else
{
throw CommandLineException(2,argc -1);
}
FILE* i= fopen(infile,"r");
if(!i)
throw FileException(infile);
ofstream o(outfile);
if(!o)
throw FileException(outfile);
PostfixMgr(i,o);
fclose(i);
} catch(...)
{
cout << " Program Terminated. " << endl;
exit(EXIT_FAILURE);
}
return 0;
} // end of main.
void PostfixMgr(FILE* i, ostream& o)
{
Scan L(i);
Stack s;
int k = 0;
int front=0;
int back = 0;
int divisor =0;
int div = 0;
int answer = 0;
int counter = 0;
for(;;)
{
int t=L.Lex();
if(t==0)
{
break;
cout << endl;
cout << setw(4) << t;
cout << setw(30) << L.FetchSpelling();
}
switch (t)
{
case INTLIT:
sscanf(L.FetchSpelling(),"%d",&k);
s.Push(k);
break;
case PLUS:
s.Push(s.Pop() + s.Pop());
break;
case MINUS:
back = s.Pop();
front = s.Pop();
s.Push(front - back);
break;
case SLASH:
div = s.Pop();
divisor = s.Pop();
s.Push(divisor/div);
break;
case STAR:
s.Push(s.Pop() + s.Pop());
break;
default:
cout << " Did not recognize token" << endl;
cout << endl;
break;
} // end of switch statement.
while ( !s.Is_Empty())
{
o << "S[tos]=" << " " << s.Pop() << endl;
o << endl;
counter++;
} // end of while loop.
} // end of for loop.
} // end of PostfixMgr
#include <cstdio>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <cstdlib>
//____________________________________________________________________________________
using namespace std;
//____________________________________________________________________________________
#include "Stack03.h"
CommandLineException::CommandLineException(int max, int actual)
{
cout << " Too many command line arguments. " << endl;
}
//____________________________________________________________________________________
FileException::FileException(char* fn)
{
cout << " File " << fn << " could not be opened. " << endl;
}
//____________________________________________________________________________________
Stack::Element::Element(Element* p, int v)
{
prev = p; // initialization of variables of class Element.
value = v; // initialization of variables of class Element.
}
//____________________________________________________________________________________
Stack::Element* Stack::Element::Prev(void)
{
return prev;
}
//____________________________________________________________________________________
int Stack::Element::Value(void)
{
return value;
}
//____________________________________________________________________________________
//____________________________________________________________________________________
Stack::StackException::StackException(char* m)
{
cout << endl;
cout << " I am the stack and I am " << m << " . " << endl;
}
//____________________________________________________________________________________
Stack::Stack()
{
top_of_stack = 0; // initialization of variables of class Stack.
}
//____________________________________________________________________________________
Stack::~Stack()
{
Kill(top_of_stack);
}
//____________________________________________________________________________________
void Stack::Kill(Element* e)
{
while(e)
{
Element* p = e;
e = e->Prev();
delete[]p;
} // end of while loop.
}
//____________________________________________________________________________________
bool Stack::Is_Full(void)
{
return false;
}
//____________________________________________________________________________________
bool Stack::Is_Empty(void)
{
return top_of_stack == 0;
}
//____________________________________________________________________________________
void Stack::Push(int v)
{
if (Is_Full())
{
throw StackException("Full");
}
Element* n = new Element(top_of_stack,v);
top_of_stack = n;
}
//____________________________________________________________________________________
int Stack::Pop(void)
{
if (Is_Empty())
{
throw StackException("Empty");
}
Element* n = top_of_stack;
int v=n->Value();
top_of_stack = n;
delete[]n;
return v;
}
#ifndef STACK_H
#define STACK_H
#include <cstdio>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <cstdlib>
//____________________________________________________________________________________
using namespace std;
class CommandLineException
{
public:
CommandLineException(int max, int actual);
};
//____________________________________________________________________________________
class FileException
{
public:
FileException(char* fn);
};
//____________________________________________________________________________________
class Stack
{
class Element
{
private:
Element* prev;
int value;
public:
Element(Element*p, int v); // member function of class Element
Element* Prev(void); // member function of class Element
int Value(void); // member function of class Element
}; // end of the class Element
//____________________________________________________________________________________
class StackException
{
public:
StackException(char* m); // constructor of class StackException
}; // end of class StackException
private:
Element* top_of_stack;
void Kill(Element* e); // member function of class Stack
public:
Stack();
~Stack();
bool Is_Full(void);
bool Is_Empty(void);
void Push(int v);
int Pop(void);
}; // end of class Stack
#endif
#include <string>
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
//--------------------------------------------------------------------
// Application Includes
//--------------------------------------------------------------------
#include "Scan03.h"
//--------------------------------------------------------------------
//Function prototypes
//--------------------------------------------------------------------
int TokenMgr(int T);
//--------------------------------------------------------------------
//Global Variables
//--------------------------------------------------------------------
%}
%%
[ \t\n]+ ;
[+-]?[0-9]+ {
return(TokenMgr(INTLIT));
}
"+" {
return(TokenMgr(PLUS));
}
"-" {
return(TokenMgr(MINUS));
}
"*" {
return(TokenMgr(STAR));
}
"/" {
return(TokenMgr(SLASH));
}
%%
//--------------------------------------------------------------------
int TokenMgr(int T)
{ return T;
}
//--------------------------------------------------------------------
//Class Scan implementation
//--------------------------------------------------------------------
//Constructor Scan is used to redirect the input file stream from the
//keyboard to input file stream i.
//--------------------------------------------------------------------
Scan::Scan(FILE* i)
{ yyin=i;
}
//--------------------------------------------------------------------
//Function Lex calls yylex
//--------------------------------------------------------------------
int Scan::Lex(void)
{ return tokencode=yylex();
}
//--------------------------------------------------------------------
//Function FetchSpelling returns a pointer to the spelling of the most
//recent token.
//--------------------------------------------------------------------
char* Scan::FetchSpelling(void)
{ return (char*)yytext;
}
//--------------------------------------------------------------------
//Function FetchTokenCode returns the code of the most recent token
//--------------------------------------------------------------------
int Scan::FetchTokenCode(void)
{ return tokencode;
}
//--------------------------------------------------------------------
//Function StoreTokenCode records the most recent token code
//--------------------------------------------------------------------
void Scan::StoreTokenCode(int T)
{ tokencode=T;
}
//-----------------------End of Lex Definition--
#include <cstdio>
#include <fstream>
#include <iostream>
//--------------------------------------------------------------------
//Namespaces
//--------------------------------------------------------------------
using namespace std;
//--------------------------------------------------------------------
//Token code definitions
//--------------------------------------------------------------------
#define INTLIT 1
#define PLUS 2
#define MINUS 3
#define STAR 4
#define SLASH 5
//--------------------------------------------------------------------
//--------------------------------------------------------------------
#ifdef __cplusplus
extern "C"
#endif
int yylex (void);
//--------------------------------------------------------------------
//Class Scan defines the attributes of a Scanner
//--------------------------------------------------------------------
class Scan {
int tokencode; //Code for the most recent token found
public:
Scan(FILE* i); //Redirect the input source from the
//keyboard to input file i.
int Lex(void); //Call the scanner yylex and return the code
//found by yylex
int FetchTokenCode(void); //Return the code of the most recent token
void StoreTokenCode(int T); //Store the token code.
char* FetchSpelling(void); //Return the spelling of the most recent
//token
};
#endif