词法分析实验报告Word文件下载.docx
- 文档编号:22268856
- 上传时间:2023-02-03
- 格式:DOCX
- 页数:14
- 大小:16.83KB
词法分析实验报告Word文件下载.docx
《词法分析实验报告Word文件下载.docx》由会员分享,可在线阅读,更多相关《词法分析实验报告Word文件下载.docx(14页珍藏版)》请在冰豆网上搜索。
usingnamespacestd;
constintKeyis_num=35;
charch='
'
;
stringkeyword[50]={"
bool"
"
break"
case"
include"
char"
"
const"
continue"
default"
do"
double"
else"
false"
float"
for"
if"
int"
long"
namespace"
new"
return"
short"
signed"
struct"
switch"
true"
using"
void"
while"
"
then"
string"
scanf"
printf"
gets"
puts"
};
structError
{
intline;
stringst;
vector<
Error>
err;
boolis_keyword(stringc)
inti;
for(i=0;
i<
Keyis_num;
i++)
if(keyword[i]==c)
return1;
return0;
}
boolis_word(charc)
if(((c<
='
z'
)&
&
(c>
a'
))||((c<
Z'
A'
))||c=='
_'
)
else
boolis_num(charc)
if(c>
0'
c<
9'
voidcaculate(FILE*file)
ofstreamout("
output.txt"
ios:
:
out);
Errorx;
err.clear();
stringarr="
"
stringatt="
intline=1,word_cnt=0,char_cnt=0;
intflag=0;
while(~(ch=fgetc(file)))
{
char_cnt++;
arr="
if(flag==1)
if(ch=='
\n'
line++;
flag=0;
out<
<
注释内容:
att<
endl;
}
att+=ch;
elseif(flag==2)
*'
ch=fgetc(file);
/'
att+="
*"
fseek(file,-1L,SEEK_CUR);
elseif(!
flag)
||ch=='
\t'
elseif(is_word(ch))
while(is_word(ch)||is_num(ch))
arr=arr+ch;
word_cnt++;
if(is_keyword(arr))
arr<
\t\t\t关键字"
\t\t\t标识符"
elseif(is_num(ch))
intdot_cnt=0,e_cnt=0;
//doublea=1.3e+9;
errorneedtosolve
while(is_num(ch)||(ch=='
.'
is_num(fgetc(file)))||ch=='
e'
dot_cnt++;
if(dot_cnt==2)
x.line=line;
x.st="
无符号数中小数点过多"
err.push_back(x);
e_cnt++;
if(e_cnt==2)
无符号数中e过多"
if(ch=='
+'
||ch=='
-'
||is_num(ch))
arr+=ch;
无符号数格式错误"
if(is_word(ch))
标识符以数字开始"
\t\t\t无符号数"
elseswitch(ch)
case'
++"
\t\t\t运算符"
elseif(ch=='
+="
\t\t\t赋值运算符"
+"
break;
:
--"
-="
-"
*="
=="
\t\t\t相等比较"
="
\t\t\t赋值符"
att="
flag=1;
flag=2;
/"
('
)'
['
]'
'
#'
'
{'
}'
ch<
\t\t\t界限符"
case'
\"
\t\t\t引号"
stringstr="
while(ch!
str+=ch;
if(str=="
||str[0]=='
NULL"
\t\t\t空串"
str<
\t\t\t字符串"
\t\t\t控制符"
>
\t\t\t取地址符"
default:
\t\t\t无法识别字符"
if(flag!
=0)
没有结束标记的注释"
******总计如下******"
源文件共"
line<
行"
char_cnt<
个字符"
word_cnt<
个单词"
err.size()<
个错误"
for(inti=0;
i<
err.size();
i++)
第"
err[i].line<
行:
err[i].st<
voidread()
ifstreamin("
stringx;
while(getline(in,x))
cout<
x<
intmain()
charaddress[50];
FILE*file;
请输入要进行词法分析的文件名(包括路径和后缀名):
while(true)
cin>
address;
if((file=fopen(address,"
r"
))!
=NULL)
路径错误!
请重新输入:
\n********************词法分析结果如下*********************"
caculate(file);
fclose(file);
read();
\n*****************参看本目录下的output.txt*******************"
//system("
pause"
);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 词法 分析 实验 报告