verify if each method started with a comment

87 views Asked by At

I want to verify if each method MyFunction() started with a comment. So I generated a parser by Javacc via the production rules.

extrait of jj file :

// here the comments of MyFunction()
  void  MyFunction : {}
{
<begin> <id> "(" (Argument ())* ")" {}
(Statement ()) *
<end>
}

void Argument:{}
{
<STRING> id
<STRING> id
}

void statement () {}
{
..........
}

how I can use regular expression to verifiy if existing comments before declaration of MyFunction() in program source in input file stream.

1

There are 1 answers

3
Theodore Norvell On

What you could do is treat comments as special tokens. Special tokens are not passed on to the parser, but they are accessible from the next regular token.

Suppose comments are special tokens, spaces are skipped, and your input is

"/*Floyd*/ /*Ummagumma*/ begin pink() end"

The each token has a .next pointer (to the right in the picture) and a .specialToken pointer (down in the picture). (null pointers not shown.)

begin  -->  id    --->   (     -->    )  -->  end ---> EOF 
  |
  V
comment("Ummagumma") 
  |
  V
comment("Floyd")

So what you can do is look at the first token of your method. Was it preceded by a special token? And, if so, was that special token a comment?

SKIP : { " ", "\n", "\r" }
MORE: { "/*" : IN_COMMENT }
<IN_COMMENT> MORE { ~[] }
<IN_COMMENT> SPECIAL_TOKEN{ <COMMENT: "*/"> }
.
.
.
void  FunctionDefinition : {
    Token firstToken, id;}
{
    firstToken=<begin> id=<id> "(" (Argument ())* ")"
    (Statement ()) *
    <end>
    { if( firstToken.specialToken == null
       || firstToken.specialToken.kind != COMMENT ) 
           System.out.println("Function " +id.image+
                              " is not preceded by a comment!" ) ;
    }
}