首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单词法分析java程序

简单词法分析java程序
EN

Stack Overflow用户
提问于 2012-01-17 08:49:37
回答 2查看 16.8K关注 0票数 4

我的小项目是一个词法分析程序,在这个程序中,我必须获取在任意.java文件中找到的每个单词,并列出它在文件中出现的每一行。我需要有一个查询表专门为保留字和另一个在文档中找到的所有额外的字。所以对于像这样的程序:

代码语言:javascript
复制
    public class xxxx {
    int xyz;
    xyz = 0;
}

输出应为:

代码语言:javascript
复制
Reserved words:
class: 1
int: 2
public: 1

Other words:
xxxx: 1
xyz: 2, 3

但我目前的程序有很多问题,所以我不知道发生了什么,所以欢迎对我的程序进行修改或完全重写。我只是想把掌握java语言作为一种爱好,所以只要我能理解发生了什么,所有的帮助都是欢迎的。我确信这个问题有一个简单的解决方案,但我的尝试没有奏效:(感谢您的帮助^^

代码语言:javascript
复制
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");
    }


}
EN

回答 2

Stack Overflow用户

发布于 2012-01-18 21:20:14

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

票数 1
EN

Stack Overflow用户

发布于 2015-01-15 06:03:03

我发现了一个bug,我认为它解决了所有问题。您需要在主目录中说明要恢复的文件的目录。例如,您现在拥有的是新的LexicalAnalysis("lexitest.txt");

在我的例子中,我使用了我的闪存驱动器,所以它将是新的LexicalAnalysis("F"\lexitest.txt");

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8888378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档