資源簡介
編譯原理賦值語句的翻譯程序,帶實驗文檔,程序分析源程序為C語言,輸出到控制臺,帶出錯提醒

代碼片段和文件信息
#include
#include
#include
#include
#include
#define?MAX?20
char?temp[5]={‘1‘‘2‘‘3‘‘4‘‘5‘};?//用來作為四元式的臨時變量
void?move(char?bchar?*cint?x)?//從x處向前移動,并插入b;
{?int?s;
??c[x-2]=b;
??for(s=x+1;s ???c[x-1]=c[s];
}
void?change(char?a[20]char?ch[])
{?
???int?top=-1;//棧指針,初始值為-1
???int?i=0n;
???int?j=0;
???char?sta[20];//符號棧
?????while(ch[i]!=‘#‘)
?{
?if(isalnum(ch[i]))//范圍在A–Z?a–z?or?0–9
?{
?while(isalnum(ch[i]))
?{a[j]=ch[i];
??????????????j++;
??????????????i++;
?}//while
?????????????//a[j]=‘‘;
?????????????//j++;
?}?//if
???????else//如果是符號,+-*/()(:=)以=表示
???{
?????????switch(ch[i])
?{?case?‘=‘:
???case?‘+‘:
???????????case?‘-‘:?if(sta[top]==‘=‘||top==-1||sta[top]==‘(‘)
?{?top++;?
???????????????????????sta[top]=ch[i++];
?????????????????????}//if
?????????????????????else?
?{a[j]=sta[top];
??????????????????????j++;
??????????????????????top--;?
?}//else
?????????????????????break;
??????????case?‘*‘:
??????????case?‘/‘:if(sta[top]==‘*‘||sta[top]==‘/‘)
???{a[j]=sta[top];//出棧
????????????????????j++;
????????????????????top--;
???}//if
???????????????????else
???{
??????????????????? top++;
????????????????????sta[top]=ch[i];//入棧
????????????????????i++;
???}//else
???????????????????break?;
???????case?‘(‘:?top++;?
?????????????????sta[top]=ch[i];
?????????????????i++;break;
???????case?‘)‘:?if(sta[top]==‘(‘)
?{?top--;
?????????????????????i++;
?}//if
???????????else?
???{a[j]=sta[top];
???????????top--;
???????????j++;
???}//else
???????????break;
?????default:?cout<<“?你輸入的有錯誤!“< ??????????ch[i]=‘#‘;
??????????j=0;
???????????break;
?}//switch
???}//else
?}//while
???while(top!=-1)//把sta中剩余的符號賦到a中
???{
???a[j]=sta[top];j++;
????????top--;
???}//while
???a[j]=‘#‘;
??cout<<“四元式為:?“< ???n=0;
???j=0;
??while(a[n]!=‘#‘)
??{??
??if(!isalnum(a[n]))
??{??if(a[n]==‘=‘)
??????cout< ??<<‘-‘<<‘‘< ??else??{cout< ??< ????move(temp[j]an);
j++;
n=-1;}
??}//if
?????n++;
??}//while
?}//change
void?main()
{
static?int?i=0;
static?int?j=0;
char?ch[MAX]s[MAX];
cout<<“請輸入中綴表達式(以#作為結束符):?“;
cin>>ch;
change(sch);
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????2571??2008-01-05?10:18??翻譯程序\translation.cpp
?????文件???????64512??2008-10-04?17:01??翻譯程序\賦值語句翻譯.doc
?????目錄???????????0??2015-11-24?16:27??翻譯程序\
評論
共有 條評論