1

I have today a problem with my flex/bison script. It doesn't detect the multiline comment.

%x COMMENT_MULTI
#\[                     yy_push_state(COMMENT_MULTI);

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

Can you help me?

ethrbunny
  • 10,379
  • 9
  • 69
  • 131
izanagi_1995
  • 94
  • 1
  • 8
  • 2
    You need a `%%` tag to separate the definitions section from the rules section. When I add that and `%option stack`, your scanner works just fine for me... – Chris Dodd Jan 13 '13 at 20:55

2 Answers2

2

This is the solution I ended up using in my bison definition of C++ comments and block comments:

%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"                     ;
Rovanion
  • 4,382
  • 3
  • 29
  • 49
-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;
}  
Andrade
  • 1,179
  • 12
  • 15
  • This "answer" is not in english and does not provide any explanation for what the code in question actually does. – Rovanion Dec 08 '14 at 22:28
  • The answer was already partially translated and I thought that the code was enough to enable its understanding. Now I translated its comments and it should help even more people. @Rovanion, I'm sorry if you didn't understand before, but the translated comments should help now – Andrade Apr 15 '15 at 18:42