gpt4 book ai didi

java - 简单的词法分析java程序

转载 作者:搜寻专家 更新时间:2023-11-01 03:43:27 25 4
gpt4 key购买 nike

我的小项目是一个词法分析程序,我必须在其中提取在任意 .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");
}


}

最佳答案

最简单的方法是使用 JFlex 和正确的 lex 文件定义关键字。一旦你有了它,计算标识符和关键字就很简单了。

关于java - 简单的词法分析java程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8888378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com