2013-01-13 62 views
1

今天我的flex/bison腳本出現了問題。它不檢測多行註釋。flex/bison多行註釋聲明

%x COMMENT_MULTI 
#\[      yy_push_state(COMMENT_MULTI); 

<COMMENT_MULTI>"]#"  yy_pop_state(); 
<COMMENT_MULTI>"\n"  { 
          yylloc->lines(yyleng); 
          yylloc->step(); 
         } 
<COMMENT_MULTI>.? 

你能幫我嗎?

+2

你需要一個'%%'標籤來分隔規則部分定義部分。當我添加了'%option stack'時,你的掃描器對我來說工作得很好...... –

回答

-2
/*DECLARING A SPECIFIC STATE FOR THE BLOCK COMMENT*/ 
%x BLOCK_COMMENT 
%% 

\/\* { // BEGINING OF A BLOCK COMMENT: ENTERS INTO 'BLOCK_COMMENT' STATE 
    BEGIN(BLOCK_COMMENT); 
    blockcomment_line_start = line; 
    blockcomment_col_start = frcol; 
    strncat(block_comment, yytext, sizeof(block_comment)); 
    } 
<BLOCK_COMMENT>\*\/ { // END OF BLOCK COMMENT 
    if(strlen(block_comment)+strlen(yytext) >= BLOCK_COMMENT_BUFFER-10){ //ADDS "(...)" AT THE END OF THE STRING IN CASE IT HAS BEEN TRUNCATED 
     block_comment[1013] = '\0'; 
     strcat(block_comment," (...) "); 
    } 
    strncat(block_comment, yytext, sizeof(block_comment)); 
    printf("@(%.3d,%.3d)\tBLOCK COMMENT\t- %s\n", blockcomment_line_start, blockcomment_col_start, block_comment); 

    BEGIN(INITIAL); //COMES BACK TO THE INITIAL STATE 
    } 
<BLOCK_COMMENT>\n { 
    line++; // STILL HAS TO INCREMENT THE LINE NUMBER WHEN THERE'S A LINE BREAK INSIDE THE COMMENT 
    //strcat(block_comment, "\\n"); 
    strncat(block_comment, "\\n", sizeof(block_comment)); 
} 
<BLOCK_COMMENT>. { // IGNORE ALL OTHER CHARACTERS WHILE IN 'BLOCK_COMMENT' STATE 
    //strcat(block_comment, yytext); 
    strncat(block_comment, yytext, sizeof(block_comment)); 
} 
<BLOCK_COMMENT><<EOF>> {  
    printf("ERROR! THE BLOCK COMMENT OPENED IN @(%d,%d) HASN'T BEEN CLOSED! \t\n", blockcomment_line_start, blockcomment_col_start); 
    return; 
} 
+0

這個「答案」不是英文,並且沒有提供任何有關代碼實際上做的解釋。 – Rovanion

+0

答案已經部分翻譯,我認爲代碼足以讓其理解。現在我翻譯了它的評論,它應該可以幫助更多的人。 @Rovanion,如果你以前不明白,我很抱歉,但翻譯的評論現在應該有所幫助 – Andrade

2

這是我最後使用的C++評論我的野牛定義和塊註釋的解決方案:

%x COMMENT 

"//".*\n       ;                                         

"/*"        BEGIN(COMMENT);                                     
<COMMENT>"/*"      printf("%s%d\n", "Warning: multiple comments opened at line: ", yylineno);                       
<COMMENT>"*/"      BEGIN(INITIAL);                                     
<COMMENT>"EOF"      yyerror("Reached end of file while scanning comment");                            
<COMMENT>.|"\n"      ;