1.當運行程序時,程序會讀取項目下的program.txt文件
2. 程序將會逐行讀取program.txt中的源程序,進行詞法分析,并將分析的結果輸出。
3. 如果發現錯誤,程序將會中止讀取文件進行分析,并輸出錯誤提示
所用單詞的構詞規則
(1) 關鍵字:begin end while do if then
(2) 運算符和界符::= + - * / < <= > >= == != ; ( ) #
(3) 標識符(ID)和常數(NUM) ID=letter(letter | digit) NUM=digit digit**
測試樣例
begin
i:=5+i*(i/3-i);
i:=i+i;
end
#
詞法分析
public class analyzer {final static String ID = "\\p{Alpha}(\\p{Alpha}|\\d)*";/** 整形常數 NUM >> 正則表達式*/final static String NUM = "\\d\\d*";/** token 詞法單元* <詞符號, 種別碼> *//** 關鍵字 token*/static Map<String, Integer> TOKEN_KEYWORDS;/** 運算符/界符 token */static Map<String, Integer> TOKEN_OPERATOR_BOUNDARY;/** 其他單詞 token*/static Map<String, Integer> TOKEN_ID_SUM;/** 文件根目錄*/static final String ROOT_DIRECTORY = "program.txt";/*** 初始化 token 單元*/private static void initToken(){//種別碼創建TOKEN_KEYWORDS = new HashMap<String, Integer>(){//關鍵字{put("begin", 1);put("if", 2);put("then", 3);put("while", 4);put("do", 5);put("end", 6);}};TOKEN_OPERATOR_BOUNDARY= new HashMap<String, Integer>(){//運算符和界符{put("+", 13);put("-", 14);put("*", 15);put("/", 16);put(":", 17);put(":=", 18);put("<", 20);put("<>", 21);put("<=", 22);put(">", 23);put(">=", 24);put("=", 25);put(";", 26);put("(", 27);put(")", 28);put("#", 0);}};TOKEN_ID_SUM= new HashMap<String, Integer>(){//標識符和整型常數{put(ID, 10);put(NUM, 11);}};}/*** 讀 源程序 文件*/public static void ReadFile1() {FileInputStream fis = null;InputStreamReader isr = null;BufferedReader br = null;try {fis = new FileInputStream(ROOT_DIRECTORY);isr = new InputStreamReader(fis, "UTF-8"); // 轉化類br = new BufferedReader(isr); // 裝飾類String line;/** 記錄 程序 行數 */int countLine = 1;while ((line = br.readLine()) != null) { // 每次讀取一行,分析一行boolean answer = lexicalAnalysis(line);if(answer == false){System.out.printf("ERROR 編譯錯誤=== 第 %d 行出現 詞法錯誤 \n", countLine);break;}countLine++;}System.out.printf("===編譯完成===");} catch (Exception ex) {ex.printStackTrace();} finally {try {br.close(); // 關閉最后一個類,會將所有的底層流都關閉} catch (Exception ex) {ex.printStackTrace();}}}/** 判斷key是否是其他單詞*/private static boolean isIDOrSUM(String key){if (key.matches(ID) ) {System.out.printf("(%d, %s)\n", TOKEN_ID_SUM.get(ID), key);}else if (key.matches(NUM)) {System.out.printf("(%d, %s)\n", TOKEN_ID_SUM.get(NUM), key);}else {return false;}return true;}/*** 進行 詞法分析* @param word 要分析的字符串* @return 結果*/public static boolean lexicalAnalysis(String word){word = word.trim(); // 去首尾空格String[] strings = word.split("\\p{Space}+"); // 分割字符串,保證處理的字符串沒有空格for (String string : strings) {/** 3種情況:* 1. 關鍵字 == end (關鍵字的后面一定是空格 )* 2. 運算符/ 分界符 == continue* 3. 其他單詞 == continue*/String key = "";for (int i = 0; i < string.length(); i++){String indexChar = String.valueOf(string.charAt(i)) ;if(i+1<string.length()){if((indexChar+string.charAt(i+1)).equals("//"))return true;}/** 是 運算符 或者 關鍵字*/if (TOKEN_OPERATOR_BOUNDARY.containsKey(indexChar) ||TOKEN_KEYWORDS.containsKey(string.substring(i, string.length()))){if (key.length() > 0) {if (isIDOrSUM(key) == false) {/** 詞法錯誤 */return false;}key = "";}if(TOKEN_OPERATOR_BOUNDARY.containsKey(indexChar)) {/** 1. 是 運算符/分界符 */key += indexChar;if(i + 1 < string.length() && TOKEN_OPERATOR_BOUNDARY.containsKey(indexChar + string.charAt(i+1))){ // 運算分界符key += string.charAt(++i);}System.out.printf("(%d, %s)\n",TOKEN_OPERATOR_BOUNDARY.get(key),key);key = "";}else if(TOKEN_KEYWORDS.containsKey(key = string.substring(i, string.length()))) {/** 2. 是關鍵字*/System.out.printf("(%d, %s)\n",TOKEN_KEYWORDS.get(key),key);key = "";break;}}else {/** 是其他單詞*/key += indexChar;/** 其他單詞后面是 1. 換行,2. 運算符/界符 3. 其他單詞*/if(i+1 >= string.length()){if (isIDOrSUM(key) == false) {/** 詞法錯誤 */return false;}}}}}return true;}public static void main(String[] args) {initToken();System.out.println("==詞法分析程序==");System.out.println("從文件中讀取程序");System.out.println("==============");ReadFile1();System.out.println();}}