我正在尝试使用JFlex识别多行注释。它适用于行尾注释,但我在类型/*Comment...*/
的注释中出错。我使用状态来识别这种类型的评论,如下所示
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");}
[^] {}
对于此输入,我收到错误
public class MyClass
{
//A comment
//Another comment
/*This is a comment*/
}
/*This is a comment*/
Error in line 5, column : Syntax error #0
其中#0是EOF符号
为什么我会获得EOF符号?
由于
答案 0 :(得分:1)
当您到达评论的末尾时,您需要使用yybegin(INITIAL);