编译原理实验三-自下而上语法分析及语义分析xWord格式.doc
- 文档编号:979857
- 上传时间:2023-04-29
- 格式:DOC
- 页数:17
- 大小:523.50KB
编译原理实验三-自下而上语法分析及语义分析xWord格式.doc
《编译原理实验三-自下而上语法分析及语义分析xWord格式.doc》由会员分享,可在线阅读,更多相关《编译原理实验三-自下而上语法分析及语义分析xWord格式.doc(17页珍藏版)》请在冰点文库上搜索。
(8)F->
i
状态
ACTION(动作)
GOTO(转换)
+
-
*
/
(
)
#
E
S5
S4
1
2
3
S6
S12
acc
R3
S7
S13
R6
4
8
5
R8
6
9
7
10
R12
S11
R1
R4
11
R7
12
14
13
15
R2
R5
五、处理程序例和处理结果例
示例1:
20133191*(20133191+3191)+3191#
六、源代码
【cifa.h】
//cifa.h
#include<
string>
usingnamespacestd;
//单词结构定义
structWordType{
intcode;
stringpro;
};
//函数声明
WordTypeget_w();
voidgetch();
voidgetBC();
boolisLetter();
boolisDigit();
voidretract();
intReserve(stringstr);
stringconcat(stringstr);
【Table.action.h】
//table_action.h
classTable_action
{
introw_num,line_num;
intlineName[8];
stringtableData[16][8];
public:
Table_action()
{
row_num=16;
line_num=8;
lineName[0]=30;
lineName[1]=7;
lineName[2]=13;
lineName[3]=8;
lineName[4]=14;
lineName[5]=1;
lineName[6]=2;
lineName[7]=15;
lineName[8]=0;
for(intm=0;
m<
row_num;
m++)
for(intn=0;
n<
line_num;
n++)
tableData[m][n]="
"
;
tableData[0][0]="
S5"
tableData[0][5]="
S4"
tableData[1][1]="
S6"
tableData[1][2]="
S12"
tableData[1][7]="
acc"
tableData[2][1]="
R3"
tableData[2][2]="
tableData[2][3]="
S7"
tableData[2][4]="
S13"
tableData[2][6]="
tableData[2][7]="
tableData[3][1]="
R6"
tableData[3][2]="
tableData[3][3]="
tableData[3][4]="
tableData[3][6]="
tableData[3][7]="
tableData[4][0]="
tableData[4][5]="
tableData[5][1]="
R8"
tableData[5][2]="
tableData[5][3]="
tableData[5][4]="
tableData[5][6]="
tableData[5][7]="
tableData[6][0]="
tableData[6][5]="
tableData[7][0]="
tableData[7][5]="
tableData[8][1]="
tableData[8][2]="
tableData[8][6]="
S11"
tableData[9][1]="
R1"
tableData[9][2]="
tableData[9][3]="
tableData[9][4]="
tableData[9][6]="
tableData[9][7]="
tableData[10][1]="
R4"
tableData[10][2]="
tableData[10][3]="
tableData[10][4]="
tableData[10][6]="
tableData[10][7]="
tableData[11][1]="
R7"
tableData[11][2]="
tableData[11][3]="
tableData[11][4]="
tableData[11][6]="
tableData[11][7]="
tableData[12][0]="
tableData[12][5]="
tableData[13][0]="
tableData[13][5]="
tableData[14][1]="
R2"
tableData[14][2]="
tableData[14][3]="
tableData[14][4]="
tableData[14][6]="
tableData[14][7]="
tableData[15][1]="
R5"
tableData[15][2]="
tableData[15][3]="
tableData[15][4]="
tableData[15][5]="
tableData[15][6]="
tableData[15][7]="
}
stringgetCell(introwN,intlineN)
introw=rowN;
intline=getLineNumber(lineN);
if(row>
=0&
&
row<
row_num&
line>
line<
=line_num)
returntableData[row][line];
else
return"
intgetLineNumber(intlineN)
for(inti=0;
i<
i++)
if(lineName[i]==lineN)
returni;
return-1;
【Table_go.h】
//table_go.h
classTable_go
//行数、列数
stringlineName[3];
inttableData[16][3];
Table_go(){
line_num=3;
lineName[0]="
E"
lineName[1]="
T"
lineName[2]="
F"
tableData[m][n]=0;
tableData[0][0]=1;
tableData[0][1]=2;
tableData[0][2]=3;
tableData[4][0]=8;
tableData[4][1]=2;
tableData[4][2]=3;
tableData[6][1]=9;
tableData[6][2]=3;
tableData[7][2]=10;
tableData[12][1]=14;
tableData[12][2]=3;
tableData[13][2]=15;
intgetCell(introwN,stringlineNa)
intline=getLineNumber(lineNa);
else
intgetLineNumber(stringlineNa)
if(lineName[i]==lineNa)
【Stack_num.h】
classStack_num
inti;
//栈顶标记
int*data;
//栈结构
Stack_num()//构造函数
{
data=newint[100];
i=-1;
intpush(intm)//进栈操作
{
i++;
data[i]=m;
returni;
intpop()//出栈操作
i--;
returndata[i+1];
intgetTop()//返回栈顶
returndata[i];
~Stack_num()//析构函数
delete[]data;
inttopNumber()
voidoutStack()
=i;
cout<
<
data[m];
【Stack_str.h】
classStack_str
string*data;
Stack_str()//构造函数
data=newstring[50];
intpush(stringm)//进栈操作
data[i]="
stringgetTop()//返回栈顶
~Stack_str()//析构函数
【cifa.cpp】
//cifa.cpp
iostream>
#include"
cifa.h"
//关键字表和对应的编码
stringcodestring[10]={"
main"
"
int"
if"
then"
else"
return"
void"
cout"
endl"
intcodebook[10]={26,21,22,23,24,25,27,28,29};
//全局变量
charch;
intflag=0;
/*//主函数
intmain()
WordTypeword;
cout<
请输入源程序序列:
word=get_w();
while(word.pro!
="
#"
)//#为自己设置的结束标志
cout<
("
word.code<
,"
“"
word.pro<
”"
)"
endl;
word=get_w();
};
return0;
}*/
WordTypeget_w()
stringstr="
WordTypewordtmp;
getch();
//读一个字符
getBC();
//去掉空白符
if(isLetter()){//以字母开头
while(isLetter()||isDigit()){
str=concat(str);
getch();
}
retract();
code=Reserve(str);
if(code==-1){wordtmp.code=0;
wordtmp.pro=str;
}//不是关键字
else{wordtmp.code=code;
}//是关键字
elseif(isDigit()){//以数字开头
while(isDigit()){
wordtmp.code=30;
wordtmp.pro=str;
elseif(ch=='
('
){wordtmp.code=1;
wordtmp.pro="
}
elseif(ch=='
)'
){wordtmp.code=2;
{'
){wordtmp.code=3;
{"
}'
){wordtmp.code=4;
}"
'
){wordtmp.code=5;
='
){wordtmp.code=6;
+'
){wordtmp.code=7;
+"
*'
){wordtmp.code=8;
*"
>
){wordtmp.code=9;
){wordtmp.code=10;
'
){wordtmp.code=11;
\'
){wordtmp.code=12;
-'
){wordtmp.code=13;
-"
elseif(ch=='
/'
){wordtmp.code=14;
/"
#'
){wordtmp.code=15;
|'
){wordtmp.code=16;
|"
else{wordtmp.code=100;
wordtmp.pro=ch;
returnwordtmp;
voidgetch(){
if(flag==0)//没有回退的字符
ch=getchar();
else//有回退字符,用回退字符,并设置标志
flag=0;
voidgetBC(){
while(ch=='
'
||ch=='
\t'
\n'
boolisLetter(){
if(ch>
a'
ch<
z'
||ch>
A'
Z'
returntrue;
else
returnfalse;
boolisDigit(){
0'
9'
stringconcat(stringstr){
returnstr+ch;
voidretract(){
flag=1;
intReserve(stringstr){
for(i=0;
=8;
i++){
if(codestring[i]==str)//是某个关键字,返回对应的编码
returncodebook[i];
if(i==9)//不是关键字
return-1;
【LR.cpp】
cstdlib>
stack_num.h"
stack_str.h"
table_action.h"
table_go.h"
voidprocess(){
intstepNum=1;
inttopStat;
Stack_numstatusSTK;
//状态栈
Stack_strsymbolSTK;
//符号栈
Stack_numvalueSTK;
//值栈
WordTypeword;
Table_actionactionTAB;
//行为表
Table_gogoTAB;
//转向表
请输入源程序,以#结束:
//总控程序初始化操作
symbolSTK.push("
);
statusSTK.push(0);
valueSTK.push(0);
步骤\t状态栈\t符号栈\t值栈\t当前词\t动作\t转向"
//分析
while
(1)
topStat=statusSTK.getTop();
//当前状态栈顶
stringact=actionTAB.getCell(topStat,word.code);
//根据状态栈顶和当前单词查到的动作
//输出
stepNum++<
\t"
statusSTK.outStack();
cout<
symbolSTK.outStack();
valueSTK.outStack();
cout<
//行为为“acc”,且当前处理的单词为#,且状态栈里就两个状态
//说明正常分析结束
if(act=="
word.pro=="
statusSTK.topNumber()==1)
{
act<
分析成功!
结果为:
valueSTK.getTop()<
return;
//读到act表里标记为错误的单元格
elseif
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 实验 自下而上 语法分析 语义 分析