Cminus语言词法分析器实验报告Word文档下载推荐.docx

上传人:b****6 文档编号:21589089 上传时间:2023-01-31 格式:DOCX 页数:17 大小:251.22KB
下载 相关 举报
Cminus语言词法分析器实验报告Word文档下载推荐.docx_第1页
第1页 / 共17页
Cminus语言词法分析器实验报告Word文档下载推荐.docx_第2页
第2页 / 共17页
Cminus语言词法分析器实验报告Word文档下载推荐.docx_第3页
第3页 / 共17页
Cminus语言词法分析器实验报告Word文档下载推荐.docx_第4页
第4页 / 共17页
Cminus语言词法分析器实验报告Word文档下载推荐.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

Cminus语言词法分析器实验报告Word文档下载推荐.docx

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

Cminus语言词法分析器实验报告Word文档下载推荐.docx

return 

void 

while共6个,所有的关键字都是保留字,并且必须是小写;

标识符:

识别与C语言词法规定相一致的标识符,通过下列正则表达式定义:

ID 

letter 

(letter 

digit)*;

常数:

NUM 

digit 

digit*(.digit 

digit* 

|ε)(e(+ 

|ε) 

|ε),letter 

a|..|z|A|..|Z|,digit 

0|..|9,包括整数,如123等;

小数,如123.45等;

科学计数法表示的常数,如1.23e3,2.3e-9等;

专用符号:

<

>

== 

!

 

( 

) 

/* 

*/;

2、分析器的输入为由上述几类单词构成的程序,输出为该段程序的机内表示形式,即关键字、运算符、界限符变为其对应的机内符,常数使用二进制形式,标识符使用相应的标识符表指针表示。

3、词法分析器应当能够指出源程序中的词法错误,如不可识别的符号、错误的词法等。

四、实验结果(程序)及分析 

#include<

stdio.h>

stdlib.h>

string.h>

#define 

100 

typedef 

struct 

char 

name[30];

int 

code;

addr;

}token;

//存储刚从文件中读取的字符

char 

}Keyword;

}symbol;

Keyword 

key[6]={{"

else"

1},{"

if"

2},{"

int"

3},{"

return"

4},{"

void"

5},{"

where"

6}};

ch;

//接受字符 

FILE 

*source;

//源文件 

*keytxt;

//关键字输出文件 

*badgetxt;

//标识符输出文件 

*othertxt;

//其他单词输出文件 

*number;

error_count;

//错误的个数 

addr_count;

//标识符表的指针 

lineof;

//行号 

token 

current;

// 

暂时存放读入的字符 

zancun;

symbol 

currentsymbol;

symboltable[N];

//标识符表

error(int 

i);

main() 

scan();

error_count=0;

addr_count=0;

lineof=0;

scan() 

i=0;

iskeyword();

//判断关键字

isOthers() 

//判断其他单词 

output_1();

output_2();

output_others();

//其他单词输出文件

Iszhushi();

isnumber();

if((source=fopen("

Source.txt"

"

r"

))==NULL){ 

//打开源文件

printf("

file 

open 

error/n"

);

exit(0);

if((keytxt=fopen("

key.txt"

w"

//打开关键字文件

if((badgetxt=fopen("

badge.txt"

//打开标识符文件

if((othertxt=fopen("

others.txt"

))==NULL) 

error\n"

if((number=fopen("

number.txt"

ch=fgetc(source);

while(ch!

=EOF) 

{

for(i=0;

i<

30;

i++)

current.name[i]='

\0'

;

if((ch>

='

a'

&

ch<

z'

||(ch>

A'

Z'

|| 

ch=='

_'

if(ch=='

\\'

if(ch>

0'

9'

else

isOthers();

fclose(source);

fclose(keytxt);

fclose(badgetxt);

fclose(othertxt);

fclose(number);

iskeyword(){

i=0,k=0,j=0;

h=0;

while(((ch>

(ch<

)) 

||((ch>

(ch>

'

current.name[i]=ch;

i++;

ch=fgetc(source);

}

zancun=current;

for(i=0;

6;

i++) 

for(j=0;

j<

j++) 

if(current.name[j]==key[i].name[j]){

else{ 

h=1;

break;

if(h==0) 

if(h==0){

current.code=key[i].code;

strcpy(symboltable[addr_count].name,current.name);

symboltable[addr_count].code=10;

symboltable[addr_count].addr=addr_count;

addr_count++;

isOthers(){

ch1;

i;

switch(ch){ 

case 

+'

:

current.name[0]='

current.code=13;

current.addr=-1;

-'

current.code=14;

current.addr=-1;

output_others();

break;

*'

current.code=15;

/'

current.name[0]='

current.code=16;

case'

ch1=fgetc(source);

if(ch1=='

){ 

current.name[1]='

current.code=17;

else{ 

fseek(source,-1,1);

current.code=18;

){ 

current.code=19;

}else{ 

current.code=20;

ch1=fgetc(source);

if(ch1=='

){

current.name[1]='

current.code=21;

}else 

current.code=22;

fseek(source,-1,1);

current.code=23;

}else{

current.code=24;

current.code=25;

'

current.code=26;

('

i=ftell(source);

ch1=ch;

ch2;

while(ch!

)'

current.name[0]=ch1;

current.name[1]=ch;

current.code=27;

fseek(source,i,0);

ch2=fgetc(source);

error_count++;

error

(1);

{'

current.code=28;

}'

['

current.code=29;

]'

current.code=29;

10'

lineof++;

Iszhushi() 

for(;

if(ch==EOF) 

error

(2);

error

(2);

isnumber() 

k=0;

output_number();

while(ch>

current.name[k++]=ch;

current.code=11;

output_1() 

关键字:

%s,%d>

"

current.name,current.code);

putchar(10);

fprintf(keytxt,"

output_2() 

printf("

标识符地址:

current.name,addr_count);

putchar(10);

fprintf(badgetxt,"

output_others() 

其它单词:

fprintf(othertxt,"

i) 

switch(i){ 

1:

界符发生错误,发生在第%d行/n"

lineof);

2:

注释发生错误,发生在第%d行/n"

output_number() 

常数<

\n"

fprintf(number,"

实验结果分析,程序运行结果截图:

关键字输出文件:

标识符输出文件:

数字的输出文件 

其它单词输出文件:

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

当前位置:首页 > 表格模板 > 合同协议

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

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