В Flex когда обновляется yylineno?

Я хочу использовать flex, чтобы получить текущий номер строки. похоже, что у flex есть глобальная переменная yylineno, чтобы сохранить текущий номер строки при компиляции. Несомненно, yylineno будет увеличиваться на 1 при совпадении \ n. но изменяет ли ‘r $’, совпадающую со строкой в ​​конце строки, yylineno? в противном случае, есть ли еще ситуации, когда yylineno обновляется?


Например, у меня есть исходный файл, в котором всего 71 строка / * Автор: guanwanxian date: 2014-12-29 * /

#include "cstdio"
#include "iostream"
#include "cmath"
#include "tchar.h"

using namespace std;
#define MAX 10000

//This is a struct to represent a Point in two-dimension plane
//Take a test
struct Point{
    Point(double XPos_N,double YPos_N){
        m_XPos=XPos_N;
        m_YPos=YPos_N;
    }
    double CalDistanceWithAnotherPoint(Point& OtherPoint)
    {
        double Dis=sqrt((m_XPos-OtherPoint.m_XPos)*(m_XPos-OtherPoint.m_XPos)+(m_YPos-OtherPoint.m_YPos)*(m_YPos-OtherPoint.m_YPos));
        return Dis;
    }
    double m_XPos;
    double m_YPos;
};

//this is a function to print Hello World
void PrintHelloWorld()
{
    for(int i=0;i<10;i++)
    {
        printf("Hello World\n");
    }
}

/*
this is a function to calculate the sun of two integers
balabala
2014-12-31
*/
int CalSum(int x , int y)
{
    int sum=x+y;
    return sum;
}

/*
this is the Main function
this is the enterance of my program
this is just a test program
*/
int _tmain(int argc, _TCHAR* argv[])
{
    int A=23;
    int B=34;
    int SumOfAB=CalSum(A,B);
    _tprintf(_T("The sum of A and B is:%d \n"),SumOfAB);
    PrintHelloWorld();

    Point AP(0,0);
    Point BP(2,3);
    double DisBetAP_AND_BP=AP.CalDistanceWithAnotherPoint(BP);
    _tprintf(_T("The distance between AP and BP is:%lf\n"),DisBetAP_AND_BP);

    return 0;
}

И мой гибкий файл:

%option noyywrap
%option yylineno

%{
    #include <cstdlib>
    #include <iostream>
    #include "tchar.h"
    #include "parser.hpp"
    extern int SourceFileLength;//The size of input file

    // this function will be generated using bison
    extern int yyparse();
    int DigitNum=0;
    int CommentLineNum=0;
    int ProgramLineNum=0; 
%}

Digits  [0-9]+

BinoOP  [-+*/]
parenthesis [()]

%s IN_BLOCK_COMMENT
%s IN_SINGLELINE_COMMENT
%s NOT_COMMENT
%s IN_FUNCTION

%%
<INITIAL>{
"//"  {
        BEGIN(IN_SINGLELINE_COMMENT);
        std::cout<< "enter single line comment\n"; 
      }
"/*"  {
        std::cout<<"block line num: "<<yylineno<<std::endl;
        BEGIN(IN_BLOCK_COMMENT);
        std::cout<< "enter block comment\n";
      }
([^\/\ \n][^\ \n]*)|(\/[^\*\/\ \n][^\ \n]*)|(\/) {  std::cout << yytext <<std::endl;}
\n   {std::cout << std::endl; ProgramLineNum++; } 
<<EOF>> { std::cout<<"TotalLine: "<<yylineno<<std::endl; std::cout<<"current position: "<<ftell(yyin)<<std::endl;  ProgramLineNum++;  std::cout<<"File Size: "<<SourceFileLength<<std::endl; return(0);}
.     {}  
}

<IN_BLOCK_COMMENT>{
                    "*/"  { BEGIN(INITIAL); std::cout << "leave block comment\n" << std::endl; CommentLineNum++; }
                    [^*\n]+ { std::cout << "BlockLine\n"; }//eat comment in chunks
                    "*"     { std::cout << "\"*\" " << std::endl;}//eat the lone star
                    "\n"    { std::cout <<std::endl;  CommentLineNum++; ProgramLineNum++;}
                  }

<IN_SINGLELINE_COMMENT>{
                        .*$  {  std::cout<<"curretn yyline: "<<yylineno<<std::endl; BEGIN(INITIAL); std::cout<< "SingleLine\n";  std::cout<< "leave single line comment\n"<<std::endl; CommentLineNum++; }//单行注释,包括只有//的情况

                       }

<NOT_COMMENT>{

}   
<IN_FUNCTION>{
                BEGIN(INITIAL);
             }

Ответ - 75 строк вместо 71 строки. Поскольку шаблон patter. * $ Был сопоставлен три раза, а начальное yylineno кажется равным 1, поэтому ответ будет 1 + 71 + 3 = 75. я прав?


person Choris    schedule 31.12.2014    source источник


Ответы (1)


r$, который соответствует строке в конце строки, тоже изменяется yylineno?

No.

Точно так же неверно увеличивать CommentLineNum в правиле <IN_SINGLE_LINE_COMMENT>.*$. Это правило не использует терминатор строки.

person user207421    schedule 01.01.2015
comment
да, поэтому я перехожу в НАЧАЛЬНОЕ состояние, когда это правило соблюдается. - person Choris; 03.01.2015