当前位置:文档之家› 编译原理词法分析及语法分析

编译原理词法分析及语法分析

编译原理实验报告实验名称:词法分析及语法分析专业班级:姓名:学号:完成日期:实验一、sample语言的词法分析一、实验目的给出SAMPLE文法规范,要求编写SAMPLE语言的词法分析程序。

二、实验准备了解sample语言单词的定义,选择任一种编程语言实现词法分析。

三、实验内容给出SAMPLE语言文法,输出单词(关键字、专用符号以及其它标记)。

1、格式输入:源程序文件。

输出:关键字、专用符号以及其它标记。

2、实现原理程序中先判断这个句语句中每个单元为关键字、常数、运算符、界符,对与不同的单词符号给出不同编码形式的编码,用以区分之。

3、实验方法读懂Sample源代码,自己重点独立实现对常量的判别。

四、实验设计1、设计SAMPLE语言的词法分析器A、字符集定义1. <字符集> → <字母>│<数字>│<单界符>2. <字母> → A│B│…│Z│a│b│…│z3. <数字> → 0│1│2│…│94. <单界符> → +│-│*│/│=│<│>│(│)│[│]│:│. │; │, │'B、单词集定义5.<单词集> → <保留字>│<双界符>│<标识符>│<常数>│<单界符>6.<保留字> → and│array│begin│bool│call│case│char│constant│dim│do│else │end│false│for│if│input│integer│not│of│or│output│procedure│program│read│real│repeat│set│stop│then│to│true│until│var│while│write 7.<双界符> → <>│<=│>=│:= │/*│*/│..8.<标识符> → <字母>│<标识符> <数字>│<标识符> <字母>9.<常数> → <整数>│<布尔常数>│<字符常数>10.<整数> → <数字>│<整数> <数字>11.<布尔常数> → true│false12.<字符常数> → ' 除 {'} 外的任意字符串 '2、词法分析系统流程设计四、源程序清单1、源代码/*** 读取测试文件中的每一行然后传入Scaner中扫描*/import java.io.File;import java.io.RandomAccessFile;public class Analyzer {private Scaner scaner;private File testFile;public static final String testFileAdd = "e:\\t7.txt"; //测试文件的绝对地址private RandomAccessFile fileReandomReader;public void initAnalayzer() {this.scaner = new Scaner();try {testFile = new File(testFileAdd);this.fileReandomReader = new RandomAccessFile(testFile, "r");} catch (Exception e) {System.out.println("测试文件不存在");}}public void startAnalyze() {String tmpString = "";String result;int row = 0;System.out.println("······················开始分析·····················");try {while ((tmpString = this.fileReandomReader.readLine()) != null) {++row;System.out.println("·····················analyze row " + row+ "·················");// System.err.println(tmpString + "!! " +row);result = scaner.scan(tmpString);System.out.println(result);scaner.cleanScaner();tmpS tring = "";}} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {Analyzer a = new Analyzer();a.initAnalayzer();a.startAnalyze();}}/*** 分析从analyze传过来的每一行源码*/import java.util.HashMap;import java.util.Map;public class Scaner {private char ch;private String token;private String result;private int index = 0;private char[] strChar = new char[1000];private int rowLength = 0;// private KeywordConfig keywordConfig;// private PunctuationConfig punctuationConfig;//保留字键值对表private Map<String , Integer> keywords;//单界符键值对表private Map<String , String> punctuations;//双界符键值对表private Map<String,String> doublePun;public Scaner() {this.result = "";// this.keywordConfig = new KeywordConfig();// this.punctuationConfig = new PunctuationConfig();//保留字键值对表创建this.keywords = new HashMap<String , Integer>();keywords.put("and", 1); keywords.put("array", 2);keywords.put("begin", 3); keywords.put("bool", 4);keywords.put("call", 5); keywords.put("case", 6);keywords.put("char", 7); keywords.put("constant", 8);keywords.put("dim", 9); keywords.put("do", 10);keywords.put("else", 11); keywords.put("end", 12);keywords.put("write", 13); keywords.put("for", 14);keywords.put("if", 15); keywords.put("input", 16);keywords.put("integer", 17); keywords.put("not", 18);keywords.put("of", 19); keywords.put("or", 20);keywords.put("output", 21); keywords.put("procedure", 22);keywords.put("program", 23); keywords.put("read", 24);keywords.put("real", 25); keywords.put("repeat", 26);keywords.put("set", 27); keywords.put("stop", 28);keywords.put("then", 29); keywords.put("to", 30);keywords.put("while", 31); keywords.put("until", 32);keywords.put("var", 33);//单界符键值对表创建this.punctuations = new HashMap<String , String>();punctuations.put("+", "加号"); punctuations.put("-", "减号");punctuations.put("*", "星号"); punctuations.put("/", "除号");punctuations.put("(", "左圆括号"); punctuations.put(")", "右圆括号");punctuations.put("{", "左花括号"); punctuations.put("}", "右花括号");punctuations.put("[", "左方括号"); punctuations.put("]", "右方括号");punctuations.put(",", "逗号"); punctuations.put(".", "点号");punctuations.put(";", "分号"); punctuations.put("%", "百分号");punctuations.put("\'", "单引号"); punctuations.put("\"", "双引号");//双界符键值对初始化this.doublePun = new HashMap<String,String>();doublePun.put("<>", "双界符1"); doublePun.put(">=", "双界符2");doublePun.put("<=", "双界符3"); doublePun.put("/*", "双界符4");doublePun.put("*/", "双界符5"); doublePun.put(":=", "双界符6");doublePun.put("..", "双界符7");}public String scan(String rowString) {strChar = rowString.toCharArray();rowLength = strChar.length;while (this.index <= rowLength) {this.token = "";//获取字符this.ch = this.getBC(strChar);if (this.ch == ';') {break;} else if (ng.Character.isLetter(ch)) {//判断首ch是否为字符this.token = this.contact(token, ch);//获取下一字符ch = this.getNextChar(strChar);//如果指定的字符是字符或者数字while ((ng.Character.isLetter(ch)) ||(ng.Character.isDigit(ch))) {//拼凑当前读取的字符串this.token = this.contact(token, ch);ch = this.getNextChar(strChar);}this.index--;// System.err.println("!!!"+this.token);//判断hashmap键值对中有没有该字符串,有则匹配输出if (this.findKeyword(token)) {this.result += "[保留字] " + this.token.toString() + "\n";}else if(token.equals("true")){this.result += "[布尔常数] " + this.token.toString() + "\n";}else if(token.equals("false")){this.result += "[布尔常数] " + this.token.toString() + "\n";}elsethis.result += "[标识符] " + this.token.toString() + "\n";this.token = "";} else if (ng.Character.isDigit(ch)) {//如果首个字符是数字this.token = this.contact(token, ch);ch = this.getNextChar(strChar);while (ng.Character.isDigit(ch)) {this.token = this.contact(token, ch);ch = this.getNextChar(strChar);}this.index--;this.result += "[数字] " + this.token.toString() + "\n";this.token = "";} else {//如果首字符不是字母也不是数字char a = ch;String key = String.valueOf(ch);//如果是符号,则到键值对中匹配输出if (this.findPunctuation(key)) {this.result += "[单界符:" + this.getPunctuation(key)+ "] " + key + "\n";}else if (key.equals(" ") || key.equals(" ")) {break;} else{//获取下一字符ch = this.getNextChar(strChar);if(!ng.Character.isDigit(ch)&&!ng.Character.isLetter(ch)){this.token = this.contact(token, a);this.token = this.contact(token, ch);//System.out.println(a+"%%%%%%%%"+token+"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"+thi s.finddoublePun(token));if(this.finddoublePun(token)){this.result += "[" + this.getdoublePun(token)+ "] " + token + "\n";}}else{this.index -= 1;// System.out.println("未知字符:");}this.token = "";}}}return result;}public void cleanScaner() {this.ch = ' ';this.index = 0;this.result = "";this.rowLength = 0;this.strChar = null;}public char getBC(char[] strChar) {try {while ((strChar[this.index]) == ' ') {this.index++;}this.index++;} catch (ArrayIndexOutOfBoundsException e) {return';';}return strChar[this.index - 1];}public char getNextChar(char[] strChar) {this.index++;return strChar[this.index - 1];}public String contact(String token, char ch) {// System.out.println(token + String.valueOf(ch)+"&&&&&&&&&&&&&&&&&");return token + String.valueOf(ch);}public boolean findKeyword(String str){if(this.keywords.containsKey(str)){return true;}return false;}public boolean findPunctuation(String str){if(this.punctuations.containsKey(str)){return true;}else return false;}public boolean finddoublePun(String str){if(this.doublePun.containsKey(str)){return true;}else return false;}public String getdoublePun(String str){return this.doublePun.get(str);}public String getPunctuation(String str){return this.punctuations.get(str);}}2、测试文件program example;vara,b,c:integer;x:char;beginif(a+c*3>b) and (b>3)then c:=3;x:=2+(3*a)-b*c*8;if(2+3>a) and (b>3) and (a>c)then c:=3;for x:=1+2 to 3 dob:=100;while a>b doc:=5;for x:=1+d to 3 dob:=15;while t>b doc:=5;repeat a:=10; until a>b;end.3、测试结果······················开始分析····················· ·····················analyze row 1·················[保留字] program[标识符] example·····················analyze row 2·················[保留字] var·····················analyze row 3················· [标识符] a[单界符:逗号] ,[标识符] b[单界符:逗号] ,[标识符] c[保留字] integer·····················analyze row 4················· [标识符] x[保留字] char·····················analyze row 5················· [保留字] begin·····················analyze row 6················· [保留字] if[单界符:左圆括号] ([标识符] a[单界符:加号] +[标识符] c[单界符:星号] *[数字] 3[标识符] b[单界符:右圆括号] )[保留字] and[单界符:左圆括号] ([标识符] b[数字] 3[单界符:右圆括号] )·····················analyze row 7················· [保留字] then[标识符] c[双界符6] :=[数字] 3·····················analyze row 8················· [标识符] x[双界符6] :=[数字] 2[单界符:加号] +[单界符:左圆括号] ([数字] 3[单界符:星号] *[标识符] a[单界符:右圆括号] )[单界符:减号] -[标识符] b[单界符:星号] *[标识符] c[单界符:星号] *[数字] 8·····················analyze row 9················· [保留字] if[单界符:左圆括号] ([数字] 2[单界符:加号] +[数字] 3[标识符] a[单界符:右圆括号] )[保留字] and[单界符:左圆括号] ([标识符] b[数字] 3[单界符:右圆括号] )[保留字] and[单界符:左圆括号] ([标识符] a[标识符] c[单界符:右圆括号] )·····················analyze row 10················· [保留字] then[标识符] c[双界符6] :=[数字] 3·····················analyze row 11················· [保留字] for[标识符] x[双界符6] :=[数字] 1[单界符:加号] +[数字] 2[保留字] to[数字] 3[保留字] do·····················analyze row 12················· [标识符] b[双界符6] :=[数字] 100·····················analyze row 13················· [保留字] while[标识符] a[标识符] b[保留字] do·····················analyze row 14················· [标识符] c[双界符6] :=[数字] 5·····················analyze row 15················· [保留字] for[标识符] x[双界符6] :=[数字] 1[单界符:加号] +[标识符] d[保留字] to[数字] 3[保留字] do·····················analyze row 16················· [标识符] b[双界符6] :=[数字] 15·····················analyze row 17················· [保留字] while[标识符] t[标识符] b[保留字] do·····················analyze row 18················· [标识符] c[双界符6] :=[数字] 5·····················analyze row 19·················[保留字] repeat[标识符] a[双界符6] :=[数字] 10·····················analyze row 20·················[保留字] end[单界符:点号] .实验二、sample语言的语法分析一、实验目的给出SAMPLE文法规范,要求编写SAMPLE语言的语法分析程序。

相关主题