北邮编译原理词法分析报告器实验Word文件下载.docx
《北邮编译原理词法分析报告器实验Word文件下载.docx》由会员分享,可在线阅读,更多相关《北邮编译原理词法分析报告器实验Word文件下载.docx(18页珍藏版)》请在冰豆网上搜索。
//将标识符插入符号表
voidinsertNumList();
//将数字插入常数表
voidfillBuffer(inta);
//填充buff的半区函数
2.程序源代码
#include<
stdio.h>
stdlib.h>
string.h>
math.h>
intpointer=0;
//
inti=0,j=0,c=0,appear,d=0,num_location;
introw_num=0,letter_num=0,word_num=0;
intz=1;
charC='
'
;
chartoken[30];
charbuff[4095];
chartoken_list[200][30];
charnumber[200][10];
charnext_charac,charac,file_name[20];
charkeywords[32][10]={"
auto"
"
break"
case"
char"
const"
continue"
default"
do"
double"
else"
enum"
extern"
float"
for"
goto"
if"
int"
long"
register"
return"
short"
signed"
sizeo"
fstatic"
struct"
switch"
typedef"
union"
unsigned"
void"
volatile"
while"
};
doublenum;
FILE*file_pointer;
voidfillBuffer(inta)//
{
//
i=0;
while((!
feof(file_pointer))&
&
i<
2048){//
buff[a+i]=charac;
if(charac!
='
){//
if(charac=='
\n'
){
row_num++;
}
else{
letter_num++;
charac=fgetc(file_pointer);
i++;
if(feof(file_pointer)){
buff[a+i]='
\0'
voidreadChar()//
C=buff[pointer];
if(pointer==1023){//
fillBuffer(1024);
pointer++;
elseif(pointer==2047){//
fillBuffer(0);
pointer==0;
voidignoreSpace()//
if(C=='
||C=='
\t'
pointer=0;
ignoreSpace();
voidlink()//
token[j++]=C;
boolalphabet()//
if((C>
=97&
C<
=122)||(C>
=65&
=90)){
returntrue;
returnfalse;
booldigit()//
if(C>
=48&
=57){
intsearchForKeywords()//
for(intx=0;
x<
32;
x++){
if(strcmp(token,keywords[x])==0){
return0;
return1;
intsearchForToken()//
inti=0;
while(i<
=c-1){
if(strcmp(token,token_list[i])==0){
appear=i;
}//
word_num++;
intsearchForNum()//
=d-1){
if(strcmp(token,number[i])==0){
num_location=i;
voidinsertTokenList()//
strcpy(token_list[c++],token);
voidinsertNumList()//
strcpy(number[d++],token);
main()
printf("
输入源文件的路径:
\n"
);
scanf("
%s"
file_name);
file_pointer=fopen(file_name,"
r"
if(file_pointer==NULL)printf("
无法查找到文件,发生错误!
while(C!
readChar();
switch(C){
case65:
case66:
case67:
case68:
case69:
case70:
case71:
case72:
case73:
case74:
case75:
case76:
case77:
case78:
case79:
case80:
case81:
case82:
case83:
case84:
case85:
case86:
case87:
case88:
case89:
case90:
case97:
case98:
case99:
case100:
case101:
case102:
case103:
case104:
case105:
case106:
case107:
case108:
case109:
case110:
case111:
case112:
case113:
case114:
case115:
case116:
case117:
case118:
case119:
case120:
case121:
case122:
case'
_'
:
//ǰזΪזĸʽז
ϟ
while(alphabet()||digit()||C=='
link();
token[j]='
j=0;
pointer--;
if(searchForKeywords()==1){//
if(searchForToken()==1){//
insertTokenList();
<
ID,%d>
c-1);
appear);
%s,ؼז>
token);
break;
case48:
case49:
case50:
case51:
case52:
case53:
case54:
case55:
case56:
case57:
num=(C-48);
while(digit()){
num=num*10+(C-48);
.'
num=num+(C-48)*pow(0.1,z++);
if(searchForNum()==1){//
insertNumList();
%f,%d>
num,d-1);
num,num_location);
+'
+=,赋值运算符>
elseif(C=='
++,自加>
+,加号>
-'
--,自减>
"
-=,赋值运算符>
-,减号>
*'
*,乘号>
break;
/'
next_charac=buff[pointer];
&
next_charac!
/*,多行注释>
//,单行注释>
/,除号>
%'
%,取模>
('
(,左小括号>
)'
),右小括号>
['
[,左中括号>
]'
],右中括号>
{'
{,-左大括号>
}'
},右大括号>
'
:
冒号>
;
分号>
'
逗号>
.,句号>
?
?
问号>
"
引号>
#'
#,井号>
>
>
=,大于等于>
大于>
<
=,小于等于>
小于>
==,判等>
=,赋值等>
且>
取地址符>
|'
||,或>
|,运算符>
!
!
=,不等>
非>
该源文件的行数为:
%d,单词数为%d,字符数为%d.\n"
row_num,word_num,letter_num);
system("
pause"
}
3.程序的执行结果
测试程序为:
运行结果为:
四、有待改进的地方