賦值語(yǔ)句解釋程序設(shè)計(jì)_第1頁(yè)
賦值語(yǔ)句解釋程序設(shè)計(jì)_第2頁(yè)
賦值語(yǔ)句解釋程序設(shè)計(jì)_第3頁(yè)
賦值語(yǔ)句解釋程序設(shè)計(jì)_第4頁(yè)
賦值語(yǔ)句解釋程序設(shè)計(jì)_第5頁(yè)
已閱讀5頁(yè),還剩13頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、 編譯原理課程設(shè)計(jì)報(bào)告二 姓名:李 敏 學(xué)號(hào):20132017 專業(yè):計(jì)算機(jī)科學(xué)與技術(shù) 班級(jí):2013級(jí)二班 西南交通大學(xué)信息科學(xué)與技術(shù)學(xué)院2015年 6月 13日 (二)賦值語(yǔ)句的解釋程序設(shè)計(jì)一、 設(shè)計(jì)題目賦值語(yǔ)句的解釋程序設(shè)計(jì)二、設(shè)計(jì)目的了解掌握算符優(yōu)先分析的基本方法、內(nèi)容以及基本程序?qū)崿F(xiàn);學(xué)會(huì)科學(xué)思考并解決問題,提高程序設(shè)計(jì)能力。更進(jìn)一步的去體會(huì)這種簡(jiǎn)單直觀、廣為使用的自下而上分析法。三、設(shè)計(jì)環(huán)境: VS2013,Windows系統(tǒng)四、需求分析:用算符優(yōu)先分析方法設(shè)計(jì)一個(gè)分析解釋程序,對(duì)輸入的賦值語(yǔ)句、輸出語(yǔ)句、清除語(yǔ)句進(jìn)行詞法分析、語(yǔ)法分析、表達(dá)式求值并存儲(chǔ)于指定變量中;若存在錯(cuò)誤,提

2、示錯(cuò)誤相關(guān)信息。文法表示: Sv=E|E?|clear EE+T|E-T|T TT*F|T/F|F F (E)|v|c 五、概要設(shè)計(jì): (1)在設(shè)計(jì)的過程中,設(shè)計(jì)單詞的種別碼 單詞符號(hào)種別碼=1?2+3-4*5/6(7)8v9c10clear11#12 (2)設(shè)計(jì)優(yōu)先關(guān)系表,歸約棧關(guān)系+ - * / ( ) v c ? clear #+-*/()vc?Clear#> > < < < > < < >> > < < < > < < >> > > > < >

3、 < < >> > > > < > < < >< < < < < = < < > > > > > > > >> > > > > > >> > > > > > > < < > < < < < < < < < < =六、詳細(xì)設(shè)計(jì):/ OperatorPrecedencePrase.cp

4、p : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。/#include "stdafx.h"#include "stdafx.h"#include "iostream"#include "fstream"#include "sstream"#include "string"#include "iomanip"using namespace std;void InputCheck(string &ch)/判斷輸入的表達(dá)式是否規(guī)范int len = ch.size()

