编译原理课程设计语言编译器.docx
《编译原理课程设计语言编译器.docx》由会员分享,可在线阅读,更多相关《编译原理课程设计语言编译器.docx(15页珍藏版)》请在冰豆网上搜索。
![编译原理课程设计语言编译器.docx](https://file1.bdocx.com/fileroot1/2023-1/26/f51d996c-2551-488e-90c7-376272225dc5/f51d996c-2551-488e-90c7-376272225dc51.gif)
编译原理课程设计语言编译器
编译原理课程设计报告
课题名称:
词法分析器
小组成员:
陈锋:
2008221105110010
姚珂:
2008221104210527
王自然:
2008221104210001
王力:
2008221104210055
丁昭岩:
2008221104210518
周德娇:
2008221104210508
李文莉:
2008221104210517
庄黎:
2008221104210042
提交报告时间:
2010年6月10日
目录:
课程设计目标2
实验建立词法分析器。
可分离单词并判断单词属性。
2
程序代码实现4
实验总结16
课程设计目标
实验建立词法分析器。
可分离单词并判断单词属性。
1.分析与设计
(1)实现方法:
编程语言为C++语言。
编程方法:
首先扫描表达式,依次分开单词,保存于数组中。
然后,依次判断单词属性并写入文件保存。
(2)扫描器:
C-惯用的词法
1、语言的关键字:
chardoubleenumfloatintlongshortsignedstructunion
unsignedvoidfordowhilebreakcontinueifelsegotoswichcase
returnautoexternregisterstaticsizeoftypedefvolatileconstdefault
#includemain#define
2、专用符号:
+-*/<<=>>===!
==;,()[]{}/**/
3、其他标记是ID和NUM,通过下列正则表达式定义:
ID=letterletter*NUM=digitdigit*letter=a|..|z|A|..|Zdigit=0|..|9
4、空格由空白、换行符和制表符组成。
空格通常被忽略。
5.注释用通常的C语言符号/*...*/围起来。
保存注释符忽略注释内容。
(3)代码设计说明:
程序结构:
编写CEditDate类来扫描并分析单词属性。
文件和类的设计说明:
CEditDate类
包含两个成员和两个函数。
classCEditData
{
private:
CStringfileText;
CStringwordArray[N];
public:
CEditData(CStringfileText);
virtual~CEditData();
BOOLreadText();
BOOLjudgeProperty();
};
readText(),judgeProperty()是最主要的两个函数,分别实现分离单词和判断属性功能。
WordStruct类
此类用于保存单词属性值。
classWordStruct
{
private:
CStringwordName;//Savethenameoftheword;
CStringwordProperty;//Savethepropertyoftheword;
intwordValue;//Savethevalueoftheword;
public:
voidset_WordName(CStringname);
voidset_WordProperty(CStringpro);
voidset_WordValue(intval);
CStringget_WordName();
CStringget_WordProperty();
intget_WordValue();
};
程序代码实现
文件CEditDate.h代码如下:
//实验建立C-编译器。
只含有scanner和parser部分。
#include
usingnamespacestd;
#if!
defined(AFX_EDITDATA_H__16C35366_8E9C_4067_97BE_F5BEE9F03CA8__INCLUDED_)
#defineAFX_EDITDATA_H__16C35366_8E9C_4067_97BE_F5BEE9F03CA8__INCLUDED_
#if_MSC_VER>1000
#pragmaonce
#endif//_MSC_VER>1000
#defineN10000
classCEditData
{
private:
CStringfileText;
CStringwordArray[N];
public:
CEditData(CStringfileText);
virtual~CEditData();
BOOLreadText();
BOOLjudgeProperty();
};
文件CEditDate.cpp代码如下:
文件util.c代码如下:
//EditData.cpp:
implementationoftheCEditDataclass.
//
//////////////////////////////////////////////////////////////////////
#include"stdafx.h"
#include"EditProject.h"
#include"EditData.h"
#include"WordStruct.h"
usingnamespacestd;
#ifdef_DEBUG
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#definenewDEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
//Construction/Destruction
//////////////////////////////////////////////////////////////////////
CEditData:
:
CEditData(CStringfileText)
{
this->fileText=fileText;
}
CEditData:
:
~CEditData()
{
}
boolisDigit(charch)
{
if(ch>='0'&&ch<='9')
{
returntrue;
}
else
returnfalse;
}
boolisLetter(charch)
{
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
{
returntrue;
}
else
returnfalse;
}
intn;
BOOLCEditData:
:
readText()
{
charch;
n=0;
for(inti=0;i{
ch=fileText.GetAt(i);
if(ch==''||ch==13||ch==10||ch=='\t')
{
continue;
}
elseif(isDigit(ch))
{
wordArray[n]=ch;
ch=fileText.GetAt(i+1);
while(isDigit(ch)||ch=='.')
{
wordArray[n]+=ch;
i++;//メニカッヨクユ・
ch=fileText.GetAt(i+1);
}
n++;
}
elseif(isLetter(ch)||ch=='#'||ch=='_')
{
wordArray[n]=ch;
ch=fileText.GetAt(i+1);
while(isLetter(ch)||isDigit(ch))
{
wordArray[n]+=ch;
i++;
ch=fileText.GetAt(i+1);
}
n++;
}
elseif(ch=='%')
{
wordArray[n]=ch;
ch=fileText.GetAt(i+1);
if(ch=='=')
{
ch=fileText.GetAt(++i);
wordArray[n]+=ch;
}
else
{
while(fileText.GetAt(i+1)!
=','&&fileText.GetAt(i+1)!
='\"'&&fileText.GetAt(i+1)!
='%')
{
ch=fileText.GetAt(++i);
wordArray[n]+=ch;
}
}
n++;
}
elseif(ch=='+'&&fileText.GetAt(i+1)=='='||ch=='+'&&fileText.GetAt(i+1)=='+'||ch=='-'&&fileText.GetAt(i+1)=='-'||
ch=='-'&&fileText.GetAt(i+1)=='='||ch=='*'&&fileText.GetAt(i+1)=='='||ch=='/'&&fileText.GetAt(i+1)=='='||
ch=='&'&&fileText.GetAt(i+1)=='='||ch=='^'&&fileText.GetAt(i+1)=='='||ch=='|'&&fileText.GetAt(i+1)=='='||
ch=='?
'&&fileText.GetAt(i+1)==':
'||ch=='&'&&fileText.GetAt(i+1)=='&'||ch=='|'&&fileText.GetAt(i+1)=='|'||
ch=='>'&&fileText.GetAt(i+1)=='='||ch=='<'&&fileText.GetAt(i+1)=='='||ch=='='&&fileText.GetAt(i+1)=='='||
ch=='!
'&&fileText.GetAt(i+1)=='='||ch==':
'&&fileText.GetAt(i+1)==':
')
{
wordArray[n]=ch;
ch=fileText.GetAt(++i);
wordArray[n]+=ch;
n++;
}
elseif(ch=='/'&&fileText.GetAt(i+1)=='*')
{
wordArray[n]=ch;
ch=fileText.GetAt(i+1);
wordArray[n]+=ch;
n++;i++;
ch=fileText.GetAt(i+1);
while(!
(ch=='*'&&fileText.GetAt(i+1)=='/'))
{
i++;
ch=fileText.GetAt(i);
}
if(ch=='*'&&fileText.GetAt(i+1)=='/')
{
wordArray[n]=ch;
ch=fileText.GetAt(i+1);
wordArray[n]+=ch;
i++;n++;
}
}
elseif(ch=='/'&&fileText.GetAt(i+1)=='/')
{
wordArray[n]=ch;
ch=fileText.GetAt(i+1);
wordArray[n]+=ch;
n++;i++;
ch=fileText.GetAt(i+1);
while(ch!
=13)
{
i++;
ch=fileText.GetAt(i);
}
}
elseif(ch=='.'||ch=='='||ch==';'||ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('||ch==')'||ch=='&'||ch=='?
'||ch=='<'||ch=='>'||ch==','||ch=='}'||ch=='{')
{
wordArray[n]=ch;
n++;
}
elseif(ch=='\\')
{
wordArray[n]=ch;
ch=fileText.GetAt(i+1);
if(ch=='x'||ch=='0'||ch=='n'||ch=='t'||ch=='\''||ch=='\"'||ch=='\0'||ch=='a'||ch=='b'||ch=='\\'||ch=='f'||ch=='r'||ch=='v'||ch=='v')
{
wordArray[n]+=ch;
n++;i++;
}
}
}
CStdioFilefile;
file.Open("原文件.txt",CFile:
:
modeCreate|CFile:
:
modeWrite);
for(intj=0;j{
CStringstr;
str.Format("%s",wordArray[j]);
file.WriteString(str+"\n");
}
returntrue;
}
CStringkeys[]={"if","then","while","do","begin","goto","swich","case","break","default",
"printf","scanf","continue","for","signed","unsigned","const","static","sizeof","typedef",
"auto","extern","register","volatile","#include","main","#define","else","long","short",
"int","double","enum","float","char","struct","union","void","return","##"};
CStringsymbol[]={"+","-","*","/","%","++","--","<",">","=",
">=","<=","==","!
=","+=","-=","*=","/=","%=","&",
"&&","||","!
","\'","\"","\\","\\0","\\a","\\b","\\f",
"\\n","\\r","\\t","\\v","?
","(",")","{","}",",",
";",":
","%d","%f","%lf","%c","##"};
intgetCStringArrayLenth(CStringstrArr[])
{
intarrLen=0,i=0;
while(strArr[i]!
="##")
{
arrLen++;i++;
}
returnarrLen;
}
BOOLCEditData:
:
judgeProperty()
{
intlen=n,k=0;
intkeysLen=getCStringArrayLenth(keys);
intsymbolLen=getCStringArrayLenth(symbol);
WordStructmyWord[N];
for(inti=0;i{
//intj=0;
if(isLetter(wordArray[i].GetAt(0)))
{
intflag=0;
for(intj=0;j{
if(wordArray[i]==keys[j])
{
myWord[k].set_WordName(wordArray[i]);
myWord[k].set_WordProperty("关键字");
myWord[k].set_WordValue
(1);
k++;flag=1;break;
}
}
if(flag==0)
{
myWord[k].set_WordName(wordArray[i]);
myWord[k].set_WordProperty("字符常量");
myWord[k].set_WordValue
(2);
k++;
}
//j=0;
}
elseif(isdigit(wordArray[i].GetAt(0)))
{
intisInt=wordArray[i].Find('.',1);
if(isInt>0)
{
myWord[k].set_WordName(wordArray[i]);
myWord[k].set_WordProperty("实型常量");
myWord[k].set_WordValue(3);
}
else
{
myWord[k].set_WordName(wordArray[i]);
myWord[k].set_WordProperty("整型");
myWord[k].set_WordValue(4);
}
k++;
//j=0;
}
elseif(wordArray[i].GetLength()>1&&wordArray[i].GetAt(0)=='/'&&wordArray[i].GetAt
(1)=='*'
||wordArray[i].GetLength()>1&&wordArray[i].GetAt(0)=='*'&&wordArray[i].GetAt
(1)=='/'
||wordArray[i].GetLength()>1&&wordArray[i].GetAt(0)=='/'&&wordArray[i].GetAt
(1)=='/')
{
myWord[k].set_WordName(wordArray[i]);
myWord[k].set_WordProperty("注释符“);
myWord[k].set_WordValue(5);
k++;//j=0;
}
else
{
for(intj=0;j{
if(wordArray[i]==symbol[j])
break;
}
if(j!
=symbolLen)
{
myWord[k].set_WordName(wordArray[i]);
myWord[k].set_WordProperty("运算符“);
myWord[k].set_WordValue(20+j);
k++;
}
else
{
myWord[k].set_WordName(wordArray[i]);
myWord[k].set_WordProperty("变量");
myWord[k].set_WordValue(88);
k++;
}
}
}
try
{
CStdioFilefile;
file.Open("・・txt",CFile:
:
modeCreate|CFile:
:
modeWrite);
for(intm=0;m{
CStringstr;
strFormat(“%-10s\t属性%-10\t类别编码”%-2d",myWord[m].get_WordName(),myWord[m].get_WordProperty(),myWord[m].get_WordValue());
file.WriteString(str+"\n");
}
AfxMessageBox("写入完成");
}
catch(CMemoryException*e)
{
AfxMessageBox("写入失败");
}
returntrue;
}
实验总结:
本实验充分体现了对现今正在学习的编译原理知识的考察,同时也是对上学去VC知识的复习和巩固。
在编写过程中也曾遇到过不少错误,如开始设置的字符串长度不够,不能准确划分各个单词(把单词的第一个字母给漏掉了),……也曾有很多不够完善的地方,如很多的操作符没能进行判断,开始做的那个所有代码都是写在一个函数里显得冗长不便阅读,……通过小组成员的协力合作,我们尽量完善了其中一些不足之处,尽力做到了最好。
综合考虑这次实验,我们觉得我们有以下优点:
1、我们的实验代码基本上达到了老师所布置的要求;2、是我们的代码全部是由我们的组员自己编写,不存在网上复制;3、我们的代码部分逻辑性较强,具有一定的易读性;4、我们实验的界面简单,就两个控件,操作简单。
虽然最终结果并不一定比其他小组的好,但是我们相信我们利用这次机会,通过这次小组合作完成这个实验所收获的,无论是知识还是经验方面,不会比其他小组少,也希望以后能有更多这样的机会,让每个小组成员更上一层楼。