資源簡介
三、設計要求
1、使用模塊化設計思想來設計該編譯器;
2、詞法分析模塊用于讀入輸入串,并將其轉換成供語法分析模塊使用的記號流。其中包括濾掉空格和注釋、識別常數、識別標識符和關鍵字等功能;
3、要求在語法分析模塊中利用語法制導翻譯技術完成具體的中綴表達式到后綴表達式的翻譯,其中包括按前述翻譯器的規格說明構建對應表達式、項、因子的非終結符expr、term和factor的函數以及檢查記號是否匹配的函數;并在不匹配時調用錯誤處理模塊;
4、要求符號表管理模塊主要完成符號表對應數據結構的具體實現功能;
5、錯誤處理模塊負責報告錯誤信息及位置,并終止分析過程;
6、輸出模塊完成翻譯后所得到的后綴表達式的輸出。
四、運行結果
1、從鍵盤輸入任意中綴表達式,如:
4 - 5 * 6 DIV 4 + 8 MOD 2
輸出相應的后綴表達式:
456*4DIV-82MOD+
1、 若鍵盤輸入串為非中綴表達式時,如:
4 !+* 5 - 6 DIV 4 + 8 MOD 2
輸出相應語法錯誤報告信息,并停止語法分析,如:
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)
//判斷操作符的優先級
//top_op為棧頂操作符
//InfixExp_op為當前讀入操作符
//如果棧頂操作符優先級高,則彈出棧頂操作符
//如果棧頂操作符優先級低,則壓入當前讀入操作符
{
?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‘)
?{
??
- 上一篇:SegY地震體數據可視化分析工具 C++庫
- 下一篇:c++源碼C-語法分析器源代碼
評論
共有 條評論