Java JFlex错误:意外字符:YYINITIAL
我正在尝试将JFlex与以下输入文件一起使用:Java JFlex错误:意外字符:YYINITIAL,java,lexer,jflex,Java,Lexer,Jflex,我正在尝试将JFlex与以下输入文件一起使用: %class Lexer %line %column %init{ yybegin(YYINITIAL); %init} %{ Copied directly to Java file. %} delim = \r|\n|\r\n not_newline = . whitespace = {delim} | [ \t\n\r] any = {not_newline} | {delim}
%class Lexer
%line
%column
%init{
yybegin(YYINITIAL);
%init}
%{
Copied directly to Java file.
%}
delim = \r|\n|\r\n
not_newline = .
whitespace = {delim} | [ \t\n\r]
any = {not_newline} | {delim} | {quote}
upp_letter = [A-Z]
low_letter = [a-z]
digit = [0-9]
quote = [\”]
backslash = [\\]
escape = {backslash}{any}
LPAR = [(]
RPAR = [)]
COMMA = [,]
letter = {upp_letter} | {low_letter}
ID = {letter}({letter}|{digit})*
INT = {digit}+
STRING = {quote}({letter} | {digit} | {escape})*{quote}
%%
<YYINITIAL> {
{ID} { return ID }
{INT} { return INT }
{LPAR} { return symbol(sym.LPAR); }
{RPAR} { return symbol(sym.RPAR); }
{COMMA} { return symbol(sum.COMMA); }
{STRING} { return STRING }
{whitespace} {}
}
[^] { throw new Error(“Illegal character <“+yytext()+”>”); }
%class Lexer
%线
%纵队
%初始化{
yybegin(YY首字母);
%init}
%{
直接复制到Java文件。
%}
delim=\r |\n |\r\n
不是新行=。
空白={delim}|[\t\n\r]
any={not_newline}{delim}{quote}
upp_字母=[A-Z]
低字母=[a-z]
数字=[0-9]
quote=[\“]
反斜杠=[\\]
转义={反斜杠}{任何}
LPAR=[(]
RPAR=[)]
逗号=[,]
字母={upp_字母}{low_字母}
ID={letter}({letter}{digit})*
INT={digit}+
字符串={quote}({letter}{digit}}{escape})*{quote}
%%
{
{ID}{return ID}
{INT}{return INT}
{LPAR}{返回符号(sym.LPAR);}
{RPAR}{返回符号(sym.RPAR);}
{COMMA}{返回符号(sum.COMMA);}
{STRING}{return STRING}
{空白}{}
}
[^]{抛出新错误(“非法字符”);}
(它还没有100%完成,我只是想看看我是否有任何错误)
无论如何,当我尝试使用JFlex时,它给了我以下错误:
Reading "lexer2.flex"
Error in file "lexer2.flex" (line 35):
Unexpected character
<YYINITIAL> {
^
1 error, 0 warnings.
阅读“lexer2.flex”
文件“lexer2.flex”中出错(第35行):
意外字符
{
^
1个错误,0个警告。
我以为扫描器是从哪里开始的,它总是默认声明的?我是不是遗漏了什么
感谢您的帮助。您的.flex文件格式错误。根据的定义,您必须按以下方式组织文件:
UserCode
%%
Options and declarations
%%
Lexical rules
%%
%class Lexer
%line
%column
您当前没有用户代码,因此您将以%%开始文件,这表示文件立即开始,并带有选项和声明。因此文件的开头如下所示:
UserCode
%%
Options and declarations
%%
Lexical rules
%%
%class Lexer
%line
%column