2014-12-31 2 views
0

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


К примеру, у меня есть исходный файл, который 71 строк в общем /* Автор: guanwanxian Дата: 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; 
} 

И мой файл flex:

%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. я прав?

ответ

1

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

No.

Smilarly, неверно приращение CommentLineNum в правиле <IN_SINGLE_LINE_COMMENT>.*$. Это правило не использует терминатор линии.

+0

да, поэтому я перехожу в состояние INITIAL, когда это правило сопоставляется. – Choris