词法分析器实验报告Word格式.docx
《词法分析器实验报告Word格式.docx》由会员分享,可在线阅读,更多相关《词法分析器实验报告Word格式.docx(16页珍藏版)》请在冰豆网上搜索。
/*判断i当前所指的字符是否为一个分界符,是的话返回真,反之假*/
boolIsBoundary(stringstr,inti);
/*判断i当前所指的字符是否为一个运算符,是的话返回真,反之假*/
boolIsOperation(stringstr,inti);
/*此函数将一个pair数组输出到一个文件中*/
voidOutFile(vector<
pair<
int,string>
>
v);
/*此函数接受一个字符串数组,对它进行词法分析,返回一个pair型数组*/
vector<
analyst(vector<
string>
vec);
/*此函数判断传递的参数是否为关键字,是的话,返回真,反之返回假*/
boolIsKey(stringstr);
2.各模块设计
(1).首先根据上面单词符号表及ID和NUM的正规定义式,构造出状态转换图;
(2).定义相关的变量和数据结构。
关键字作为特殊标识符处理,把它们预先安排在一张表格中(称为关键字表),当扫描程序识别出标识符时,查关键字表。
如能查到匹配的单词,则该单词为关键字,否则为一般标识符。
关键字表为一个字符串数组,其描述如下:
charKEY_WORDS[7]={″main″,″int″,″char″,″if″,″else″,″for″,″while″};
用以存放单词符号二元式的数据结构可如下定义:
classWord_Analyzer
{
public:
charContent[MAXLENGTH];
intval;
voidprint();
};
(3).按照编译程序一遍扫描的要求,把词法分析器Scaner作为一个独立的子程序来设计,通过对Scaner的反复调用识别出所有的单词符号;
(4).当Scaner识别出一个单词符号时,则将该单词符号的二元式写入到输出文件中。
若Scaner无法识别出一个单词符号时,则调用错误处理程序PrintError,显示当前扫描到的字符及其所在行、列位置,并跳过该字符重新开始识别单词符号。
四、程序代码
#include<
iostream>
#include<
vector>
fstream>
usingnamespacestd;
/*用来存储目标文件名*/
stringfile_name;
/*提取文本文件中的信息。
stringGetText();
/*获得一个单词符号,从位置i开始查找。
//并且有一个引用参数j,用来返回这个单词最后一个字符在str的位置。
stringGetWord(stringstr,inti,int&
/*这个函数用来除去字符串中连续的空格和换行
//第一个参数为目标字符串,第二个参数为开始位置
//返回值为连续的空格和换行后的第一个有效字符在字符串的位置*/
intDeleteNull(stringstr,inti);
/*判断i当前所指的字符是否为一个分界符,是的话返回真,反之假*/
boolIsBoundary(stringstr,inti);
/*判断i当前所指的字符是否为一个运算符,是的话返回真,反之假*/
boolIsOperation(stringstr,inti);
/*此函数将一个pair数组输出到一个文件中*/
voidOutFile(vector<
/*次函数将参数str,digit添加到temp中*/
voidadd(stringstr,intdigit,vector<
temp);
/*此函数接受一个字符串数组,对它进行词法分析,返回一个pair型数组*/
vector<
/*此函数判断传递的参数是否为关键字,是的话,返回真,反之返回假*/
boolIsKey(stringstr);
intmain()
{
cout<
<
"
*********************************************************\n"
;
cout<
*************编译原理课程设计--词法编译器ver1.0*********\n"
cout<
endl;
stringcom1="
"
stringcom2="
\n"
stringfileline=GetText();
intbegin=0,end=0;
array;
do
begin=DeleteNull(fileline,begin);
stringnowString;
nowString=GetWord(fileline,begin,end);
if(end==-1)
break;
if(nowSpare(com1)&
&
nowSpare(com2))
array.push_back(nowString);
begin=end+1;
}while(true);
mid_result;
mid_result=analyst(array);
OutFile(mid_result);
*程序已完成词法分析,分析结果已经存储在文件"
file_name<
中*\n"
感谢使用,谢谢再见!
system("
pause"
);
return0;
}
/*提取文本文件中的信息*/
stringGetText()
stringfile_name1;
请输入源文件名(包括路径和后缀名):
cin>
>
file_name1;
ifstreaminfile(file_name1.c_str(),ios:
:
in);
if(!
infile)
cerr<
无法打开文件!
file_name1.c_str()<
exit(-1);
charf[1000];
infile.getline(f,1000,EOF);
infile.close();
printf(f);
returnf;
//并且有一个引用参数j,用来返回这个单词最后一个字符在原字符串的位置。
j)
stringno_use("
(){},;
\n+=*/-<
\"
j=str.find_first_of(no_use,i);
if(j==-1)
return"
if(i!
=j)
j--;
returnstr.substr(i,j-i+1);
//第一个参数为目标字符串,第二个参数为开始位置
//返回值为连续的空格和换行后的第一个有效字符在字符串的位置*/
intDeleteNull(stringstr,inti)
for(;
i++)
if(str[i]!
='
'
str[i]!
\n'
)
returni;
boolIsBoundary(stringstr,inti)
intt;
chararr[7]={'
'
'
{'
}'
('
)'
};
for(t=0;
t<
7;
t++)
if(str[i]==arr[t])
returntrue;
returnfalse;
boolIsOperation(stringstr,inti)
chararr[8]={'
+'
-'
*'
/'
8;
/*此函数将一个个字符串数组输出到一个文件中*/
v)
请输入目标文件名(包括路径和后缀名):
file_name;
ofstreamoutfile(file_name.c_str(),ios:
out);
outfile)
file_name.c_str()<
inti;
for(i=0;
i<
v.size();
{
outfile<
v[i].first<
\"
v[i].second<
}
\n\n*********************************\n"
outfile.close();
return;
boolIsKey(stringstr)
stringp[7]={"
main"
"
int"
char"
if"
else"
for"
while"
vector<
ppp(p,p+7);
intu;
for(u=0;
u<
ppp.size();
u++)
if(!
pare(ppp[u]))
returntrue;
returnfalse;
vec)
temp;
vec.size();
if(vec[i].size()==1)
if(vec[i]=="
vec[i+1]=="
="
{
stringjk=vec[i];
jk.append(vec[++i],0,1);
pair<
pp(37,jk);
temp.push_back(pp);
continue;
}
pp(38,jk);
pp(39,jk);
!
pp(40,jk);
pp(42,jk);
|"
pp(43,jk);
if(IsBoundary(vec[i],0))
if(vec[i]=="
{
pair<
pp1(32,vec[i]);
temp.push_back(pp1);
}
pp2(34,vec[i]);
temp.push_back(pp2);
{"
{
pp3(30,vec[i]);
temp.push_back(pp3);
}"
pp4(31,vec[i]);
temp.push_back(pp4);
("
pp5(26,vec[i]);
temp.push_back(pp5);
)"
{
pp6(27,vec[i]);
temp.push_back(pp6);
{
pp7(33,vec[i]);
temp.push_back(pp7);
elseif(IsOperation(vec[i],0))
+"
{
pp8(22,vec[i]);
temp.push_back(pp8);
-"
pp9(23,vec[i]);
temp.push_back(pp9);
*"
pp10(24,vec[i]);
temp.push_back(pp10);
/"
pp11(25,vec[i]);
temp.push_back(pp11);
pp12(21,vec[i]);
temp.push_back(pp12);
pp13(35,vec[i]);
temp.push_back(pp13);
pp14(36,vec[i]);
temp.push_back(pp14);
pp26(41,vec[i]);
temp.push_back(pp26);
elseif(vec[i][0]<
9'
vec[i][0]>
0'
pair<
pp24(10,vec[i]);
temp.push_back(pp24);
else
pp25(10,vec[i]);
temp.push_back(pp25);
elseif(vec[i][0]<
pp23(3,vec[i]);
temp.push_back(pp23);
elseif(IsKey(vec[i]))
{
pp15(1,vec[i]);
temp.push_back(pp15);
pp16(2,vec[i]);
temp.push_back(pp16);
{
pp17(3,vec[i]);
temp.push_back(pp17);
{
pp18(4,vec[i]);
temp.push_back(pp18);
{
pp19(5,vec[i]);
temp.push_back(pp19);
pp20(6,vec[i]);
temp.push_back(pp20);
pp21(7,vec[i]);
temp.push_back(pp21);
pp22(10,vec[i]);
temp.push_back(pp22);
returntemp;
/*finished*/
五、运行调试
六、实验心得
该词法分析器功能良好,可以完成预定的要求。
此次实验让我了解了如何设计、编制并调试词法分析程序,并加深了我对词法分析器原理的理解;
熟悉了直接构造词法分析器的方法和相关原理,并学会使用c语言直接编写词法分析器;
同时更熟练的掌握用c语言编写程序,实现一定的实际功能。
通过这次实验,我对词法分析器有了进一步的了解,把理论知识应用于实验中。
也让我重新熟悉了C语言的相关内容,加深了对C语言知识的深化和用途的理解。
通过这次语义分析的实验,我对高级语言的学习有了更深的认识,了解得更透彻。
同时我了解了高级语言转化为目标代码或汇编指令的过程,。
对今后的学习将起很大的作用,对以后的编程有很大的帮助.本次实验虽然只是完成了一个简单的程序,并且程序的主要框架课本上有给出,但在组织程序结构和深入了解上学到了很多,加深了对编译原理的理解,掌握乐编译程序的实现方法和技术。
巩固了前面所学的知识。