Search code examples
javacc

verify if each function started with a comment and verify if this comments contains reserved words.comments contains reserved words


I checked if each function Function() started with a comments in input file stream. this like :

     SKIP : {   " " | "\t" | "\n" | "\r" }

            /* COMMENTS */ 

SPECIAL_TOKEN : {   <SINGLE_LINE_COMMENT: "--" (~["\n","\r"])* ("\n"|"\r"|"\r\n")?> } 


     void  Function : {
                Token firstToken, id;} {
                firstToken=<start> id=<id> "("  ")"
                .........

                <end>
                { if( firstToken.specialToken == null
                   || firstToken.specialToken.kind != COMMENT ) 
                       System.out.println("Function " +id.image+
                                          " is not preceded by a comment!" ) ;
                } }

so, I want to verify if this comments contains reserved words.

Thank you in advance.


Solution

  • If you just want to know if a comment contains given word, you might as well use Java's string search mechanism. Suppose you want to know whether the word "bandersnatch" in the comment that precedes the function definition.

       void  Function() : {
            Token firstToken, id;}
       {
            firstToken=<start> id=<id> "("  ")"
            .........
            <end>
            { if( firstToken.specialToken == null
               || firstToken.specialToken.kind != COMMENT ) 
                   System.out.println("Function " +id.image+
                                      " is not preceded by a comment!" ) ;
              else {
                  String comment = firstToken.specialToken.image ;
                  boolean hasBandersnatch = comment.indexOf("bandersnatch") != -1 ;                   if( ! hasBandersnatch ) 
                      System.out.println("Function " +id.image+
                                      " is preceded by a comment that does not contain 'bandersnatch'!" ) ; }
            }
       }
    

    If you wan the search to be case insensitive change the initialization of comment.

    String comment = firstToken.specialToken.image.toLower() ;