编译原理课程设计编译课设Word下载.docx
- 文档编号:19512244
- 上传时间:2023-01-07
- 格式:DOCX
- 页数:17
- 大小:55.77KB
编译原理课程设计编译课设Word下载.docx
《编译原理课程设计编译课设Word下载.docx》由会员分享,可在线阅读,更多相关《编译原理课程设计编译课设Word下载.docx(17页珍藏版)》请在冰豆网上搜索。
,'
L'
P'
S'
E'
G'
T'
R'
F'
Q'
\0'
charVT[15]={'
i'
='
<
'
>
+'
-'
*'
/'
('
)'
d'
w'
;
#'
charp[18][6]={"
dLwS\0"
"
SP\0"
\0"
iQE\0"
TG\0"
+TG\0"
-TG\0"
FR\0"
"
*FR\0"
/FR\0"
(E)\0"
i\0"
=\0"
charstack[MAX];
charqueue[MAX];
intsp,front;
intM[10][14]={{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,-1,-1,-1},{1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,3,2,-1},{4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{5,-1,-1,-1,-1,-1,-1,-1,5,-1,-1,-1,-1,-1},{-1,-1,-1,-1,6,7,-1,-1,-1,-1,-1,8,8,8},{9,-1,-1,-1,-1,-1,-1,-1,9,-1,-1,-1,-1,-1},{-1,-1,-1,-1,12,12,10,11,-1,-1,-1,12,12,12},
{14,-1,-1,-1,-1,-1,-1,-1,13,-1,-1,-1,-1,-1},{-1,15,16,17,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
intf=0;
intcount=0;
intc=0;
chararr_i[MAX];
charvar[MAX];
//表À
格?
管¨
¹
理¤
inttd[MAX];
//输º
出?
产¨
²
生¦
式º
序¨
列¢
D
intt=0;
intopd=-1;
intopr=-1;
intid=0;
//intptr[MAX];
intd=0;
chararr[MAX][4];
//存ä
放¤
待ä
y输º
的Ì
四?
元a式º
//charkeyword[2][7]={"
do\0"
while\0"
boolIsCharInStr(charc,chars[])
for(inti=0;
s[i]!
i++)
{
if(s[i]==c)
returntrue;
}
returnfalse;
}
intGetIndex(chars[],charc)
if(c==s[i])
returni;
return-1;
//
stringGetSubString(chars[],intfrom,intto)
stringst;
for(inti=from;
i<
to;
st+=s[i];
returnst;
/*******************************************
翻¤
-译°
赋3值¦
Ì
达ä
********************************************/
voidtranslatefuzhi(char*sInput)
//栈?
结¨
¢
构1的Ì
初?
始º
化¡
¥
charStack[STR_LEN]={0};
intindex=0;
inttop=1;
Stack[0]='
list<
char>
rPolish;
Num;
intbegin=0;
while(sInput[begin]!
)
if(IsCharInStr(sInput[begin],sTable))
{
cout<
Equalexpressionisillegal!
endl;
return;
}
begin++;
Num.push_back(GetSubString(sInput,0,begin));
intnow=begin+1;
intbefore=begin;
rPolish.push_back('
);
while(sInput[now]!
if(IsCharInStr(sInput[now],sTable))
chartemp[3]={0};
if(now-before>
1)
{
temp[0]='
temp[1]=sInput[now];
Num.push_back(GetSubString(sInput,before+1,now));
}
else
temp[0]=sInput[now];
inti=0;
while(temp[i]!
=0)
intleft=GetIndex(sTable,Stack[index]);
intright=GetIndex(sTable,temp[i]);
switch(ShipTable[left][right])
{
case-1:
//移°
入¨
Stack[top++]=temp[i];
index=top-1;
i++;
break;
case0:
//脱ª
括¤
¡
§
号?
if(Stack[index]=='
{
Stack[top++]='
Stack[index]='
top=index+1;
index--;
}
else//规?
约?
if(top!
=2)
{
return;
}
rPolish.push_back('
list<
:
iteratoriter;
iteratorsiter=Num.begin();
stringfuzhi[256];
intd=0,m;
for(iter=rPolish.begin();
iter!
=rPolish.end();
iter++)
if(*iter=='
{
fuzhi[d++]=*siter;
siter++;
}
else
fuzhi[d++]=*iter;
m=d;
stringop1,op2;
intflag,xiabiao=0;
for(d=0;
d<
m;
d++)
if(fuzhi[d]=="
+"
||fuzhi[d]=="
-"
*"
/"
="
op2="
"
op1="
for(flag=d-1;
flag>
=0;
flag--)
{
if((fuzhi[flag]!
)&
&
(op1=="
))
{
if(op2=="
{
op2=fuzhi[flag];
fuzhi[flag]="
}
else
op1=fuzhi[flag];
}
}
cout<
("
fuzhi[d]<
op1<
op2<
T"
xiabiao<
)"
fuzhi[d]="
fuzhi[d]+=(xiabiao+'
0'
xiabiao++;
//cout<
fuzhi[d];
return;
case1:
//归¨
¦
else
rPolish.push_back(Stack[index]);
Stack[index-1]='
top=index;
index-=2;
default:
return;
}
before=now;
now++;
intlen(charstr[]){
inti=0;
while(str[i]!
)i++;
returni;
intindex(charch,charstr[]){
){
if(ch!
=str[i])i++;
elsebreak;
if(str[i]=='
)return-1;
voiderr(intn){
if(n==1)cout<
字Á
符¤
不?
匹£
¤
配?
elseif(n==2)cout<
没?
有®
D出?
现?
在¨
中D"
elseif(n==3)cout<
D找¨
到Ì
合?
适º
º
候¨
选?
elsecout<
该?
句?
子Á
®
是º
文?
法¤
语®
言?
!
ê
voidprint(){
cout<
if(count<
10)cout<
count<
inti;
for(i=0;
=sp;
i++)cout<
stack[i];
for(;
=20;
front;
queue[i]!
queue[i];
intprintguiyue(char*zh)
intj=0;
步?
骤¨
符¤
栈?
输º
串ä
动¡
作Á
cout<
(0)#"
for(j=0;
j<
zhlen;
j++){cout<
zh[j];
}cout<
移°
进?
(1)#d"
for(j=1;
(2)#d"
zh[1]<
for(j=2;
(3)#d"
zh[2]<
for(j=3;
归¨
(4)#d"
F"
(5)#d"
F"
zh[3]<
for(j=4;
(6)#d"
zh[4]<
for(j=5;
(7)#d"
T"
(8)#d"
zh[5]<
for(j=6;
(9)#d"
FR"
(10)#d"
FR;
for(j=7;
(11)#dG"
(12)#dGW"
for(j=8;
(13)#dGW"
zh[8]<
for(j=9;
(14)#dGW"
zh[9]<
for(j=10;
(15)#dGW"
(16)#dGW"
zh[10]<
for(j=11;
(17)#dGWE"
(18)#S"
接¨
受º
return0;
voidsemantic(){
if(VT[opr]=='
){arr[d][0]='
arr[d][1]=arr_i[opd];
arr[d][2]=id;
arr[d][3]='
--'
id++;
elseif(opr==-2){arr[d][0]='
arr[d][1]=id-1;
arr[d][2]=arr_i[opd];
else{arr[d][0]=VT[opr];
if(VT[opr]!
VT[opr]!
)arr[d][3]=id-1;
elsearr[d][3]=id+1;
d++;
voidsyntax(){//语®
分¤
析?
intn;
count++;
print();
X=stack[sp];
a=queue[front];
if(X=='
a=='
)f=4;
if(X<
A'
||X>
Z'
if(X==a){
sp--;
front++;
if(a!
if(a!
a!
){opr=index(a,VT);
semantic();
elseif(a=='
||a=='
){opr=-2;
cout<
\t'
\'
a<
else{
opd=c;
arr_i[c++]<
elsef=1;
else{
inttx=index(X,VN);
intta=index(a,VT);
n=M[tx][ta];
td[t++]=M[tx][ta];
if(ta==-1){f=2;
}
elseif(n==-1)f=3;
else{
X<
->
if(len(p[n])!
=0){
for(inti=len(p[n])-1;
i>
i--){stack[++sp]=p[n][i];
p[n][len(p[n])-1-i];
elsecout<
空?
if(f==0)syntax();
else{td[t]='
-1'
err(f);
voidlexical(){//词ä
inti,j,d;
charch;
j=d=0;
var[i]!
i++){
ch=var[i];
if(ch=='
var[i+1]=='
o'
){cout<
do"
keword"
queue[j++]='
i+=1;
elseif(ch=='
){ch=var[i+1];
h'
){ch=var[i+2];
){ch=var[i+3];
l'
){ch=var[i+4];
e'
){ch=var[i+5];
}}}}
cout<
while"
keyword"
i+=4;
elseif(index(ch,VT)<
if(ch!
{'
ch!
}'
ch<
variable:
i["
d++<
]"
arr_i[d-1]=ch;
queue[
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 课程设计