實驗1 根據狀態轉換圖手工構造詞法掃描器
一、實驗目的
1. 理解詞法分析器的基本功能
2. 理解詞法規則的描述方法
3. 理解狀態轉換圖及其實現
4. 能夠編寫簡單的詞法分析器
二、實驗平台
任選
三、實驗內容
編制一個讀單詞過程,源程序為一個文件,讀取該文件,識別出各個具有獨立意義的單詞,即基本保留字、標識符、常數、運算符、界符五大類。並依次輸出各個單詞的內部編碼及單詞符號自身值。
單詞的內部編碼如下:
1、保留字:if、int、for、while、do、return、break、continue;單詞種別碼為1;
2、標識符:除保留字外的以字母開頭,后跟字母、數字的字符序列;單詞種別碼為2;
3、常數為無符號整形數;單詞種別碼為3;
4、運算符包括:+、-、*、/、=;單詞種別碼為4;
5、分隔符包括:,、;、{、}、(、); 單詞種別碼為5。
源代碼:
/*** * 信 1605-3 20163432 張運濤 * 編制一個讀單詞過程,源程序為一個文件,讀取該文件,識別出各個具有獨立意義的單詞, * 即基本保留字、標識符、常數、運算符、界符五大類。並依次輸出各個單詞的內部編碼及單詞符號自身值。 */ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Word { static Scanner sc = new Scanner(System.in); // 保存關鍵字 private static List<String> KeyWords; // 保存操作符 private static List<String> Operators; // 保存界符 private static List<String> Boundarys; private static List<String> Spaces; static String str=""; public static String readFileByChars(String fileName) { File file = new File("源程序.txt"); Reader reader = null; try { //System.out.println("以字符為單位讀取文件內容,一次讀多個字符:"); // 一次讀多個字符 char[] tempchars = new char[300]; int charread = 0; reader = new InputStreamReader(new FileInputStream("源程序.txt")); // 讀入多個字符到字符數組中,charread為一次讀取字符數 while ((charread = reader.read(tempchars)) != -1) { // 同樣屏蔽掉r不顯示 if ((charread == tempchars.length) && (tempchars[tempchars.length - 1] != '~')) { System.out.print(tempchars+"1"); } else { for (int i = 0; i < charread; i++) { if (tempchars[i] == '~') { continue; } else { str+=tempchars[i]; //System.out.print(tempchars[i]); } } //System.out.println("str="+str); } } } catch (Exception e1) { e1.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return str; } // 初始化 static { // 關鍵字數組 --> 關鍵字列表 String[] keywordArr = { "public", "private", "protected", "short", "int", "long", "char", "float", "double", "boolean", "static", "void", "for","while" ,"return","continue"}; KeyWords = Arrays.asList(keywordArr); // 操作符數組 --> 操作符列表 String[] operatorArr = { "+", "-", "*", "/", "%", "=", ">", "<", "&" }; Operators = Arrays.asList(operatorArr); // 界符數組 --> 界符列表 String[] boundaryArr = { "" + '{', "" + '}', "" + '[', "" + ']', "" + '(', "" + ')', "" + ';' , "" + '.' }; Boundarys = Arrays.asList(boundaryArr); // 空格字符數組 --> 空格字符列表 String[] SpaceArr = { " ", "\t", "\n" }; Spaces = Arrays.asList(SpaceArr); } static boolean isDelimiter = false;//是否有分隔符 // 字符串緩沖 static StringBuffer strb = new StringBuffer(); public static void main(String[] args) { String inStr = readFileByChars("源程序.txt"); //System.out.println("+++"+inStr); System.out.println("####################簡單詞法分析器#####################"); for (char ch : inStr.toCharArray()) { match(ch); } } static void match(char ch) { // 分割符緩沖 StringBuffer bouStrb = new StringBuffer(); // 1. 判斷字符類型 /** * (空格|操作符|界符)都是(關鍵字|標識符|數字)的分割符 * 即,任意兩個(關鍵字|標識符|數字)之間不可直接相連,而無分割符(空格|操作符|界符) */ // 1.0 空格,返回空格 if (Spaces.indexOf(ch + "") >= 0) { isDelimiter = true; } // 1.1. 操作符, 返回"operator" if (Operators.indexOf(ch + "") >= 0) { isDelimiter = true; bouStrb = new StringBuffer(ch + " 是操作符 單詞種別碼為4"); } // 1.2 界符, 返回"boundary" if (Boundarys.indexOf(ch + "") >= 0) { isDelimiter = true; bouStrb = new StringBuffer(ch + " 是界符 單詞種別碼為5"); } // 2. 如果是分割符, /** * 判斷strb中有緩沖字符串, 2.1 若有,檢查其是不是常數(暫不接受負數) 2.1.1 若是, 輸出字符串, * 並標識該字符串為constant 2.1.2 若不是, 檢查在不在關鍵字表中, 2.1.2.1 若在, * 輸出字符串,並標識該字符串為keyword 2.1.2.2 若不在, 輸出字符串,並標識該字符串為identifier * * 還原變量初始設置,並退出 */ if (isDelimiter) { if (strb.length() > 0) { if (strb.charAt(0) >= '0' && strb.charAt(0) <= '9') { System.out.println(strb + " 是常數 單詞種別碼為3"); } else if (KeyWords.indexOf(strb.toString()) >= 0) { System.out.println(strb + " 是關鍵字 單詞種別碼為1"); } else { System.out.println(strb + "是標識符 單詞種別碼為2"); } } if (bouStrb.length() > 0) System.out.println(bouStrb); strb.setLength(0); isDelimiter = false; return; } // 3. 進行到該步,說明字符不是分割符,則把該字符追加到strb中即可 strb.append(ch); } }
運行截圖