北邮 编译原理 词法分析Word下载.docx
- 文档编号:3931493
- 上传时间:2023-05-02
- 格式:DOCX
- 页数:27
- 大小:91.75KB
北邮 编译原理 词法分析Word下载.docx
《北邮 编译原理 词法分析Word下载.docx》由会员分享,可在线阅读,更多相关《北邮 编译原理 词法分析Word下载.docx(27页珍藏版)》请在冰点文库上搜索。
题目:
*/
#include<
iostream>
fstream>
vector>
string>
#include<
cctype>
usingnamespacestd;
constintMAX_INPUT_BUFFER=512;
//默认输入缓存区
ifstreamfile;
//打开文件
vector<
keyword;
//预先记录关键字
comment;
//存储注释
id;
//记录标示符
pre;
//预处理文件
intsum_char=0;
//字符数
intsum_word=0;
//单词数
intsum_line=0;
//行数
intcurrent_line=0;
//当前行数
boolin_comment=false;
//标识此行是否在注释中
charbuffer[MAX_INPUT_BUFFER];
//缓存区
intbig_bracket=0;
//大括号
intsmall_bracket=0;
//小括号
voidinit(void)//初始化关键字
{
keyword.clear();
keyword.push_back("
auto"
);
break"
case"
char"
const"
continue"
default"
do"
double"
else"
enum"
extern"
float"
for"
goto"
if"
int"
long"
register"
return"
short"
signed"
static"
sizeof"
struct"
switch"
typedef"
union"
unsigned"
void"
volatile"
while"
}
voidsum(void)//计算字符数行数存注释
file.open("
data.c"
intstate=0;
//状态转换0:
正常1:
输入一个/2:
输入/*
chartemp;
//暂存
stringtemp_comment;
//暂存注释
file.seekg(0);
//文件指针回到头
while(!
file.eof())//遍历全文
{
switch(state)
{
case0:
file.get(temp);
sum_char++;
//增加字符
if('
\n'
==temp)
sum_line++;
//增加行
if('
/'
state=1;
break;
case1:
//前一个字符是/
sum_char++;
//增加字符
==temp)//单行注释
{
temp_comment.clear();
//清空注释缓存
while('
!
=temp)//输完整行
file.get(temp);
temp_comment.append(1,temp);
//添加注释
}
sum_line++;
//行增加
comment.push_back(temp_comment);
//添加到注释记录表
state=0;
//状态回归
}
else
*'
==temp)//多行注释
state=2;
else//不是注释
state=0;
//状态回归
case2:
//再取一个
==temp)//结束多行注释
{
comment.push_back(temp_comment);
//存入注释表
state=0;
}
else//还在注释中
temp_comment.append(1,'
temp_comment.append(1,temp);
if('
sum_line++;
}
if(file.eof())//当注释到尾时
if(!
temp_comment.empty())//不为空时存入最后一个注释
{
return;
if('
sum_line++;
}
default:
break;
}
}
voidword_analyse(void)
stringtemp_word;
//暂存单词
char*p=buffer;
//处理指针
while(in_comment)//处理多行注释问题
\0'
==*p)
return;
if(('
==*p)&
&
('
==*(p+1)))
p+=2;
in_comment=false;
p++;
}
while('
=*p)//遍历整句
{
'
==*p)//当是空格时后移
else
if(isalpha(*p)||'
_'
==*p)//以字母或下划线开头,关键字和标识符
temp_word.clear();
while(isalnum(*p)||'
==*p)//记录关键字
temp_word.append(1,*p);
p++;
}//当为空格或别的符号时弹出
sum_word++;
//单词+1
id.push_back(temp_word);
//存入标示符
inti;
for(i=0;
i<
keyword.size();
i++)//遍历关键字表
if(keyword.at(i)==temp_word)
break;
if(i<
keyword.size())//记号的形式输出每个单词符号
cout<
<
endl<
"
第"
sum_word<
个单词:
保留字:
<
temp_word<
endl;
标示符:
if(isdigit(*p))//无符号数
temp_word.clear();
while(isdigit(*p))
==*p)//非法字符
while('
=*p&
'
=*p)
{
temp_word.append(1,*p);
非法字符:
error:
在第"
current_line<
行,单词"
命名非法!
.'
==*p||'
E'
e'
==*p)//小数和指数形式
p++;
}
无符号数:
else
#'
==*p)//预处理文件特殊处理
//p指向换行,完成直接退出
pre.push_back(temp_word);
else
==*p)//字符串
while('
字符串:
+'
==*p)//处理符号
temp_word.clear();
='
==*(p+1))//自加
temp_word="
+="
;
id.push_back(temp_word);
sum_word++;
自加号:
p+=2;
//推进
==*(p+1))//自加1
++"
自加1号:
if(isdigit(*(p+1)))//有符号数
temp_word="
+"
for(intj=1;
isdigit(*(p+j));
j++)
temp_word.append(1,*(p+j));
有符号数:
//推进
加号:
//推进
-'
==*(p+1))//自减
-="
自减号:
==*(p+1))//自减1
--"
自减1号:
-"
减号:
==*(p+1))//自乘
*="
自乘号:
*"
乘号:
==*(p+1))//自除
自除号:
==*(p+1))//行注释
//直接跳出
==*(p+1))//多行注释
in_comment=true;
=*p)//判断这行为止注释能不能结束
if(('
)==*(p+1))
in_comment=false;
/"
除号:
}
==*p)//处理等号
==*(p+1))//比较
=="
比较号:
="
赋值号:
>
==*(p+1))
大于等于号:
大于号:
小于等于号:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 北邮 编译原理 词法分析 编译 原理 词法 分析