西南交大-编译原理课程设计二-赋值语句的解释程序设计.docx

上传人:scccc 文档编号:11248266 上传时间:2021-07-17 格式:DOCX 页数:32 大小:262.60KB
返回 下载 相关 举报
西南交大-编译原理课程设计二-赋值语句的解释程序设计.docx_第1页
第1页 / 共32页
西南交大-编译原理课程设计二-赋值语句的解释程序设计.docx_第2页
第2页 / 共32页
西南交大-编译原理课程设计二-赋值语句的解释程序设计.docx_第3页
第3页 / 共32页
西南交大-编译原理课程设计二-赋值语句的解释程序设计.docx_第4页
第4页 / 共32页
西南交大-编译原理课程设计二-赋值语句的解释程序设计.docx_第5页
第5页 / 共32页
点击查看更多>>
资源描述

《西南交大-编译原理课程设计二-赋值语句的解释程序设计.docx》由会员分享,可在线阅读,更多相关《西南交大-编译原理课程设计二-赋值语句的解释程序设计.docx(32页珍藏版)》请在三一文库上搜索。

1、编译原理课程设计赋值语句的解释程序设计姓名:汤朋学号:2014112217班级:软件四班时间:2017/6/13学期:2016-2017第一学期1. 设计题目:赋值语句的解释程序设计2. 设计内容:用算符优先分析方法设计一个分析解释程序,对输入的赋值语句、输出语句、清除语句进行词法分析、语法分析、表达式求值并存储于指定变量中:若存在错误,提示错误相关信息。3. 设计目的:a) 了解掌握算符优先分析的基本方法、内容b) 学会科学思考并解决问题,提高程序设计能力4. 实现环境 电脑:l Windows10家庭中文版l 型号:雷神l 处理器:Intel(R) Core(TM) i7-6700HQ C

2、PU 2.60GHz l RAM:16.0GB(15.9GB可用)l 系统类型:64位操作系统,基于x64的处理器 实现语言及环境:Java,JDK 1.8IDE:Ecplise neon.15. 概要设计文法表示:S v=E|E?|clearEE+T|E-T|TTT*F|T/F|FF(E)|v|c归约规则:N v=N| N?|clearN N + N | N - N | NN N * N | N / N | NN ( N)|v|c种别码设计:单词符号种别码=1?2+3-4*5/6(7)8v9c10clear11#12N13优先关系表123456789101112=?+-*/()vcclear

3、#1=2?3+4-5*6/7(=8)9v=10c11clear12#=程序流程图6. 详细设计单词符号二元组使用下面的类来表示:public class WordSymbol public static final int TYPE_NULL = 0; /无值public static final int TYPE_INT = 1; /整数public static final int TYPE_STRING = 2; /字符串int code;/种别码int type;/单词符号值类型Object value;/单词符号的属性值public WordSymbol() super();publ

4、ic WordSymbol(int code, int type, Object value) super();this.code = code;this.type = type;this.value = value;public int getCode() return code;public void setCode(int code) this.code = code;public int getType() return type;public void setType(int type) this.type = type;public Object getValue() return

5、 value;public void setValue(Object value) this.value = value;Overridepublic String toString() return WordSymbol code= + code + , type= + type + , value= + value + ;归约栈:用Java中的栈对象 Stack 来表示单词串:用链表对象List来存放单词串变量表:使用Map对象来充当变量表,其以键值对的方式存放变量,键可以设为变量名,值存放变量值变量名值KeyValue可归约串语义解释:变量归约:N v,在变量表中查找该变量,若不存在则报

6、错:变量未定义,否则修改非终结符N的属性值为变量v的值,并设N的种别码为13常量归约:N c,修改非终结符N的属性值为常量c的值,并设N的种别码为13运算归约:设运算的操作数为N1,N2;将N1,N2进行相应运算并将运算结果设为N3的属性值,将N3的种别码设为13括号归约:将(N)归约为N赋值归约:在变量表中查找被赋值的变量v,若不存在,则先在变量表中创建该变量,然后再将N的属性值赋值给v,最后将 v = N归约为N输出语句:先输出表达式N的属性值,然后将N?归约为N清除语句:将变量表中的所以变量清空,然后clear归约为N运算符之间的关系使用对象Relation来描述,其结构如下public

7、 class Relation public static final int REL_LESS = -1; / 小于public static final int REL_EQUAL = 0; / 等于public static final int REL_GREATER = 1; / 大于public static final int REL_NULL = 2; / 无关系int codeLeft, codeRight;int relation;public Relation() super();public Relation(int codeLeft, int codeRight, in

8、t relation) super();this.codeLeft = codeLeft;this.codeRight = codeRight;this.relation = relation;public int getCodeLeft() return codeLeft;public void setCodeLeft(int codeLeft) this.codeLeft = codeLeft;public int getCodeRight() return codeRight;public void setCodeRight(int codeRight) this.codeRight =

