Pascal词法分析器用java实现
生活随笔
收集整理的這篇文章主要介紹了
Pascal词法分析器用java实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? 此Pascal詞法分析器翻版于一位叫csu_zhuzi老哥的作品,感興趣可去查看,他寫的很精巧很完備。
本作的特點
1.忠于原圖,與轉換圖一一對應。
2.引入swith,相對簡易的實現了關鍵字和符號的一符一碼。
3.代碼相對csu_zhuzi老哥的更容易懂,在保證功能的前提下剔除了一些復雜的邏輯判斷。
老規矩,先上圖:
轉換圖
測試文本:
運行結果:
源代碼:
package com.serein.lexical_analyzer;import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner;/*** 用java寫的Pascal語言簡易語法分析器,忠于原圖。*//*** 2、標識符 14~49、關鍵字* 4、純數字* 5、=號 6、+號 8、*號 9、**號,即平方 10、,號 11、(號 12、)號* 13、其他符號*/public class PascalLexicalAnalyzer2 {static int lines,p;static ArrayList<String> keyWord = null;static ArrayList<String> operateWord = null;public static void main(String[] args) throws Exception {File file=new File("day10-io-app2/src/LexicalAnalyzerTest.txt");keyWord = new ArrayList<>();operateWord = new ArrayList<>();Collections.addAll(keyWord, "program","function","procedure","array","const","file","label","of","packed","record","set","type","var","case","do","downto","else","for","forward","goto","if","repeat","then","to","until","while","with","and","div","in","mod" ,"not","or","begin","end","nil");Collections.addAll(operateWord,"=","+","*","**",",","(",")");lines=1;try(Scanner input=new Scanner(file)) {while (input.hasNextLine()){String str=input.nextLine();analyze(str);lines++;}}}public static void analyze(String str) {p = 0;char ch;str = str.trim(); //刪除字符串兩盤的空白for (; p < str.length(); p++) {ch = str.charAt(p);if (Character.isDigit(ch)){checkDigit(str);} else if (Character.isLetter(ch)) {checkLetter(str);} else if (ch == ' ') {continue;} else if (operateWord.contains(String.valueOf(ch))) {checkOperate(str);} else {otherChars(str);}}}private static void checkOperate(String str) {String token = String.valueOf(str.charAt(p++));char ch;switch (token){case "=" :System.out.println("(5," + token + ")");p--;break;case "+" :System.out.println("(6," + token + ")");p--;break;case "," :System.out.println("(10," + token + ")");p--;break;case "(" :System.out.println("(11," + token + ")");p--;break;case ")" :System.out.println("(12," + token + ")");p--;break;case "*" :if (p <str.length()){ch = str.charAt(p);if (operateWord.contains(token + ch)){token +=ch;System.out.println("(8," + token +")");}else {p--;System.out.println("(7," + token +")");}}break;}}private static void otherChars(String str) {String token = String.valueOf(str.charAt(p++));System.out.println("(13," + token +")");if (p != str.length() -1 || p == str.length() - 1 && !operateWord.contains(token)){p--;}}private static void checkLetter(String str) {String token = String.valueOf(str.charAt(p++));char ch;for (; p < str.length(); p++) {ch = str.charAt(p);if (!Character.isLetter(ch)){break;} else {token += ch;}}if (keyWord.contains(token)){switch (token){case "program":System.out.println("(14," + token + ")");p--;break;case "procedure":System.out.println("(15," + token + ")");p--;break;case "array":System.out.println("(16," + token + ")");p--;break;case "const":System.out.println("(17," + token + ")");p--;break;case "file":System.out.println("(18," + token + ")");p--;break;case "label":System.out.println("(19," + token + ")");p--;break;case "of":System.out.println("(20," + token + ")");p--;break;case "packed":System.out.println("(21," + token + ")");p--;break;case "record":System.out.println("(22," + token + ")");p--;break;case "set":System.out.println("(23," + token + ")");p--;break;case "type":System.out.println("(24," + token + ")");p--;break;case "var":System.out.println("(25," + token + ")");p--;break;case "case":System.out.println("(26," + token + ")");p--;break;case "do":System.out.println("(27," + token + ")");p--;break;case "downto":System.out.println("(28," + token + ")");p--;break;case "else":System.out.println("(29," + token + ")");p--;break;case "for":System.out.println("(30," + token + ")");p--;break;case "forward":System.out.println("(31," + token + ")");p--;break;case "goto":System.out.println("(32," + token + ")");p--;break;case "if":System.out.println("(33," + token + ")");p--;break;case "repeat":System.out.println("(34," + token + ")");p--;break;case "then":System.out.println("(35," + token + ")");p--;break;case "to":System.out.println("(36," + token + ")");p--;break;case "until":System.out.println("(37," + token + ")");p--;break;case "while":System.out.println("(38," + token + ")");p--;break;case "with":System.out.println("(39," + token + ")");p--;break;case "and":System.out.println("(40," + token + ")");p--;break;case "div":System.out.println("(41," + token + ")");p--;break;case "in":System.out.println("(42," + token + ")");p--;break;case "mod":System.out.println("(43," + token + ")");p--;break;case "not":System.out.println("(44," + token + ")");p--;break;case "or":System.out.println("(45," + token + ")");p--;break;case "begin":System.out.println("(46," + token + ")");p--;break;case "end":System.out.println("(47," + token + ")");p--;break;case "nil":System.out.println("(48," + token + ")");p--;break;case "function":System.out.println("(49," + token + ")");p--;break;}}else {System.out.println("(1," +token + ")");if (p != str.length() - 1|| p == str.length() - 1 && (Character.isLetterOrDigit(str.charAt(p)))){p--;} // if (p != str.length() - 1|| p == str.length() - 1 && (Character.isLetterOrDigit(str.charAt(p)))){ // p--;}}private static void checkDigit(String str) {String token = String.valueOf(str.charAt(p++));char ch;for (; p < str.length(); p++) {ch = str.charAt(p);if (ch == ' ' || (!Character.isDigit(ch))){break;} else {token += ch;}}System.out.println("(4," + token + ")");if (p != str.length() -1 || (p == str.length() - 1 && !Character.isDigit(str.charAt(p)))){p--;}}}寫在最后
? 測試文本用了IO流讀取,諸位可以直接定義一個字符串當測試文本。也可以在電腦上新建一個txt文件,然后復制自己的文件路徑替換掉我的路徑。
? 這種思路精妙之處在于定義一個靜態變量p當指針,一次掃文本一行當字符串,每掃一行總方法把指針p置零,子方法先判斷,判否立刻break,p可以到其他子方法中去。所以雖然總方法和子方法都在循環p,但是遍歷過的字符并沒有重復遍歷。指針定位和回撥是個精細活,諸位可自研之。
? 今天的分享就到這里,如果覺得有幫助,歡迎點贊評論轉發哦!
? 你們的支持是我更新的最大動力!
總結
以上是生活随笔為你收集整理的Pascal词法分析器用java实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于菜鸡学习时服务器购买的注意点
- 下一篇: [大赛推荐]短视频开发大赛,万元现金大奖