编译原理实验一词法分析器Word文档格式.docx
- 文档编号:17947190
- 上传时间:2022-12-12
- 格式:DOCX
- 页数:17
- 大小:71.66KB
编译原理实验一词法分析器Word文档格式.docx
《编译原理实验一词法分析器Word文档格式.docx》由会员分享,可在线阅读,更多相关《编译原理实验一词法分析器Word文档格式.docx(17页珍藏版)》请在冰豆网上搜索。
识别非保留字的一般标识符(有下划线、字符、数字,且第一个字符不能是数字)。
识别数字序列(整数和小数);
识别:
=,<
=,>
=之类的特殊符号以及;
,(,)等界符。
2、相关过程(函数):
Scanner()词法扫描程序,提取标识符并填入display表中
3、这个小语言有顺序结构的语句
4、这个小语言能表达分支结构的语句
5、这个小语言能够输出结果;
三、算法设计
单词种别码设计:
状态
种别编码
种类
解释
初态
1
2
关键字
3
变量标识符
由字母下划线数字组成,且第一位不能是数字
4
读入了数字
5
整数
6
小数
7
小数点个数出错,大于1
8
读入了<
9
双目运算<
>
、<
=
10
单目运算<
11
读入了>
12
双目运算>
13
单目运算>
14
单目运算+、—、*、/、=、#
用default实现
15
读入了:
16
双目运算:
17
界符(、)、,、;
、—、[、]、:
、{、}、‘’
用变量errorflag实现
四、程序源代码
1使用环境:
vc++6.0,win8;
2源代码:
#include<
iostream>
fstream>
string>
math.h>
ctype.h>
cstdlib>
usingnamespacestd;
#defineMax655
#defineWordMaxNum256//变量最大个数
#defineDigitNum256//常量最大个数
#defineMaxKeyWord32//关键字数量
#defineMaxOptANum8//运算符最大个数
#defineMaxOptBNum4//运算符最大个数
#defineMaxEndNum11//界符最大个数
typedefstructDisplayTable
{
intIndex;
//标识符所在表的下标
inttype;
//标识符的类型
intline;
//标识符所在表的行数
charsymbol[20];
//标识符所在表的名称
}Table;
intTableNum=0;
//display表的下标
charWord[WordMaxNum][20];
//标识符表
charDigit[WordMaxNum][20];
//数字表
intWordNum=0;
//变量表的下标
intDigNum=0;
//常量表的下标
boolerrorFlag=0;
//错误标志
constchar*constKeyWord[MaxKeyWord]={"
and"
"
array"
begin"
case"
"
char"
"
constant"
do"
else"
end"
false"
for"
if"
input"
integer"
not"
of"
or"
output"
packed"
procedure"
program"
read"
real"
repeat"
set"
then"
to"
type"
until"
var"
while"
with"
prn"
};
//关键字
constcharOptA[]={'
+'
'
-'
*'
/'
='
#'
<
'
//单目运算
constchar*OptB[]={"
="
:
"
//双目运算符
constcharEnd[]={'
('
'
)'
'
;
.'
['
]'
{'
}'
//界符
voiderror(charstr[20],intnLine,interrorType)
{
cout<
\nError:
switch(errorType)
case1:
cout<
第"
<
nLine-1<
行"
str<
变量的长度超过限制!
\n"
errorFlag=1;
break;
case2:
小数点错误!
case3:
常量的长度超过限制!
}
}//error
voidScanner(charch[],intchLen,Tabletable[Max],intnLine)
intchIndex=0;
while(chIndex<
chLen)//对输入的字符扫描
while(ch[chIndex]=='
'
||ch[chIndex]==9)
{chIndex++;
while(ch[chIndex]==10)//遇到换行符,行数加1
{nLine++;
chIndex++;
}
if(isalpha(ch[chIndex]))
{
charstr[256];
intstrLen=0;
while(isalpha(ch[chIndex])||ch[chIndex]=='
_'
)//是字母、下划线
{
str[strLen++]=ch[chIndex];
chIndex++;
while(isdigit(ch[chIndex]))//不是第一位,可以为数字
{
str[strLen++]=ch[chIndex];
chIndex++;
}
}
str[strLen]=0;
//字符串结束符
if(strlen(str)>
20)//标识符超过规定长度,报错处理
error(str,nLine,1);
else{inti;
for(i=0;
i<
MaxKeyWord;
i++)
if(strcmp(str,KeyWord[i])==0)
strcpy(table[TableNum].symbol,str);
table[TableNum].type=1;
table[TableNum].line=nLine;
table[TableNum].Index=i;
TableNum++;
break;
if(i>
=MaxKeyWord)
{
table[TableNum].Index=WordNum;
strcpy(Word[WordNum++],str);
table[TableNum].type=2;
//变量标识符
}
elseif(isdigit(ch[chIndex]))
intflag=0;
while(isdigit(ch[chIndex])||ch[chIndex]=='
)
if(ch[chIndex]=='
flag++;
if(strlen(str)>
20)
error(str,nLine,3);
if(flag==0)
table[TableNum].type=3;
//整数
if(flag==1)
table[TableNum].type=4;
//小数
if(flag>
1)
error(str,nLine,2);
table[TableNum].Index=DigNum;
strcpy(Digit[DigNum++],str);
strcpy(table[TableNum].symbol,str);
table[TableNum].line=nLine;
TableNum++;
/*******************************运算符************************************/
else
interrorFlag;
//用来区分是不是无法识别的标识符,0为运算符,1为界符
charstr[3];
str[0]=ch[chIndex];
str[1]=ch[chIndex+1];
str[3]=0;
for(inti=0;
MaxOptBNum;
i++)//MaxOptBNum)
if(strcmp(str,OptB[i])==0)
errorFlag=0;
table[TableNum].type=6;
chIndex=chIndex+2;
if(i>
=MaxOptBNum)
for(intk=0;
k<
MaxOptANum;
k++)
if(OptA[k]==ch[chIndex])
{
errorFlag=0;
table[TableNum].type=5;
table[TableNum].symbol[0]=ch[chIndex];
table[TableNum].symbol[1]=0;
table[TableNum].line=nLine;
table[TableNum].Index=k;
TableNum++;
chIndex++;
break;
}
/*************************界符*****************************************/
for(intj=0;
j<
MaxEndNum;
j++)
if(End[j]==ch[chIndex])
errorFlag=1;
table[TableNum].symbol[0]=ch[chIndex];
table[TableNum].symbol[1]=0;
table[TableNum].Index=j;
table[TableNum].type=7;
/********************其他无法识别字符*************************************/
if(errorFlag!
=0&
&
errorFlag!
=1)//开头的不是字母、数字、运算符、界符
charstr[256];
intstrLen=-1;
chIndex++;
while(*ch!
='
||*ch!
=9||ch[chIndex]!
=10)//
{
str[strLen++]=ch[chIndex];
chIndex++;
}
str[strLen]=0;
table[TableNum].type=8;
table[TableNum].Index=-2;
voidTrans(doublex,intp)//把十进制小数转为16进制
inti=0;
//控制保留的有效位数
while(i<
p)
if(x==0)//如果小数部分是0
break;
//则退出循环
else
intk=int(x*16);
//取整数部分
x=x*16-int(k);
//得到小数部分
if(k<
=9)
cout<
k;
char(k+55);
};
i++;
intmain()
{
ifstreamin;
ofstreamout,outVar,outCon;
charin_file_name[26],out_file_name[26];
//读入文件和写入文件的名称
charch[Max];
//存放输入代码的缓冲区
intnLine=1;
//初始化行数
Table*table=newTable[Max];
intchoice;
请输入读入方式:
1:
从文件中读,2:
从键盘读(输入结束标志位#):
cin>
choice;
switch(choice)
inti;
/****************************从文件读取***************************/
cout<
Entertheinputfilename:
cin>
in_file_name;
in.open(in_file_name);
if(in.fail())//打开display表读文件失败
cout<
Inputputfileopeningfailed.\n"
exit
(1);
Entertheoutputfilename:
out_file_name;
out.open(out_file_name);
outVar.open("
变量表.txt"
);
outCon.open("
常量表.txt"
if(out.fail())//打开display表写文件失败
Outputfileopeningfailed.\n"
if(outVar.fail())//打开变量表写文件失败
VarOutputfileopeningfailed.\n"
if(outCon.fail())//打开常量表写文件失败
ConstOutputfileopeningfailed.\n"
in.getline(ch,Max,'
Scanner(ch,strlen(ch),table,nLine);
//调用扫描函数
if(errorFlag==1)//出错处理
return0;
/*******************************把结果打印到各个表中***********************/
out<
类型"
"
下标"
endl;
for(i=0;
i<
TableNum;
i++)//打印display
out<
(0x"
hex<
table[i].type<
"
0x"
hex<
table[i].Index<
)"
//在文件testout.txt中输出
outCon<
常量值"
endl;
for(i=0;
i++)//打印常量表
if(table[i].type==3)
longnum1;
num1=atoi(table[i].symbol);
outCon<
table[i].Index<
num1<
if(table[i].type==4)
doublenum2;
num2=atof(table[i].symbol);
num2<
outVar<
变量名称"
WordNum;
i++)//打印变量表
outVar<
i<
Word[i]<
in.close();
//关闭文件
out.close();
outVar.close();
outCon.close();
/***********************************从键盘输入****************************/
cin.getline(ch,Max,'
Scanner(ch,strlen(ch),table,nLine);
if(errorFlag==1)
\nDisplay表:
\n"
//dos界面下
i++)
\n常量表:
if(table[i].typ
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 实验 词法 分析器
![提示](https://static.bdocx.com/images/bang_tan.gif)