西南交大-編譯原理課程設計二-賦值語句的解釋程序設計 - 下載本文

6. 詳細設計

單詞符號二元組使用下面的類來表示:

publicclass WordSymbol {

@Override

public String toString() {

6

publicstaticfinalintTYPE_NULL = 0; //無值 publicstaticfinalintTYPE_INT = 1; //整數 publicstaticfinalintTYPE_STRING = 2; //字符串

intcode; inttype;

public WordSymbol() { }

public WordSymbol(intcode, inttype, Object value) { }

publicint getCode() { }

publicvoid setCode(intcode) { }

publicint getType() { }

publicvoid setType(inttype) { }

public Object getValue() { }

publicvoid setValue(Object value) { }

this.value = value; returnvalue; this.type = type; returntype; this.code = code; returncode; super();

this.code = code; this.type = type; this.value = value; super();

//種別碼

//單詞符號值類型

Object value; //單詞符號的屬性值

}

}

return\[code=\ + code + \type=\ + type + \value=\ + value

+ \;

歸約棧:用Java中的棧對象Stack來表示 單詞串:用鏈表對象List來存放單詞串 變量表:

使用Map對象來充當變量表,其以鍵值對的方式存放變量,鍵可以設為變量名,值存放變量值

變量名 Key 可歸約串語義解釋:

變量歸約:N?v,在變量表中查找該變量,若不存在則報錯:變量未定義,否則修改非終結符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

7

值 Value 清除語句:將變量表中的所以變量清空,然后clear歸約為N 運算符之間的關系使用對象Relation來描述,其結構如下

publicclass Relation {

publicvoid setRelation(intrelation) {

8

publicstaticfinalintREL_LESS = -1; // 小于 publicstaticfinalintREL_EQUAL = 0; // 等于 publicstaticfinalintREL_GREATER = 1; // 大于 publicstaticfinalintREL_NULL = 2; // 無關系 intcodeLeft, codeRight; intrelation; public Relation() { }

public Relation(intcodeLeft, intcodeRight, intrelation) { }

publicint getCodeLeft() { }

publicvoid setCodeLeft(intcodeLeft) { }

publicint getCodeRight() { }

publicvoid setCodeRight(intcodeRight) { }

publicint getRelation() { }

returnrelation;

this.codeRight = codeRight; returncodeRight;

this.codeLeft = codeLeft; returncodeLeft; super();

this.codeLeft = codeLeft; this.codeRight = codeRight; this.relation = relation; super();

}

this.relation = relation;

@Override

public String toString() { } }

String str = \; switch (relation) { case -1: }

return\:\ + codeLeft + str + codeRight;

str = \; break; str = \; break; str = \; break;

str = \無關系 \; break;

case 1:

case 0:

case 2:

同時,使用Relation[][]二維數組來存放優先符關系表

7. 程序清單

package tp;

import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Stack;

publicclass AssignmentExplain {

// 單詞符號

private String[] words = { \, \, \, \, \, \, \, \, \, \, private Stackstack = new Stack<>(); // 歸約棧

private ListwordSymbols = new ArrayList<>(); // 單詞符號 private Mapvariables = new HashMap<>(); // 變量表

9

\, \, \ };

private Relation[][] relations; // 優先關系表 privateintindex = 0;

publicstaticvoid main(String[] args) { }

publicvoid initRelation() {

relations = new Relation[12][12]; for (inti = 0; i< 12; i++) {

for (intj = 0; j< 12; j++) {

Relation relation = new Relation(); relation.setCodeLeft(i + 1); relation.setCodeRight(j + 1); switch (i + 1) { case 1:

switch (j + 1) { case 1: case 2: case 8: case 11:

relation.setRelation(Relation.REL_NULL); break;

String str = \;

AssignmentExplain assignmentExplain = new AssignmentExplain(); try { }

String[] sentence = str.split(\); for (String inputStr : sentence) { }

e.printStackTrace();

assignmentExplain.getDoubleGroup(inputStr); System.out.println(\單詞符號串:\);

System.out.println(assignmentExplain.wordSymbols + \); assignmentExplain.startExplain(inputStr); System.out.println(\變量表:\);

for (String key : assignmentExplain.variables.keySet()) { }

System.out.println(key + \ +

assignmentExplain.variables.get(key));

} catch (Exception e) {

case 3: case 4: case 5: case 6:

10





宁夏11选5(任六)