我的小项目是一个词法分析程序,我必须在任意.java文件中找到每个单词并列出它在文件中出现的每一行。我需要有一个专用于保留字的查找表和另一个用于文档中找到的所有附加单词的表。所以对于像这样的程序:
public class xxxx {
int xyz;
xyz = 0;
}
输出应为:
Reserved words:
class: 1
int: 2
public: 1
Other words:
xxxx: 1
xyz: 2, 3
但是我当前的程序存在很多问题,所以我不知道最近会发生什么,所以我的程序修改或完全重写是值得欢迎的。我只是试图把java语言作为一种爱好,所以只要我能理解到底是什么,所有的帮助都是受欢迎的。我确信这个问题有一个简单的解决方案,但我的尝试没有用:(感谢您的帮助^^
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class LexicalAnalysis {
private String[] keywords = { "abstract", "boolean", "byte", "case",
"catch", "char", "class", "continue", "default", "do", "double",
"else", "extends", "final", "finally", "float", "for", "if",
"implements", "import", "instanceof", "int", "interface", "long",
"native", "new", "package", "private", "protected", "public",
"return", "short", "static", "super", "switch", "synchronized",
"this", "throw", "throws", "transient", "try", "void", "volatile",
"while", "false", "true", "null" };
HashMap<String, ArrayList<Integer>> keywordsTable;
HashMap<String, ArrayList<Integer>> otherWords = new HashMap<String, ArrayList<Integer>>();
public LexicalAnalysis(String fileName){
Scanner kb = null;
int lineNumber = 0;
try {
kb = new Scanner(new File(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
keywordsTable = new HashMap<String, ArrayList<Integer>>();
for(int i = 0; i < 47; i++){
keywordsTable.put(keywords[i], new ArrayList<Integer>());
}
while(kb.hasNextLine()){
lineNumber++;
String line = kb.nextLine();
String[] lineparts = line.split("\\s+|\\.+|\\;+|\\(+|\\)+|\\\"+|\\:+|\\[+|\\]+");
for(String x: lineparts){
ArrayList<Integer> list = keywordsTable.get(x);
if(list == null){
list = otherWords.get(x);
if(list == null){
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(lineNumber);
otherWords.put(x,temp);
}else{
otherWords.remove(x);
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(lineNumber);
otherWords.put(x, temp);
}
}else{
keywordsTable.remove(x);
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(lineNumber);
keywordsTable.put(x, temp);
}
}
}
System.out.println("Keywords:");
printMap(keywordsTable);
System.out.println();
System.out.println("Other Words:");
printMap(otherWords);
}
public static void printMap(Map<String, ArrayList<Integer>> mp) {
Iterator<Map.Entry<String, ArrayList<Integer>>> it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, ArrayList<Integer>> pairs = (Map.Entry<String, ArrayList<Integer>>)it.next();
System.out.print(pairs.getKey() + " = ");
printList(pairs.getValue());
System.out.println();
it.remove();
}
}
public static void printList(List x){
for(Object m : x){
System.out.print(m + ", ");
}
}
public static void main(String args[]){
new LexicalAnalysis("lexitest.txt");
}
}
答案 0 :(得分:1)
最简单的方法是使用JFlex和正确的lex文件定义关键字。一旦你有了这个,计算标识符和关键字是微不足道的。
答案 1 :(得分:0)
我发现了一个我认为修复过的错误。您需要在main中声明要恢复的文件的目录。例如,你现在拥有的是新的LexicalAnalysis(&#34; lexitest.txt&#34;);
对于我的例子,我使用我的flashdrive,因此它将是新的LexicalAnalysis(&#34; F&#34; \ lexitest.txt&#34;);