javacompiler-constructioncommentseofjflex

Multiline comment in JFlex - EOF error


I'm trying to recognize multiline comments using JFlex. It works well with end-of-line comments but I get an error with the comments of the type /*Comment...*/. I am using states to recognize this type of comment, as follows

import java_cup.runtime.*;

%%

%public
%class Scanner
%unicode
%cup
%line
%column

%{

    private Symbol symbol(int type)
    {
        return new Symbol(type, yyline, yycolumn);
    }

    private Symbol symbol(int type, Object value)
    {
        return new Symbol(type, yyline, yycolumn, value);
    }
%}

LineTerminator = \r|\n|\r\n

Whitespace = {LineTerminator} | [ \t\f]

Identifier = [A-Za-z_][A-Za-z_0-9]*

Library = [A-Za-z_][A-Za-z_0-8]* [.][[.][A-Za-z_][A-Za-z_0-8]*]* [[A-Za-z_][A-Za-z_0-8]*| [*]]

Number = [0] | [0-9]+

EndLineComment = "//" [^\r\n]* {LineTerminator}?

CadenaCaracteres = [^\r\n\"\\]

%state COMMENT

%%

<YYINITIAL> "import"                 {System.out.print(" import "); return symbol(sym.IMPORT, yytext());}

<YYINITIAL> "public"                 {System.out.print(" public "); return symbol(sym.PUBLIC, yytext());}
<YYINITIAL> "private"                {System.out.print(" private "); return symbol(sym.PRIVATE, yytext());}
<YYINITIAL> "protected"              {System.out.print(" protected "); return symbol(sym.PROTECTED, yytext());}

<YYINITIAL> "final"                  {System.out.print(" final "); return symbol(sym.FINAL, yytext());}

<YYINITIAL> "class"                  {System.out.print(" class "); return symbol(sym.CLASS, yytext());}

<YYINITIAL> "int"                    {System.out.print(" int "); return symbol(sym.INT, yytext());}
<YYINITIAL> "boolean"                {System.out.print(" boolean "); return symbol(sym.BOOLEAN, yytext());}
<YYINITIAL> "String"                 {System.out.print(" String "); return symbol(sym.STRING, yytext());}
<YYINITIAL> "char"                   {System.out.print(" char "); return symbol(sym.CHAR, yytext());}
<YYINITIAL> "double"                 {System.out.print(" double "); return symbol(sym.DOUBLE, yytext());}
<YYINITIAL> "Object"                 {System.out.print(" Object "); return symbol(sym.OBJECT, yytext());}

<YYINITIAL> "void"                   {System.out.print(" void "); return symbol(sym.VOID, yytext());}

<YYINITIAL> "new"                    {System.out.print(" new "); return symbol(sym.NEW, yytext());}
<YYINITIAL> "static"                 {System.out.print(" static "); return symbol(sym.STATIC, yytext());}
<YYINITIAL> "args"                   {System.out.print(" args "); return symbol(sym.ARGS, yytext());}
<YYINITIAL> "main"                   {System.out.print(" main "); return symbol(sym.MAIN, yytext());}

<YYINITIAL> "{"                      {System.out.print(" { "); return symbol(sym.LBRACK, yytext());}
<YYINITIAL> "}"                      {System.out.print(" } "); return symbol(sym.RBRACK, yytext());}
<YYINITIAL> "("                      {System.out.print(" ( "); return symbol(sym.LPAREN, yytext());}
<YYINITIAL> ")"                      {System.out.print(" ) "); return symbol(sym.LPAREN, yytext());}


<YYINITIAL> ","                      {System.out.print(" , "); return symbol(sym.COL, yytext());}
<YYINITIAL> ";"                      {System.out.print(" ; "); return symbol(sym.SEMI, yytext());}

<YYINITIAL> "/*"                     {yybegin(COMMENT);}

<YYINITIAL> {Identifier}            {System.out.print(yytext()); return symbol(sym.ID, yytext());}
<YYINITIAL> {Library}               {System.out.print(yytext()); return symbol(sym.Library, yytext());}
<YYINITIAL> {Number}                {System.out.print(yytext()); return symbol(sym.Number, yytext());}
<YYINITIAL> {EndLineComment}        {System.out.println(yytext();}
<YYINITIAL> {Whitespace}            {}

<COMMENT> "*"                       {}
<COMMENT> [^"*/"]                   {}
<COMMENT> "*/"                    {System.out.println("Ignored comment");}



[^]                                  {}

For this input I get the error

public class MyClass
{
   //A comment
   //Another comment
   /*This is a comment*/
}

/*This is a comment*/

Error in line 5, column  : Syntax error #0

Where # 0 is the EOF symbol

Why do I get EOF symbol?

Thanks


Solution

  • When you reach the end of the comment, you need to switch back to the initial state with yybegin(INITIAL);