我正在尝试将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} | {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()+”>”); }
(现在还没有100%完成,我只是想看看我是否有任何错误)
无论如何,当我尝试使用JFlex时,它会给我以下错误:
Reading "lexer2.flex"
Error in file "lexer2.flex" (line 35):
Unexpected character
<YYINITIAL> {
^
1 error, 0 warnings.
我以为是扫描仪启动的地方,默认情况下总是声明?我错过了什么吗?
感谢您的帮助。
答案 0 :(得分:1)
您的.flex文件格式错误。根据{{3}}的定义,您必须按如下方式组织文件:
UserCode
%%
Options and declarations
%%
Lexical rules
您目前没有UserCode,因此您可以使用%%启动文件,指示文件立即启动时使用选项和声明。所以文件的开头看起来像这样:
%%
%class Lexer
%line
%column