9、 codeRight;public int getRelation() return relation;public void setRelation(int relation) this.relation = relation;Overridepublic String toString() String str = ;switch (relation) case -1:str = ;break;case 0:str = = ;break;case 2:str = 无关系 ;break;return Relation: + codeLeft + str + codeRight;同时,使用Re

10、lation二维数组来存放优先符关系表7. 程序清单package tp;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Stack;public class AssignmentExplain / 单词符号private String words = =, ?, +, -, *, /, (, ), v, c, clear, #, N ;private Stack stack = new Stack(); / 归约栈pr

11、ivate List wordSymbols = new ArrayList(); / 单词符号private Map variables = new HashMap(); / 变量表private Relation relations; / 优先关系表private int index = 0;public static void main(String args) String str = a=3;(a+17)/3?;AssignmentExplain assignmentExplain = new AssignmentExplain();try String sentence = str

12、.split(;);for (String inputStr : sentence) assignmentExplain.getDoubleGroup(inputStr);System.out.println(n单词符号串:);System.out.println(assignmentExplain.wordSymbols + n);assignmentExplain.startExplain(inputStr);System.out.println(n变量表:);for (String key : assignmentExplain.variables.keySet() System.out

13、.println(key + : + assignmentExplain.variables.get(key); catch (Exception e) e.printStackTrace();public void initRelation() relations = new Relation1212;for (int i = 0; i 12; i+) for (int j = 0; j 12; j+) Relation relation = new Relation();relation.setCodeLeft(i + 1);relation.setCodeRight(j + 1);swi

14、tch (i + 1) case 1:switch (j + 1) case 1:case 2:case 8:case 11:relation.setRelation(Relation.REL_NULL);break;case 3:case 4:case 5:case 6:case 7:case 9:case 10:relation.setRelation(Relation.REL_LESS);break;case 12:relation.setRelation(Relation.REL_GREATER);break;break;case 2:switch (j + 1) case 12:re

15、lation.setRelation(Relation.REL_GREATER);break;default:relation.setRelation(Relation.REL_NULL);break;break;case 3:case 4:switch (j + 1) case 1:case 11:relation.setRelation(Relation.REL_NULL);break;case 2:case 3:case 4:case 8:case 12:relation.setRelation(Relation.REL_GREATER);break;default:relation.s

16、etRelation(Relation.REL_LESS);break;break;case 5:case 6:switch (j + 1) case 1:case 11:relation.setRelation(Relation.REL_NULL);break;case 7:case 9:case 10:relation.setRelation(Relation.REL_LESS);break;default:relation.setRelation(Relation.REL_GREATER);break;break;case 7:switch (j + 1) case 1:case 2:c

17、ase 11:relation.setRelation(Relation.REL_NULL);break;case 8:relation.setRelation(Relation.REL_EQUAL);break;case 12:relation.setRelation(Relation.REL_GREATER);break;default:relation.setRelation(Relation.REL_LESS);break;break;case 8:switch (j + 1) case 1:case 7:case 9:case 10:case 11:relation.setRelat

18、ion(Relation.REL_NULL);break;default:relation.setRelation(Relation.REL_GREATER);break;break;case 9:switch (j + 1) case 1:relation.setRelation(Relation.REL_EQUAL);break;case 7:case 9:case 10:case 11:relation.setRelation(Relation.REL_NULL);break;default:relation.setRelation(Relation.REL_GREATER);break

19、;break;case 10:switch (j + 1) case 1:case 7:case 9:case 10:case 11:relation.setRelation(Relation.REL_NULL);break;default:relation.setRelation(Relation.REL_GREATER);break;break;case 11:switch (j + 1) case 12:relation.setRelation(Relation.REL_GREATER);break;default:relation.setRelation(Relation.REL_NU

20、LL);break;break;case 12:switch (j + 1) case 12:relation.setRelation(Relation.REL_EQUAL);break;default:relation.setRelation(Relation.REL_LESS);break;break;relationsij = relation;/ 扫描缓冲区,求得二元组public void getDoubleGroup(String inputStr) throws Exception wordSymbols.clear();inputStr = # + inputStr + #;i

21、ndex = 0;while (index = inputStr.length()break;if (ch.equals( ) index+;continue;if (Character.isLetter(ch.charAt(0) if (!recognizeIdentifier(inputStr) new Exception(不能识别的标识符);index+; else if (Character.isDigit(ch.charAt(0) if (!recognizeInteger(inputStr) new Exception(不能识别的整数);index+; else int code

22、= -1;switch (ch) case =:code = codeOfWord(=);if (code = -1)System.out.println(找不到该单词符号的种别码);else WordSymbol symbol = new WordSymbol(code, WordSymbol.TYPE_NULL, -);wordSymbols.add(symbol);break;case ?:code = codeOfWord(?);if (code = -1)System.out.println(找不到该单词符号的种别码);else WordSymbol symbol = new Wor

23、dSymbol(code, WordSymbol.TYPE_NULL, -);wordSymbols.add(symbol);break;case +:code = codeOfWord(+);if (code = -1)System.out.println(找不到该单词符号的种别码);else WordSymbol symbol = new WordSymbol(code, WordSymbol.TYPE_NULL, -);wordSymbols.add(symbol);break;case -:code = codeOfWord(-);if (code = -1)System.out.pr

24、intln(找不到该单词符号的种别码);else WordSymbol symbol = new WordSymbol(code, WordSymbol.TYPE_NULL, -);wordSymbols.add(symbol);break;case *:code = codeOfWord(*);if (code = -1)System.out.println(找不到该单词符号的种别码);else WordSymbol symbol = new WordSymbol(code, WordSymbol.TYPE_NULL, -);wordSymbols.add(symbol);break;cas

25、e /:code = codeOfWord(/);if (code = -1)System.out.println(找不到该单词符号的种别码);else WordSymbol symbol = new WordSymbol(code, WordSymbol.TYPE_NULL, -);wordSymbols.add(symbol);break;case (:code = codeOfWord();if (code = -1)System.out.println(找不到该单词符号的种别码);else WordSymbol symbol = new WordSymbol(code, WordSym

26、bol.TYPE_NULL, -);wordSymbols.add(symbol);break;case ):code = codeOfWord();if (code = -1)System.out.println(找不到该单词符号的种别码);else WordSymbol symbol = new WordSymbol(code, WordSymbol.TYPE_NULL, -);wordSymbols.add(symbol);break;case clear:code = codeOfWord(clear);if (code = -1)System.out.println(找不到该单词符号

27、的种别码);else WordSymbol symbol = new WordSymbol(code, WordSymbol.TYPE_NULL, -);wordSymbols.add(symbol);break;case #:code = codeOfWord(#);if (code = -1)System.out.println(找不到该单词符号的种别码);else WordSymbol symbol = new WordSymbol(code, WordSymbol.TYPE_NULL, -);wordSymbols.add(symbol);break;case N:code = cod

28、eOfWord(N);if (code = -1)System.out.println(找不到该单词符号的种别码);else WordSymbol symbol = new WordSymbol(code, WordSymbol.TYPE_NULL, -);wordSymbols.add(symbol);break;default:throw new Exception(无法识别的字符);System.out.println(识别出界符/运算符: + ch);index+;/ 识别标识符的子程序public boolean recognizeIdentifier(String inputStr

29、) int state = 0;String strToken = ;String ch = ;while (index inputStr.length() ch = inputStr.substring(index, index + 1);switch (state) case 0:if (Character.isLetter(ch.charAt(0) state = 1;strToken += ch;index+; else return false;break;case 1:if (Character.isLetter(ch.charAt(0) | Character.isDigit(c

30、h.charAt(0) strToken += ch;index+; else state = 2;index-;break;case 2:int code = codeOfWord(v);if (code = -1)System.out.println(找不到该单词符号的种别码);else if (strToken.equals(clear)code = 11;WordSymbol symbol = new WordSymbol(code, WordSymbol.TYPE_STRING, strToken);wordSymbols.add(symbol);return true;return

31、 false;/ 识别常整数的子程序public boolean recognizeInteger(String inputStr) int state = 0;String strToken = ;String ch = ;while (index inputStr.length() ch = inputStr.substring(index, index + 1);switch (state) case 0:if (Character.isDigit(ch.charAt(0) state = 1;strToken += ch;index+; else return false;break;

32、case 1:if (Character.isDigit(ch.charAt(0) strToken += ch;index+; else state = 2;break;case 2:index-;int code = codeOfWord(c);if (code = -1)System.out.println(找不到该单词符号的种别码);else WordSymbol symbol = new WordSymbol(code, WordSymbol.TYPE_INT, Integer.valueOf(strToken);wordSymbols.add(symbol);return true

33、;return false;/ 返回单词符号的种别码public int codeOfWord(String word) for (int i = 0; i words.length; i+) if (wordsi.equals(word)return i + 1;return -1;public void startExplain(String inputStr) throws Exception / 开始归约initRelation();stack.clear();int pointer = 0;WordSymbol topWord = wordSymbols.get(pointer);p

34、ointer+;WordSymbol rightWord;stack.add(topWord);while (pointer = 0) if (stack.get(stackSize).getCode() != 13) topWord = stack.get(stackSize);break;rightWord = wordSymbols.get(pointer);System.out.println(栈: + stack);/ System.out.println(rightWord: + rightWord);Relation temp = relationstopWord.getCode() - 1rightWord.getCode

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1