加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

简单的词法分析java程序

发布时间:2020-12-15 02:30:49 所属栏目:Java 来源:网络整理
导读:我的小项目是一个词法分析程序,我必须在任意. java文件中找到每个单词并列出它在文件中出现的每一行.我需要有一个专用于保留字的查找表和另一个用于文档中找到的所有附加单词的表.所以对于像这样的程序: public class xxxx { int xyz; xyz = 0;} 输出应该是
我的小项目是一个词法分析程序,我必须在任意. 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文件定义关键字.一旦你有了,计算标识符和关键字是微不足道的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读