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

System.out.println(\輸入串在\ + str + \附近有語法錯誤!\); stack.clear(); variables.clear();

thrownew Exception(\輸入串在\ + str + \附近有語法錯誤!\); intleftPos, rightPos = stack.size() - 1; leftPos = rightPos - 1;

while (stack.get(rightPos).getCode() == 13) { }

while (stack.get(leftPos).getCode() == 13) { }

while (relations[stack.get(leftPos).getCode() - }

// System.out.println(\終結符:left:\ // rightPos); leftPos++;

if (leftPos == stack.size() - 1) {

if (stack.get(leftPos).getCode() == 11) {

variables.clear();

String key = stack.get(leftPos).getValue().toString(); Integer integer = variables.get(key); if (integer == null) { }

WordSymbol symbol = stack.pop(); symbol.setCode(13); stack.push(symbol);

21

} else {

rightPos--; leftPos--;

leftPos--;

1][stack.get(rightPos).getCode() - 1]

.getRelation() == 0) { rightPos = leftPos; leftPos--;

while (stack.get(leftPos).getCode() == 13) { }

leftPos--;

} elseif (stack.get(leftPos).getCode() == 9) {

thrownew Exception(\變量:\ + key + \未定義!\); WordSymbol symbol = stack.pop(); symbol.setCode(13); symbol.setValue(integer); stack.push(symbol);

} else {

} else {

}

System.out.println(\輸出語句:\ + stack.pop();

if (stack.get(leftPos).getCode() == 9) {

String key = stack.get(leftPos).getValue().toString(); Integer integer = variables.get(key); if (integer == null) { }

stack.pop();

WordSymbol N = stack.pop(); stack.pop(); stack.push(N);

WordSymbol operator1, operator2; operator2 = stack.pop();

intcode = stack.pop().getCode(); operator1 = stack.pop(); switch (code) { case 3:

operator1.setValue((Integer) operator1.getValue() break;

operator1.setValue((Integer) operator1.getValue() break;

operator1.setValue((Integer) operator1.getValue() WordSymbol N = stack.pop(); stack.pop(); stack.pop();

variables.put(key, (Integer) N.getValue()); stack.push(N);

WordSymbol symbol = stack.pop();

variables.put(key, (Integer) symbol.getValue()); stack.pop(); stack.pop(); stack.push(symbol);

} elseif (stack.size() - leftPos == 2) {

stack.get(leftPos).getValue());

} elseif (stack.size() - leftPos == 3) {

} else {

} elseif (stack.get(leftPos).getCode() == 7) {

} else {

+ (Integer) operator2.getValue());

case 4:

- (Integer) operator2.getValue());

case 5:

* (Integer) operator2.getValue());

22

}

}

}

}

}

}

}

break;

operator1.setValue((Integer) operator1.getValue() break;

thrownew Exception(\不能識別操作符:\ +

case 6:

/ (Integer) operator2.getValue());

default:

stack.get(leftPos + 1).getValue());

stack.push(operator1);

} else {

thrownew Exception(\表達式錯誤\);

System.out.println(\棧:\ + stack);

if (stack.size() == 3 &&stack.pop().getCode() == 12 }

&&stack.pop().getCode() == 12) {

&&stack.pop().getCode() == 13

} else {

variables.clear();

thrownew Exception(\表達式錯誤\);

8. 運行結果

輸入串:a=5;b=a+10;b?;b+a*a?;a=a+b ,其中每一條可執行語句以

23

分號隔開 單詞串:

? a=5 : [(12,0,-), (9,2,a), (1,0,-), (10,1,5), (12,0,-)]

? b=a+10 : [(12,0,-), (9,2,b), (1,0,-), (9,2,a), (3,0,-), (10,1,10), (12,0,-)]

? b? : [(12,0,-), (9,2,b), (2,0,-), (12,0,-)]

? b+a*a? : [(12,0,-), (9,2,b), (3,0,-), (9,2,a), (5,0,-), (9,2,a), (2,0,-), (12,0,-)]

? a=a+b: [(12,0,-), (9,2,a), (1,0,-), (9,2,a), (3,0,-), (9,2,b), (12,0,-)] 執行上述語句的控制臺輸出(展示了每一條程序執行的詳細步驟包括識別單詞符號,運算符優先關系比較,棧與變量表的內容變化過程等):

識別出界符/運算符:# 識別出界符/運算符:= 識別出界符/運算符:#

單詞符號串:

[(12,0,-), (9,2,a), (1,0,-), (10,1,5), (12,0,-)]

棧:[(12,0,-)] 優先關系:(12,9,<) 棧:[(12,0,-), (9,2,a)] 優先關系:(9,1,=)

棧:[(12,0,-), (9,2,a), (1,0,-)] 優先關系:(1,10,<)

棧:[(12,0,-), (9,2,a), (1,0,-), (10,1,5)] 優先關系:(10,12,>)

棧:[(12,0,-), (9,2,a), (1,0,-), (13,1,5)] 優先關系:(1,12,>)

棧:[(12,0,-), (13,1,5)]

24

優先關系:(12,12,=)

棧:[(12,0,-), (13,1,5), (12,0,-)]

變量表: a:5

識別出界符/運算符:# 識別出界符/運算符:= 識別出界符/運算符:+ 識別出界符/運算符:#

單詞符號串:

[(12,0,-), (9,2,b), (1,0,-), (9,2,a), (3,0,-), (10,1,10), (12,0,-)]

棧:[(12,0,-)] 優先關系:(12,9,<) 棧:[(12,0,-), (9,2,b)] 優先關系:(9,1,=)

棧:[(12,0,-), (9,2,b), (1,0,-)] 優先關系:(1,9,<)

棧:[(12,0,-), (9,2,b), (1,0,-), (9,2,a)] 優先關系:(9,3,>)

棧:[(12,0,-), (9,2,b), (1,0,-), (13,2,5)] 優先關系:(1,3,<)

棧:[(12,0,-), (9,2,b), (1,0,-), (13,2,5), (3,0,-)] 優先關系:(3,10,<)

棧:[(12,0,-), (9,2,b), (1,0,-), (13,2,5), (3,0,-), (10,1,10)] 優先關系:(10,12,>)

棧:[(12,0,-), (9,2,b), (1,0,-), (13,2,5), (3,0,-), (13,1,10)] 優先關系:(3,12,>)

棧:[(12,0,-), (9,2,b), (1,0,-), (13,2,15)] 優先關系:(1,12,>)

棧:[(12,0,-), (13,2,15)] 優先關系:(12,12,=)

棧:[(12,0,-), (13,2,15), (12,0,-)]

變量表: a:5 b:15

識別出界符/運算符:# 識別出界符/運算符:? 識別出界符/運算符:#

單詞符號串:

[(12,0,-), (9,2,b), (2,0,-), (12,0,-)]

25





宁夏11选5(任六)