我有以下 .flex 文件:
package mini_c;
import java_cup.runtime.*;
import static mini_c.sym.*;
%%
%class Lexer
%unicode /* The characters are unicode */
%cup /* Syntax analyser with cup (Parser part) */
%cupdebug /* ? */
%line /* Decompose by line */
%column /* Decompose by column */
// %yylexthrow Exception /* Can throw exceptions */
%{
/* No need for preamble in JAVA */
%}
WhiteSpace = [ trn]+
Integer = [:digit:]+ // Digit is defined on jflex
Identifier = ([:jletter:] | [_]) ([:jletter:] | [:digit:] | [_] )* // Will be used to read function names
%%
<YYINITIAL> {
"(" { return new Symbol(LPAR, yyline, yycolumn); }
")" { return new Symbol(RPAR, yyline, yycolumn); }
"{"
{ return new Symbol(LB, yyline, yycolumn); }
"}" { return new Symbol(RB, yyline, yycolumn); }
";" { return new Symbol(SEMICOLON, yyline, yycolumn);}
"return"
{ return new Symbol(RETURN, yyline, yycolumn); }
{Integer}
{ return new Symbol(CST, yyline, yycolumn, Integer.parseInt(yytext())); }
{Identifier}
{ return new Symbol(IDENT, yyline, yycolumn, yytext()); }
. { throw new Exception(String.format("Error in line %d, column %d: illegal character '%s'n", yyline, yycolumn, yytexy())); }
}
当我写jflex Lexer.flex
时(Lexer.flex显然是文件的名称)cmd 上显示以下内容:
Reading "Lexer.flex"
Warning : Macro "WhiteSpace" has been declared but never used.
Constructing NFA : 31 states in NFA
Converting NFA to DFA :
................
18 states before minimization, 15 states in minimized DFA
Old file "Lexer.java" saved as "Lexer.java~"
Writing code to "Lexer.java"
Error: Syntax error @ Symbol: ID (unknown:4/15 - unknown:4/20)
Error : Illegal use of reserved word
Warning : Scanner at 6(1): Unrecognized character '%' -- ignored
Warning : Scanner at 6(2): Unrecognized character '%' -- ignored
Warning : Scanner at 8(1): Unrecognized character '%' -- ignored
Error: Syntax error @ Symbol: ID (unknown:8/2 - unknown:8/6)
Warning : Scanner at 9(1): Unrecognized character '%' -- ignored
Warning : Scanner at 10(1): Unrecognized character '%' -- ignored
Warning : Scanner at 11(1): Unrecognized character '%' -- ignored
Warning : Scanner at 12(1): Unrecognized character '%' -- ignored
Warning : Scanner at 13(1): Unrecognized character '%' -- ignored
Warning : Scanner at 16(1): Unrecognized character '%' -- ignored
Warning : Scanner at 16(2): Unrecognized character '{' -- ignored
Warning : Scanner at 18(1): Unrecognized character '%' -- ignored
Warning : Scanner at 18(2): Unrecognized character '}' -- ignored
Warning : Scanner at 20(12): Unrecognized character '=' -- ignored
Warning : Scanner at 20(16): Unrecognized character '' -- ignored
cmd错误实际上更长,但我想你明白了。
尝试在"简单示例"部分下进行 http://jflex.de/manual.html#Example 时。我复制粘贴示例,出现以下错误:
Reading "Lexer.flex"
Error in file "Lexer.flex" (line 87):
This seems not to be a lexical specification (first %% is missing)
yytext()+">"); }
1 error, 0 warnings.
Warning : Scanner at 7(5): Unrecognized character '%' -- ignored
Warning : Scanner at 7(6): Unrecognized character '%' -- ignored
Warning : Scanner at 9(5): Unrecognized character '%' -- ignored
Error: Syntax error @ Symbol: ID (unknown:9/6 - unknown:9/10)
Warning : Scanner at 10(5): Unrecognized character '%' -- ignored
Warning : Scanner at 11(5): Unrecognized character '%' -- ignored
谢谢!
解决了!我的 jflex.bat 文件有问题。有效的好方法有以下行:
java -Xmx128m -jar %JFLEX_HOME%libjflex-1.6.1.jar %1 %2 %3 %4 %5 %6 %7 %8 %9
我在那里有一个拼写错误,它是jflex-1.6.jar