我有这个基本的 JFlex 词法分析器:
import java.util.*;
%%
%public
%class TuringLexer
%type Void
%init{
yybegin(YYINITIAL);
%init}
%state COMM, GETALPH, MT, PARSELOOP, PARSELEMS, PARSESYMB, PARSEMT
%{
ArrayList<Character> alf = new ArrayList<Character>();
String crtMach;
String crtLoop;
String crtLoopContent;
String crtLoopContentParam;
String crtContent;
String crtSymb;
%}
//Input = [^\r\n]
SEP = [:space:]*
//COMM =[;.*$]
name = [A-Za-z_]*
tok=[A-Za-z0-9#$@\*]
AL = "alphabet :: "
cont = [^]]*
param =[^)]*
letter = [A-Za-z]
opn = [\[?]
symb = [^\}]+
%%
<COMM> {
"." { /* ignore */ System.out.println("Got into comm state ");}
"\n" {System.out.println("Got out of comm state ");yybegin(YYINITIAL);}
}
<GETALPH> {
{SEP} { /* ignore */ }
{tok} { String str = yytext();
System.out.println("Alphabet -- " + str);
Character c = str.charAt(0);
alf.add(c); }
";" {yybegin(YYINITIAL);}
}
<YYINITIAL> {
"\n" { /* ignore */ System.out.println("Got into YYINITIAL"); }
";" { yybegin(COMM); }
[^] { throw new Error("Illegal character <"+yytext()+">"); }
}
为了清楚起见,代码已被删除,但问题仍然存在,因此在这里更容易识别。
这是输入文件 -> 文件名为simple.mt
这是主要课程:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.FileReader;
public class MainClass {
public static void main(String args[]) throws IOException {
Reader reader = new BufferedReader(new FileReader ("simple.mt"));
reader.read();
TuringLexer tl = new TuringLexer(reader);
tl.yylex();
}
}
当我在 Eclipse(或终端,就此而言)运行项目时,我得到:
Exception in thread "main" java.lang.Error: Illegal character <l>
at TuringLexer.yylex(TuringLexer.java:576)
at MainClass.main(MainClass.java:11)
我不知道错误意味着什么以及如何调试它,jflex 文件中剩下的只是一个小样本,所以错误应该不难弄清楚