c++从文件中加载总是重复最后一个字符

C++ load from file keeps repeating last character

本文关键字:最后一个 字符 文件 加载 c++      更新时间:2023-10-16

所以我在做一个编译器,我有这个LEXER的一部分,它检查一些字符是否形成一个整数或一个实数满足这个EBNF //REAL: DIGIT {DIGIT} . DIGIT {DIGIT} [ (e|E) [(+ | -)] <DIGIT> {DIGIT}


我有这部分代码(它的片段),它检查虽然不是EOF或令牌不匹配,但它继续对令牌进行分类

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));
                                }

问题在于当我必须加载文本文件并从中获取字符时。例如,如果我写123.12 WITH a Space, Lexer将在空白处停止。如果在EOF处没有空格,最后一个do while循环将永远重复。


Next Char的实现*input是一个流声明为:
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;
        }

你知道我该怎么解决这个问题吗?

谢谢

我要把nextChar改成:

int nextChar()
{
   int ch = input->getc();
   if ( ch == EOF )
   {
      return ch;
   }
   //check for new line. If true, increment Row and start column from 1
   else if(ch == 'n')
   {
      row ++;
      col = 1;
   }
   else if (ch == 't')
   {
      col +=4;
   }
   else
   {
      col++;
   }
   return ch;
}

并确保在调用getChar的地方使用int类型的变量,并在继续之前将返回的值与EOF进行比较。