So I am doing a compiler, and I have part of this LEXER which checks whether some chars form an INTEGER or a REAL number which is satisfied by this EBNF //REAL: DIGIT {DIGIT} . DIGIT {DIGIT} [ (e|E) [(+ | -)] <DIGIT> {DIGIT}
I have this part of the code (snippet of it) which checks that while it is not EOF or token is not match, it continues categorising the Tokens
while (!isEOF() && !tokenMatch)
{
//Checking for INTEGERS and REAL
//INTEGERS: DIGIT {DIGIT}
if (isDigit(ch))
{
strBuffer += ch;
do
{
ch = nextChar();
strBuffer += ch;
}
while (isDigit(ch));
//REAL or ERROR
//REAL: DIGIT {DIGIT} . DIGIT {DIGIT} [ (e|E) [(+ | -)] <DIGIT> {DIGIT}
if (ch == '.')
{
do
{
ch = nextChar();
strBuffer += ch;
}
while (isDigit(ch));
//EXPONENT
if (ch == 'E' || ch == 'e')
{
char peek = input -> peek();
//CHECK FOR + | -
if(peek == '+' || peek == '-')
{
ch = nextChar();
strBuffer += ch;
ch = nextChar();
if (isDigit(ch))
{
do
{
strBuffer +=ch;
ch = nextChar();
cout << strBuffer << endl;
}
while (isDigit(ch));
}
The problem lies when I have to load the text file and get the characters from it. IF for example a I write 123.12
WITH a Space, the Lexer will stop at the Whitespace. IF there are NO whitespace at EOF, the last do while loop keeps on repeating forever.
Implementation of Next Char *input is an instream declared as:
ifstream* input = new ifstream("test.txt");
char nextChar()
{
*input >> noskipws >> ch;
//check for new line. If true, increment Row and start column from 1
if(ch == '\n')
{
row ++;
col = 1;
}
else if (ch == '\t')
{
col +=4;
}
else
{
col++;
}
return ch;
}
Any idea how I can fix this?
thanks