5、;int i;int flag = 1;for (i = 0; i < len; i+)if (chlen - 1 != ')' && (chlen - 1<'A'&&chlen - 1>'Z'&&chlen - 1<'a'&&chlen - 1>'z'&&chlen - 1<'0'&&chlen - 1>'9')flag = 0; break;i

6、f (chi >= 'A'&&chi <= 'Z') | (chi >= 'a'&&chi <= 'z') | (chi >= '0'&&chi <= '9')if (i = 0)else if (chi - 1 = '+' | chi - 1 = '-' | chi - 1 = '*' | chi - 1 = '/' | chi - 1 = '

7、(')elseflag = 0;break;switch (chi)case'+':case'-':case'*':case'/':if (chi - 1 >= 'A'&&chi - 1 <= 'Z') | (chi - 1 >= 'a'&&chi - 1 <= 'z') | (chi - 1 >= '0'&&chi - 1 <= '9') |

8、chi - 1 = ')')elseflag = 0;break;case'(':if (chi - 1 = '+' | chi - 1 = '-' | chi - 1 = '*' | chi - 1 = '/')elseflag = 0;break;case')':if (chi - 1 >= 'A'&&chi - 1 <= 'Z') | (chi - 1 >= 'a'&&chi -

9、1 <= 'z') | (chi - 1 >= '0'&&chi - 1 <= '9')elseflag = 0;break;default:flag = 1; break;if (flag = 1)cout << "所輸入表達(dá)式規(guī)范!" << endl;elsecout << "所輸入表達(dá)式不規(guī)范!" << endl; int r;int r1;int k;char q;char a;char first1010;int f

10、flag10 = 0 ;char last1010;int lflag10 = 0 ;char st2020;char data2020;char s100;char lable20;char s12010;int zhongjiefu(char c)/判斷字符c是否是終結(jié)符int i;for (i = 0; lablei != '0' i+)if (c = lablei)return 1;return 0;int xiabiao(char c)/求字符c在算符優(yōu)先關(guān)系表中的下標(biāo)int i;for (i = 0; lablei != '0' i+)if (c =

11、 lablei)return i;return -1;void firstvt(char c)/求FIRSTVT集int i, j, k, m, n;for (i = 0; i<r; i+)if (sti0 = c)break;if (fflagi = 0)n = firsti0 + 1;m = 0;doif (m = 2 | stim = '|')if (zhongjiefu(stim + 1)firstin = stim + 1;n+;elseif (zhongjiefu(stim + 2)firstin = stim + 2;n+;if (stim + 1 != c

12、)firstvt(stim + 1);for (j = 0; j<r; j+)if (stj0 = stim + 1)break;for (k = 0; k<firstj0; k+)int t;for (t = 0; t<n; t+)if (firstit = firstjk + 1)break;if (t = n)firstin = firstjk + 1;n+;m+; while (stim != '0');firstin = '0'firsti0 = -n;fflagi = 1;void lastvt(char c)/求LASTVT集in

13、t i, j, k, m, n;for (i = 0; i<r; i+)if (sti0 = c)break;if (lflagi = 0)n = lasti0 + 1;m = 0;doif (stim + 1 = '0' | stim + 1 = '|')if (zhongjiefu(stim)lastin = stim;n+;elseif (zhongjiefu(stim - 1)lastin = stim - 1;n+;if (stim != c)lastvt(stim);for (j = 0; j<r; j+)if (stj0 = stim)

14、break;for (k = 0; k<lastj0; k+)int t;for (t = 0; t<n; t+)if (lastit = lastjk + 1)break;if (t = n)lastin = lastjk + 1;n+;m+; while (stim != '0');lastin = '0'lasti0 = -n;lflagi = 1;void PreTable()char text2010;char s12010;int i, j, k, t, l, x = 0, y = 0;int m, n;x = 0;for (i = 0;

15、 i<r; i+)firstvt(sti0);lastvt(sti0);for (i = 0; i<r; i+)textxy = sti0;y+;for (j = 1; stij != '0' j+)if (stij = '|')textxy = '0'x+;y = 0;textxy = sti0;y+;textxy+ = '-'textxy+ = '>'elsetextxy = stij;y+;textxy = '0'x+;y = 0;r1 = x;/*輸出轉(zhuǎn)化后的文法規(guī)則串*/

16、printf("轉(zhuǎn)化后的文法為:n");for (i = 0; i<x; i+) printf("%sn", texti);for (i = 0; i<x; i+)s1i0 = texti0;for (j = 3, l = 1; textij != '0' j+, l+)s1il = textij;s1il = '0'for (i = 0; i<x; i+)for (j = 1; textij + 1 != '0' j+)if (zhongjiefu(textij) &&

17、zhongjiefu(textij + 1)m = xiabiao(textij);n = xiabiao(textij + 1);datamn = '='if (textij + 2 != '0'&&zhongjiefu(textij) && zhongjiefu(textij + 2) && !zhongjiefu(textij + 1)m = xiabiao(textij);n = xiabiao(textij + 2);datamn = '='if (zhongjiefu(textij) &

18、amp;& !zhongjiefu(textij + 1)for (k = 0; k<r; k+)if (stk0 = textij + 1)break;m = xiabiao(textij);for (t = 0; t<firstk0; t+)n = xiabiao(firstkt + 1);datamn = '<'if (!zhongjiefu(textij) && zhongjiefu(textij + 1)for (k = 0; k<r; k+)if (stk0 = textij)break;n = xiabiao(tex

19、tij + 1);for (t = 0; t<lastk0; t+)m = xiabiao(lastkt + 1);datamn = '>'m = xiabiao('#');for (t = 0; t<first00; t+)n = xiabiao(first0t + 1);datamn = '<'n = xiabiao('#');for (t = 0; t<last00; t+)m = xiabiao(last0t + 1);datamn = '>'datann = '

20、='void output(int j, int k, char *s)int n = 0;int i;for (i = j; i <= k; i+)printf("%c", si);n+;for (; n<15; n+)printf(" ");int deal(string input)char string2010;int i, j;int x, y;int z;int k = 1;sk = '#'for (i = 0; inputi != '0' i+);z = i-;i = 0;while (a

21、 = inputi) != '0')if (zhongjiefu(sk)j = k;elsej = k - 1;x = xiabiao(sj);y = xiabiao(a);if (dataxy = '>')doq = sj;if (zhongjiefu(sj - 1)j = j - 1;else j = j - 2;x = xiabiao(sj);y = xiabiao(q);while (dataxy != '<');int m, n, N;for (m = j + 1; m <= k; m+)for (N = 0; N&l

22、t;r1; N+)for (n = 1; stringNn != '0' n+)if (!zhongjiefu(sm) && !zhongjiefu(stringNn)if (zhongjiefu(sm + 1) && zhongjiefu(stringNn + 1)&& sm + 1 = stringNn + 1)sj + 1 = stringN0;break;elseif (zhongjiefu(sm)if (sm = stringNn)sj + 1 = stringN0;break;k = j + 1;if (k = 2 &

23、amp;& a = '#')printf("輸入串符合文法的定義!n");return 1;elseif (dataxy = '<' | dataxy = '=')k+;sk = a;i+;elseprintf("n輸入串不符合文法的定義!n");return 0;return 0;void yufaAnalyzer(string input)/語(yǔ)法分析優(yōu)先關(guān)系int i,j; char last1010;char first1010;cout << "請(qǐng)輸入語(yǔ)法產(chǎn)生式的

24、個(gè)數(shù):"cin >> r; cout << endl;cout << "語(yǔ)法產(chǎn)生式如下:" << endl;for (int i = 0; i < r; i+)cin >> sti;firsti0 = 0;lasti0 = 0;for (i = 0; i<r; i+)for (j = 0; stij != '0' j+)if (stij<'A' | stij>'Z') && stij != '-'&am

25、p;&stij != '>'&&stij != '|')lablek+ = stij;lablek = '#'lablek + 1 = '0'PreTable();printf("算符優(yōu)先分析表如下:n");for (i = 0; lablei != '0' i+)printf("t%c", lablei);printf("n");for (i = 0; i<k + 1; i+)printf("%ct"

26、;, lablei);for (j = 0; j<k + 1; j+)printf("%ct", dataij);printf("n");void GraAnalyzer(string &ch)/詞法分析int len1 = ch.size();int i, j;string str;for (i = 0; i < len1; i+)/*判斷運(yùn)算符*/if (chi = '+' | chi = '-' | chi = '=' | chi = '*' | chi = 

27、9;/')switch (chi)case '+':cout << "3" << setw(10) << "+" << endl; break;case '-':cout << "4" << setw(10) << "-" << endl; break;case '*':cout << "5" << setw(10) &l

28、t;< "*" << endl; break;case '/':cout << "6" << setw(10) << "/" << endl; break;case '=':cout << "1" << setw(10) << "=" << endl; break;default:cout << "01:" <&l

29、t; setw(10) << chi << endl; break;/*判斷界符*/else if (chi = ',' | chi = '(' | chi = ')' | chi = '<' | chi = '>' | chi = '' | chi = ''| chi = '' | chi = '' | chi = '' | chi = '"' | chi = '.

30、' | chi = 12 | chi = '?' | chi = '#')switch (chi)case'(':cout << "7" << setw(10) << "(" << endl; break;case')':cout << "8" << setw(10) << ")" << endl; break;case'?':cou

31、t << "2" << setw(10) << "?" << endl; break;case'#':cout << "12" << setw(10) << "#" << endl; break;default:cout << "02:" << setw(10) << chi << endl; break;/*判斷標(biāo)識(shí)符與關(guān)鍵字*/els

32、e if (chi >= 'A'&&chi <= 'Z') | (chi >= 'a'&&chi <= 'z') | (chi >= '0'&&chi <= '9') | (chi = '_')if (chi >= 'A'&&chi <= 'Z') | (chi >= 'a'&&chi <= 

33、9;z')cout << "9" << setw(10) << chi << endl;else if (chi >= '0'&&chi <= '9')cout << "10" << setw(10) << chi << endl;str = chi;for (j = i + 1; j < len1; j+)if (chj >= 'A'&&chj &

34、lt;= 'Z') | (chj >= 'a'&&chj <= 'z') | (chj >= '0'&&chj <= '9') | (chj = '_')str += chj;elsebreak;i = j;/*判斷關(guān)鍵字*/if (str = "while" | str = "if" | str = "do" | str = "return" | str = "in

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論