




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、編譯技術(shù)第5次上機內(nèi)容目的:充分理解語義分析的方法及相關(guān)語義計算的執(zhí)行時機。要求:1以S屬性的語法制導定義為基礎(chǔ),將下表的語義規(guī)則嵌套在語法分析的過程中,即實現(xiàn)語法制導的翻譯過程。產(chǎn) 生 式 語 義 規(guī) 則 L E n print (E.val) E E1 + T E.val := E1 .val + T.val E T E.val := T.val T T1 * F T.val := T1.val * F.val T F T.val := F.val F (E) F.val := E.val F digit F.val := digit.lexval2以詞法分析和語法分析部分的上機結(jié)果為基礎(chǔ)
2、,添加語義分析部分。即以LR文法為基礎(chǔ)。當進行產(chǎn)生式歸約時執(zhí)行對應(yīng)的語義動作。3輸入:5+3+8*2輸出:244 若輸入有誤,如:3+2則應(yīng)提示:重新輸入!5 由于輸入串是具體的數(shù)值,因此應(yīng)調(diào)用相應(yīng)的詞法分析的功能。/ Expression.cpp : Defines the entry point for the console application./#include stdafx.h#include conio.h#include#include#includeusing namespace std;#define L 0#define E 1#define T 2#define E_
3、 3#define T_ 4#define F 6#define digit 7/ 數(shù)字#define add 8/ 左括號#define mul 9/ 右括號#define lb 10#define rb11int nStackPtr;int Stack100;/ 棧void guiyue(stack &state,stack &value,char nex);void Push(int n)nStackPtr +;StacknStackPtr = n;void Pop()nStackPtr-;void PrintStack()int i;for (i = nStackPtr; i = 0;
4、 i-) if (Stacki = E) printf(E );if (Stacki = E_ ) printf(E );if (Stacki = T ) printf(T );if (Stacki = T_ ) printf(T );if (Stacki = F) printf(F );if (Stacki = digit) printf(digit );if (Stacki = add) printf(+ );if (Stacki = mul) printf(* );if (Stacki = lb) printf( );if (Stacki = rb) printf() );printf(
5、n);/ 利用棧來分析表達式串,判定表達式串是否正確/ /int main(int argc, char* argv)char strInput100;/ 存放表達式串bool bResult;int nInputPtr;nStackPtr = -1;nInputPtr = 0;bResult = true;/ 輸入表達式串,存放在 strInput中printf(請輸入表達式串:);scanf(%s,strInput);Push(E);PrintStack();while (bResult & nStackPtr = 0)switch(StacknStackPtr)case E:if (st
6、rInputnInputPtr = 0 & strInputnInputPtr = 0 & strInputnInputPtr = 0 & strInputnInputPtr= 0 & strInputnInputPtr=9) Pop();nInputPtr+;else bResult = false;PrintStack();break;case add:if (strInputnInputPtr = +) Pop();nInputPtr+;else bResult = false;PrintStack();break;case mul:if (strInputnInputPtr = *)
7、Pop();nInputPtr+;else bResult = false;PrintStack();break;case lb:if (strInputnInputPtr = () Pop();nInputPtr+;else bResult = false;PrintStack();break;case rb:if (strInputnInputPtr = ) Pop();nInputPtr+;else bResult = false;PrintStack();break;default: bResult = false; break;if (bResult = false) printf(
8、表達式有問題了n);elseprintf(表達式?jīng)]問題!n);/以上是詞法、語法分析代碼/-/以下是語義分析代碼stack state;stack value;int i=0;int size=strlen(strInput);for(i=0;i=0&strInputi=9)state.push(digit);value.push(strInputi-0);if(i!=(size-1)guiyue(state,value,strInputi+1);elseguiyue(state,value,#);else if(strInputi=+)state.push(add);else if(strI
9、nputi=*)state.push(mul);else if(strInputi=()state.push(lb);else if(strInputi=)state.push(rb);if(i!=(size-1)guiyue(state,value,strInputi+1);elseguiyue(state,value,#);/printf(Hello World!n);return 0;void guiyue(stack &state,stack &value,char nex)int next;if(nex=0&nex=9)next=digit;else if(nex=#)next=0;
10、elseswitch(nex)case +:next=add;break;case *:next=mul;break;case (:next=lb;break;case ):next=rb;break;stack state1=state;stack value1=value;int top_s1,top_s2;int top_v1;int top=state.top();bool flag=true;while(flag)switch(state.top()case digit:state.pop();state.push(F);state1=state;break;case F:if(st
11、ate.size()=3)state1.pop();top_s1=state1.top();state1.pop();top_s2=state1.top();state1.pop();if(top_s1=mul&top_s2=T)state1.push(T);state=state1;value1.pop();top_v1=value1.top();value1.pop();value1.push(top_v1*value.top();value=value1;elsestate.pop();state.push(T);state1=state;elsestate.pop();state.pu
12、sh(T);state1=state;break;case T:if(next=mul)flag=false;else if(state.size()=3)state1.pop();top_s1=state1.top();state1.pop();top_s2=state1.top();state1.pop();if(top_s1=add&top_s2=E)state1.push(E);state=state1;value1.pop();top_v1=value1.top();value1.pop();value1.push(top_v1+value.top();value=value1;elsestate.pop();state.push(E);state1=state;elsestate.pop();state.push(E);state1=state;break;case E:if(next=add|next=rb)flag=false;elseif(state.size()=1&state.top()=E)state.pop();state.push(L);printf(
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030年中國鍍硅鋼卷行業(yè)運行狀況及發(fā)展前景分析報告
- 2025-2030年中國鋰電池鋁塑膜行業(yè)運營狀況及發(fā)展前景分析報告
- 2025-2030年中國鈾資源產(chǎn)業(yè)運行動態(tài)及發(fā)展趨勢分析報告
- 2025-2030年中國輕鋼結(jié)構(gòu)行業(yè)十三五規(guī)劃及發(fā)展策略分析報告
- 2025-2030年中國航空保險行業(yè)市場深度調(diào)查及投資前景預測報告
- 2025-2030年中國羊毛衫市場十三五規(guī)劃及發(fā)展趨勢分析報告
- 2025-2030年中國稀釋劑市場運行狀況及前景趨勢分析報告
- 2025山西省安全員《C證》考試題庫
- 河北石油職業(yè)技術(shù)大學《舞弊審計》2023-2024學年第二學期期末試卷
- 2025年重慶市建筑安全員考試題庫
- 電梯控制技術(shù)PPT完整全套教學課件
- 甲狀腺旁腺分泌的激素及功能
- 中央財政成品油價格調(diào)整對漁業(yè)補助資金項目實施方案
- PFMEA模板完整版文檔
- 論生產(chǎn)安全對于家庭的重要性
- 風力發(fā)電變槳系統(tǒng)外文翻譯
- 教學能力比賽決賽 《英語》教案
- ECMO IABP完整版可編輯
- 離婚糾紛證據(jù)清單
- 【高考作文指導】用思辨來寫現(xiàn)象類作文(共39張PPT)
- GB/T 4513-2000不定形耐火材料分類
評論
0/150
提交評論