我試圖創建一個詞法分析器使用Java德爾福。這裏的示例代碼:爪哇 - 匹配重讀單詞
String[] keywords={"array","as","asm","begin","case","class","const","constructor","destructor","dispinterface","div","do","downto","else","end","except","exports","file","finalization","finally","for","function","goto","if","implementation","inherited","initialization","inline","interface","is","label","library","mod","nil","object","of","out","packed","procedure","program","property","raise","record","repeat","resourcestring","set","shl","shr","string","then","threadvar","to","try","type","unit","until","uses","var","while","with"};
String[] relation={"=","<>","<",">","<=",">="};
String[] logical={"and","not","or","xor"};
Matcher matcher = null;
for(int i=0;i<keywords.length;i++){
matcher=Pattern.compile(keywords[i]).matcher(line);
if(matcher.find()){
System.out.println("Keyword"+"\t\t"+matcher.group());
}
}
for(int i1=0;i1<logical.length;i1++){
matcher=Pattern.compile(logical[i1]).matcher(line);
if(matcher.find()){
System.out.println("logic_op"+"\t\t"+matcher.group());
}
}
for(int i2=0;i2<relation.length;i2++){
matcher=Pattern.compile(relation[i2]).matcher(line);
if(matcher.find()){
System.out.println("relational_op"+"\t\t"+matcher.group());
}
}
所以,當我運行程序,它的工作原理,但它重新閱讀該程序認爲是2令牌例如某些話說:記錄是一個關鍵字,但重新讀取當您令牌邏輯運算符是從REC「或」 d字或。我怎樣才能取消重新閱讀文字?謝謝!
謝謝!我發現它讀取了某些組合符號,如你所說的那樣,其中'> ='將會分成3個邏輯符號。這也幫助了我。謝謝! – quSci