简单词法分析java程序

简单词法分析java程序,java,lexical-analysis,lookup-tables,Java,Lexical Analysis,Lookup Tables,我的小项目是一个词法分析程序,在这个程序中,我必须提取在任意.java文件中找到的每个单词,并列出它在文件中出现的每一行。我需要一个查找表专门用于保留字,另一个查找表用于文档中找到的所有附加字。对于这样的程序: public class xxxx { int xyz; xyz = 0; } 输出应为: Reserved words: class: 1 int: 2 public: 1 Other words: xxxx: 1 xyz: 2, 3 但是我目前的程序有很

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


}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.List;
导入java.util.Map;
导入java.util.Scanner;
公共类词汇分析{
私有字符串[]关键字={“抽象”、“布尔”、“字节”、“大小写”,
“catch”、“char”、“class”、“continue”、“default”、“do”、“double”,
“else”、“extends”、“final”、“finally”、“float”、“for”、“if”,
“implements”、“import”、“instanceof”、“int”、“interface”、“long”,
“本地”、“新”、“包”、“私有”、“受保护”、“公共”,
“返回”、“短”、“静态”、“超级”、“切换”、“同步”,
“此”、“抛出”、“抛出”、“暂时”、“尝试”、“无效”、“易变”,
“while”、“false”、“true”、“null”};
HashMap关键字稳定;
HashMap otherWords=新的HashMap();
公共词典分析(字符串文件名){
扫描器kb=null;
int lineNumber=0;
试一试{
kb=新扫描仪(新文件(文件名));
}catch(filenotfounde异常){
e、 printStackTrace();
}
关键字稳定=新HashMap();
对于(int i=0;i<47;i++){
关键字stable.put(关键字[i],新ArrayList());
}
while(kb.hasNextLine()){
lineNumber++;
String line=kb.nextLine();
String[]lineparts=line.split(“\\s+\\.+\\\.+\\.+\\.+\\.+\\.+\.+\\”+\\\\\\“+\\\\:+\\\[+\\\]+”;
用于(字符串x:线零件){
ArrayList list=keywordsTable.get(x);
if(list==null){
list=otherWords.get(x);
if(list==null){
ArrayList temp=新的ArrayList();
临时添加(行号);
换句话说,put(x,temp);
}否则{
移除(x);
ArrayList temp=新的ArrayList();
临时添加(行号);
换句话说,put(x,temp);
}
}否则{
关键词稳定。删除(x);
ArrayList temp=新的ArrayList();
临时添加(行号);
关键词稳定输入(x,温度);
}
}
}
System.out.println(“关键字:”);
printMap(关键字稳定);
System.out.println();
System.out.println(“其他词:”);
印刷地图(其他文字);
}
公共静态无效打印映射(映射mp){
迭代器it=mp.entrySet().Iterator();
而(it.hasNext()){
Map.Entry pairs=(Map.Entry)it.next();
System.out.print(pairs.getKey()+“=”);
打印列表(pairs.getValue());
System.out.println();
it.remove();
}
}
公共静态无效打印列表(列表x){
用于(对象m:x){
系统输出打印(m+“,”);
}
}
公共静态void main(字符串参数[]){
新词汇分析(“lexitest.txt”);
}
}

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

我发现了一个我认为可以修复所有问题的错误。你需要在主目录中声明你正在恢复的文件的目录。例如,你现在拥有的是新的LexicalAnalysis(“lexitest.txt”)


例如,我正在使用我的flashdrive,因此它将是新的LexicalAnalysis(“F”\lexitest.txt”);

问题出在哪里?请给出一个例子如果你在上面的例子中运行它,它也会处理空格和括号,而且它不会添加单词出现的每个位置,因此如果null出现8次,它只会显示一次=(看起来我需要一个正则表达式,它只获取没有发音字符的单词,所以它会忽略所有带有标点符号的单词。