词法分析实验报告Word文件下载.docx

上传人:b****6 文档编号:22268856 上传时间:2023-02-03 格式:DOCX 页数:14 大小:16.83KB
下载 相关 举报
词法分析实验报告Word文件下载.docx_第1页
第1页 / 共14页
词法分析实验报告Word文件下载.docx_第2页
第2页 / 共14页
词法分析实验报告Word文件下载.docx_第3页
第3页 / 共14页
词法分析实验报告Word文件下载.docx_第4页
第4页 / 共14页
词法分析实验报告Word文件下载.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

词法分析实验报告Word文件下载.docx

《词法分析实验报告Word文件下载.docx》由会员分享,可在线阅读,更多相关《词法分析实验报告Word文件下载.docx(14页珍藏版)》请在冰豆网上搜索。

词法分析实验报告Word文件下载.docx

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"

);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 农学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1