我尝试将 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.
我认为扫描仪是从哪里开始的,并且默认情况下总是声明它?我错过了什么吗?
感谢您的帮助。
最佳答案
您的 .flex 文件格式错误。正如 JFlex manual 所定义的,您必须像这样组织文件:
UserCode
%%
Options and declarations
%%
Lexical rules
您当前没有用户代码,因此您可以以 %% 开始您的文件,表明该文件立即以选项和声明开始。所以文件的开头看起来像这样:
%%
%class Lexer
%line
%column
关于java - JFlex 错误 : Unexpected character: YYINITIAL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21837308/