-
大小: 6KB文件類型: .cpp金幣: 1下載: 1 次發(fā)布日期: 2021-10-12
- 語(yǔ)言: C/C++
- 標(biāo)簽:
資源簡(jiǎn)介
三、設(shè)計(jì)要求
1、使用模塊化設(shè)計(jì)思想來(lái)設(shè)計(jì)該編譯器;
2、詞法分析模塊用于讀入輸入串,并將其轉(zhuǎn)換成供語(yǔ)法分析模塊使用的記號(hào)流。其中包括濾掉空格和注釋、識(shí)別常數(shù)、識(shí)別標(biāo)識(shí)符和關(guān)鍵字等功能;
3、要求在語(yǔ)法分析模塊中利用語(yǔ)法制導(dǎo)翻譯技術(shù)完成具體的中綴表達(dá)式到后綴表達(dá)式的翻譯,其中包括按前述翻譯器的規(guī)格說明構(gòu)建對(duì)應(yīng)表達(dá)式、項(xiàng)、因子的非終結(jié)符expr、term和factor的函數(shù)以及檢查記號(hào)是否匹配的函數(shù);并在不匹配時(shí)調(diào)用錯(cuò)誤處理模塊;
4、要求符號(hào)表管理模塊主要完成符號(hào)表對(duì)應(yīng)數(shù)據(jù)結(jié)構(gòu)的具體實(shí)現(xiàn)功能;
5、錯(cuò)誤處理模塊負(fù)責(zé)報(bào)告錯(cuò)誤信息及位置,并終止分析過程;
6、輸出模塊完成翻譯后所得到的后綴表達(dá)式的輸出。
四、運(yùn)行結(jié)果
1、從鍵盤輸入任意中綴表達(dá)式,如:
4 - 5 * 6 DIV 4 + 8 MOD 2
輸出相應(yīng)的后綴表達(dá)式:
456*4DIV-82MOD+
1、 若鍵盤輸入串為非中綴表達(dá)式時(shí),如:
4 !+* 5 - 6 DIV 4 + 8 MOD 2
輸出相應(yīng)語(yǔ)法錯(cuò)誤報(bào)告信息,并停止語(yǔ)法分析,如:
line 1 : compiler error !
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include
using?namespace?std;
bool?isOper(char?c)
//判斷是否為操作符
{
?if?((c==‘+‘)||(c==?‘-‘)||(c==?‘*‘)||(c==?‘/‘)||(c==?‘(‘)||(c==?‘)‘)||(c==1)||(c==2))
??return?true;
?return?false;
}
bool?isRealOper(char?c)
//判斷是否為操作符
{
?if?((c==‘+‘)||(c==?‘-‘)||(c==?‘*‘)||(c==?‘/‘)||(c==1)||(c==2))
??return?true;
?return?false;
}
bool?isHigh(char?top_opchar?InfixExp_op)
//判斷操作符的優(yōu)先級(jí)
//top_op為棧頂操作符
//InfixExp_op為當(dāng)前讀入操作符
//如果棧頂操作符優(yōu)先級(jí)高,則彈出棧頂操作符
//如果棧頂操作符優(yōu)先級(jí)低,則壓入當(dāng)前讀入操作符
{
?if?((top_op==?‘+‘)&&(InfixExp_op==?‘+‘))?return?true;
?if?((top_op==?‘+‘)&&(InfixExp_op==?‘-‘))?return?true;
?if?((top_op==?‘-‘)&&(InfixExp_op==?‘+‘))?return?true;
?if?((top_op==?‘-‘)&&(InfixExp_op==?‘-‘))?return?true;
?if?((top_op==?‘*‘)&&(InfixExp_op==?‘+‘))?return?true;
?if?((top_op==?‘*‘)&&(InfixExp_op==?‘-‘))?return?true;
?if?((top_op==?‘*‘)&&(InfixExp_op==?‘*‘))?return?true;
?if?((top_op==?‘*‘)&&(InfixExp_op==?‘/‘))?return?true;
?if?((top_op==?‘*‘)&&(InfixExp_op==?1))?return?true;
?if?((top_op==?‘*‘)&&(InfixExp_op==?2))?return?true;
?if?((top_op==?‘/‘)&&(InfixExp_op==?‘+‘))?return?true;
?if?((top_op==?‘/‘)&&(InfixExp_op==?‘-‘))?return?true;
?if?((top_op==?‘/‘)&&(InfixExp_op==?‘*‘))?return?true;
?if?((top_op==?‘/‘)&&(InfixExp_op==?‘/‘))?return?true;
?if?((top_op==?‘/‘)&&(InfixExp_op==?1))?return?true;
?if?((top_op==?‘/‘)&&(InfixExp_op==?2))?return?true;
?if?((top_op==?1)&&(InfixExp_op==?‘+‘))?return?true;
?if?((top_op==?1)&&(InfixExp_op==?‘-‘))?return?true;
?if?((top_op==?1)&&(InfixExp_op==?‘*‘))?return?true;
?if?((top_op==?1)&&(InfixExp_op==?‘/‘))?return?true;
?if?((top_op==?1)&&(InfixExp_op==?1))?return?true;
?if?((top_op==?1)&&(InfixExp_op==?2))?return?true;
?if?((top_op==?2)&&(InfixExp_op==?‘+‘))?return?true;
?if?((top_op==?2)&&(InfixExp_op==?‘-‘))?return?true;
?if?((top_op==?2)&&(InfixExp_op==?‘*‘))?return?true;
?if?((top_op==?2)&&(InfixExp_op==?‘/‘))?return?true;
?if?((top_op==?2)&&(InfixExp_op==?1))?return?true;
?if?((top_op==?2)&&(InfixExp_op==?2))?return?true;
?if?(InfixExp_op==?‘)‘)?return?true;
?return?false;
}
void?input(vector??*InfixExp)
{
?int?i?=?0j;
?
?char?c[100];
?char?d;
?cin>>d;
?while(d!=?‘#‘)
?{
??c[i]?=?d;
??cin>>d;
??i++;
?}
??c[i]=‘\0‘;??
???
???i?=?0;
???
???while(c[i]!=?‘\0‘)
???{???????
??????????if(c[i]==‘?‘)
????????????{??c[i]=c[i+1];
????????????}
??????????i++;
???}
???i?=0;
???while(c[i]!=?‘\0‘)
???{???????
??????????if(c[i]==‘D‘?&&?c[i+1]==‘I‘?&&?c[i+2]==‘V‘)
????????????{??c[i]=1;
???????????????j=i;
???????????????while(c[j]!=?‘\0‘){c[j+1]=c[j+3];j++;}
????????????}
??????????i++;
???}
???i?=?0;
???while(c[i]!=?‘\0‘)
???{???????
??????????if(c[i]==‘M‘?&&?c[i+1]==‘O‘?&&?c[i+2]==‘D‘)
????????????{??c[i]=2;
???????????????j=i;
???????????????while(c[j]!=?‘\0‘){c[j+1]=c[j+3];j++;}
????????????}
??????????i++;
???}
?
?i=0;
?
?while(c[i]!=?‘\0‘)
?{
??
評(píng)論
共有 條評(píng)論