编译原理词法分析器代码Word文档格式.docx
- 文档编号:20276047
- 上传时间:2023-01-21
- 格式:DOCX
- 页数:18
- 大小:18.54KB
编译原理词法分析器代码Word文档格式.docx
《编译原理词法分析器代码Word文档格式.docx》由会员分享,可在线阅读,更多相关《编译原理词法分析器代码Word文档格式.docx(18页珍藏版)》请在冰豆网上搜索。
,
const"
continue"
default"
do"
double"
else"
enum"
extern"
float"
for"
goto"
if"
int"
long"
register"
return"
short"
signed"
sizeof"
static"
struct"
switch"
typedef"
union"
unsigned"
void"
volatile"
while"
};
charerrorTab[][50]={//错误代码表
/*0*/"
未知错误"
/*1*/"
非法的字符"
/*2*/"
不正确的字符常量表达"
/*3*/"
不正确的字符串表达"
/*4*/"
不正确的数字表达"
/*5*/"
注释丢失'
*/'
typedefstructsignDuality
{
intkind;
intvalue;
}*pDualistic,Dualistic;
voidpretreatment();
//预处理
voidProcError(intid);
//错误
boolGetChar();
//获得一个字符不包括结束标记
boolGetBC();
//获得一个非空白字符
voidConcat(char*str);
//将ch连接到str后
intReserve(char*str);
//对str字符串查找保留字表若是一个保留字-返回其编码否则返回0
voidRetract();
//将搜索指示器回调一个字符位置
intInsertId(char*str);
//将str串以标识符插入符号表,并返回符号表指针
intInsertConst(char*str);
//将str串以常数插入符号表,并返回常数表指针
boolwordAnalyse(pDualisticpDu);
//词法分析true正常
//预处理将缓冲区内的源代码去掉注释和无效空格
voidpretreatment()
{
intlines=0;
chartmp[PRO_MAX_LEN];
//先将处理结果保存到临时空间
inttmpp=0;
//这个临时空间的末尾指针
boolflg;
chartmpc;
//去掉注释先//注释有两种一种是//另一种是/**/
point=0;
do
flg=GetChar();
if(ch=='
/'
)
switch(ch)
case'
:
}while(!
(ch=='
\n'
||flg==false));
//注释一直到行尾或文件结束
Retract();
//归还换行
break;
case'
*'
tmpc=ch;
//为了保证出错处理程序能正确定位出错位置保留注释中的换行
if(tmpc=='
tmp[tmpp++]=tmpc;
//归还一个字符
}while(flg&
&
!
(flg&
tmpc=='
&
ch=='
));
if(!
flg)
ProcError(5);
}
default:
//不是任何一种注释
GetChar();
tmp[tmpp++]=ch;
else
}while(flg);
tmp[tmpp]='
\0'
strcpy(proBuffer,tmp);
}
//错误
voidProcError(intid)
printf("
\nError:
第%d行,%s\n"
errorLine,errorTab[id]);
//获得一个字符
boolGetChar()
if(point<
PRO_MAX_LEN&
proBuffer[point]!
='
//如果当前下标合法且当前字符为结束标记则取字符增游标
ch=proBuffer[point++];
if(ch=='
errorLine++;
returntrue;
ch='
returnfalse;
//获得一个非空白字符
boolGetBC()
if(!
GetChar())//获取字符失败
}while(isspace(ch));
//直到获得一个非空白字符
//将ch连接到str后
voidConcat(char*str)
inti;
for(i=0;
str[i];
++i)
str[i]=ch;
str[i+1]='
//对str字符串查找保留字表若是一个保留字-返回其编码否则返回0
intReserve(char*str)
i<
KEYWORD_LEN;
++i)//从保留字表中查找str串
if(0==strcmp(kwTab[i],str))
returni+1;
//注意,这里加一原因是0值被错误标记占用
return0;
//将搜索指示器回调一个字符位置
voidRetract()///char*ch
if(proBuffer[point]=='
errorLine>
0)
errorLine--;
point--;
intInsertId(char*str)
i<
pointSTB;
++i)
if(0==strcmp(signTab[i],str))
returni;
strcpy(signTab[pointSTB++],str);
return(pointSTB-1);
//将str串以常数插入常量表,并返回常数表指针
intInsertConst(char*str)
pointCTB;
if(0==strcmp(constTab[i],str))
strcpy(constTab[pointCTB++],str);
return(pointCTB-1);
//词法分析false--分析结束
boolwordAnalyse(pDualisticpDu)
intcode,value;
charjudge;
//这里有个技巧借用此变量巧妙的运用SWITCH结构
inti=0;
//辅助
GetBC();
judge=ch;
if(isalpha(ch)||ch=='
_'
judge='
L'
if(isdigit(ch))
D'
switch(judge)
while(isalnum(ch)||ch=='
{//标识符
wordget[i++]=ch;
GetChar();
}
wordget[i]='
Retract();
//回退一个字符
code=Reserve(wordget);
if(code==0)
{
value=InsertId(wordget);
pDu->
kind=ID;
value=value;
else
kind=code;
value=-1;
returntrue;
while(isdigit(ch))
value=InsertConst(wordget);
kind=CONST;
value=value;
//()[].,!
=~sizeof<
<
<
=>
>
>
====&
=||||=?
++++=
//-->
---=**=//=%%=>
=<
=^^=
'
//字符串常量
do
}while(ch!
ch!
);
if(ch=='
printf("
%s"
wordget);
ProcError(3);
kind=ERROR;
value=0;
//字符常量
\'
//'
\\'
)//'
{
//如果是转义字符则要多接收一个字符
//ch='
if(ch!
{//'
\b'
ProcError
(2);
('
)'
['
]'
.'
'
~'
?
{'
}'
#'
kind=DIVIDE;
//界符
!
//!
=
if(ch=='
='
wordget[i]='
break;
//<
if(ch=='
||ch=='
//>
//==
//&
|'
//|||=
+'
//+++=
elseRetract();
-'
//---=->
//***=
///=
%'
//%=
^'
//^=
returnfalse;
default:
ProcError
(1);
kind=OPERAT;
//主函数
intmain()
Dualistictmp;
pDualisticptmp=&
tmp;
FILE*fin,*fout;
charc;
char[20];
源代码读入\n"
//scanf("
);
//将源程序读入缓冲区
if((fin=fopen("
Test.txt"
"
r"
))==NULL)
Cannotopeninfile\n"
i=0;
//c=fgetc(fin);
while((c=fgetc(fin))!
=EOF)
if(i>
=PRO_MAX_LEN-1)
\n程序代码太长,无法处理\a"
proBuffer[i++]=c;
fclose(fin);
//关闭文件
proBuffer[i++]='
\n***************************\n源代码读入成功,源代码如下:
\n%s"
proBuffer);
\n按任意键继续\n"
getch();
\n预处理\n"
pretreatment();
\n***************************\n预处理成功,去掉注释后的源代码为:
\n%s*"
\n词法分析\n"
//词法分析
if((fout=fopen("
Result.txt"
wb"
建立文件Result.txt失败。
\n"
errorLine=0;
//错误行归零
if(i++>
PRO_MAX_LEN)//防止遇到BUG导致程序死循环无限写文件
wordAnalyse(ptmp))
if(ptmp->
value==-1)
fprintf(fout,"
%3d,->
\t"
ptmp->
kind);
%3d,%3d>
kind,ptmp->
value);
switch(ptmp->
kind)
caseERROR:
(出错:
%s)"
caseID:
(标识符:
caseCONST:
(常量:
caseOPERAT:
(运算符:
caseDIVIDE:
(界符:
if(ptmp->
kind>
=1&
ptmp->
kind<
=KEYWORD_LEN)
(关键字:
kwTab[ptmp->
kind-1]);
\r\n"
}while
(1);
fclose(fout);
写回常量表和标识符表\n"
Const.txt"
建立文件Const.txt失败。
for(i=0;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 词法 分析器 代码