编译原理词法分析程序Word文件下载.docx
《编译原理词法分析程序Word文件下载.docx》由会员分享,可在线阅读,更多相关《编译原理词法分析程序Word文件下载.docx(18页珍藏版)》请在冰豆网上搜索。
intpoint=0;
//源程序当前位置指针
charsignTab[STB_MAX_LEN][STR_MAX_LEN];
//符号表
intpointSTB=0;
//符号表指针
charconstTab[CTB_MAX_LEN][STR_MAX_LEN];
//常量表
intpointCTB=0;
//常数表指针
charkwTab[KEYWORD_LEN][10]={//保留字表C语言一共有32个保留字[关键字]
"
auto"
"
break"
case"
char"
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字符串查找保留字表若是一个保留字-返回其编码否则返回
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;
*'
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))judge='
D'
switch(judge)
while(isalnum(ch)||ch=='
{//标识符
wordget[i++]=ch;
wordget[i]='
//回退一个字符
code=Reserve(wordget);
if(code==0)
value=InsertId(wordget);
pDu->
kind=ID;
value=value;
kind=code;
value=-1;
while(isdigit(ch))
value=InsertConst(wordget);
kind=CONST;
value=value;
//( ) [ ] . , !
= ~ sizeof <
<
<
= >
>
>
= = == &
&
= |
|| |= ?
+ ++ +=
// - ->
-- -= * *= / /= % %= >
= <
= ^ ^=
'
//字符串常量
wordget[i++]=