C 语言词法分析器
一,实验目的
1.加强对词法分析原理、方法和基本实现技术的理解;
2.灵活运用这学期所学的知识对词法分析,语法分析有进一步的理解;
3.强化对系统软件综合工程实现能力的训练;
二.实验内容
用 C 语言(或 C++ )作为宿主语言完成:
C 语言(win-TC , ANSI C 或 turbo C 2.0)词法分析器的设计和实现。
三.实验要求
1. 编写 C 语言词法分析器的源程序并调试通过;
2. 通过测试程序的验收;
3. 提交简明扼要的书面实验报告,内容包括:属性字设计;源程序主要流程;主要数据结构设计,
程序。
四,程序流程图
(1) main 主程序图
开始
处理每个字符
初始化
Run=1?
初始化数组
Program[],words[]
提示输入程序字符串
输入并读取程序字符
(2) Scan()函数流程图
Scan()函数,分析并
返回每个单词的属性
判断并输出每
个单词类型属
性。
是否结束
是
结束
初始化数组,标志符
判 断 并 标 志 是
否为关键字
判断并标志为
运算符或标点
符号
判 断 并 标 志 为
其它类型
Return 返 回
sign 数值
五,原程序代码
#include
#include
int i,j,k,sign,number,flag,run,sum,autuor;
/*flag which is use to judge the string is keywords or not!*/
char ch;
char words[10] = {" "};
char program[500];
int Scan(char program[])
{ char * keywords[39] =
{ "auto","default","do", "extern","goto",
"int","short","static","struct","switch",
"typedef","void","break","case","char",
"const","continue","double","else", "enum",
"float","for","if","long", "register",
"return","signed","zhouhong","sizeof","union",
"unsigned","volatile","while","main","printf",
"scanf","include","define","string"
};
= 0;
= 0;
= program[i++];
number = 0;
flag
j
ch
/*To handle the lettle space ands tab*/
while ((ch == ' ')
|| (ch == '\t') || (ch == '\r')||(ch=='\n'))
ch=program[i++];
/* 跳过不理睬 */
/*handle letters*/
if ((ch >= 'a') && (ch <= 'z' ))
{
while ((ch >= 'a') && (ch <= 'z' ))
{
words[j++]=ch;
ch=program[i++];
}
i--;
words[j++] = '\0';
for (k = 0; k < 39; k++)
if (strcmp (words,keywords[k]) == 0)
{sign=k+1;flag=1;
if(k==27) autuor=1;
break;
}
if (flag == 0)
/*为关键字*/
{ sign = 100;
}
}/* if 是关键字*/
/*handle digits*/
else if ((ch >= '0') && (ch <= '9'))
{ number = 0;
while ((ch >= '0' ) && (ch <= '9' ))
{
number = number*10+(ch-'0');
ch
= program[i++];
/*为数字*/
}
sign = 200;
i--;
}
/*opereation and edge handle*/
else switch (ch)
{
case '=':{
if (ch == '=')
words[j++] = ch;
words[j] = '\0';
ch = program[i++];
if (ch == '=')
{words[j++] = ch;
words[j] = '\0';
sign= 401;
/* = =
}
else
{ i--;
/*退回一个字符
sign = 402;
/* = */
}
break;
}
case'>':{ if (ch == '>')
words[j++] = ch;
words[j]
= '\0';
ch
if (ch == '=')
{words[j++] = ch;
= program[i++];
= '\0';
words[j]
sign = 403;
/* >=
}
else if(ch=='>')
{ words[j++] =ch;
words[j] ='\0';
*/
*/
*/
sign
=419;
/*
>>
*/
*/
}
}
else
{ i--;
sign = 404;
}
break;
case'<': {if (ch == '<')
/* >
= program[i++];
words[j++] = ch;
words[j]
= '\0';
ch
if (ch == '=')
{ words[j++] = ch;
words[j]
= '\0';
sign = 405;
}
else if(ch=='<')
{ words[j++] = ch;
words[j]
= '\0';
sign
= 420;
}
else
{ i--;
sign = 406;
}break;
}
case'!':
{if (ch == '!')
words[j++] = ch;
words[j] = '\0';
ch = program[i++];
if (ch == '=')
{words[j++] = ch;
= '\0';
words[j]
sign= 407;
}
else
{i--;
sign= 408;
} break;
}
case'+':{
if (ch == '+')
words[j++] = ch;
= '\0';
= '\0';
= '\0';
words[j]
ch = program[i++];
if (ch == '=')
{ words[j++] = ch;
words[j]
sign= 409;
}
else if (ch == '+')
{ words[j++] = ch;
words[j]
sign= 410;
}
else
{i--;
sign = 411;
}
break;
}
case'-': {if (ch == '-')
words[j++] = ch;
words[j]
= '\0';
ch = program[i++];
if (ch == '=')
{ words[j++] = ch;
= '\0';
words[j]
sign= 412;
}
else if( ch == '-')
{ words[j++] = ch;
= '\0';
words[j]
sign= 413;
}
/* 负数的情况 */
else if((ch >= '0') && (ch <= '9'))
{while( (ch >= '0') && (ch <= '9') )
{words[j++] = ch;
ch = program[i++];
}
words[j] = '\0';
sign = 1000;
/* 标志为 1000 */
/* 负数 */
i--;
break;
}
else
{ i--;
sign = 414;
}break;
}
case'*':{ if (ch == '*')
words[j++] = ch;
words[j]
= '\0';
ch
if (ch == '=')
{words[j++] = ch;
= program[i++];
= '\0';
words[j]
sign = 415;
}
else
{
i--;
sign
}
break;
= 416;
}
case'/':{ if (ch == '/')
words[j++] = ch;
= '\0';
words[j]
ch
= program[i++];
if (ch == '=')
{
words[j++] = ch;
= '\0';
words[j]
sign
= 417;
}
else
{
i--;
sign
}
break;
= 418;
}
case'^':{
if(ch=='^')
words[j++] = ch;
= '\0';
words[j]
sign
=421;
break;
}
case';':{
words[j]
= ch;
words[j+1] = '\0';
sign
break;
= 501;
}
case'(':{
= ch;
words[j]
words[j+1] = '\0';
sign
break;
= 502;
}
case')':{
= ch;
words[j]
words[j+1] = '\0';
sign
break;
= 503;
}
case'[':{
words[j]
= ch;
words[j+1] = '\0';
sign
break;
= 504;
}
case']':{
words[j]
= ch;
words[j+1] = '\0';
sign
break;
= 505;
}
case'{':{
words[j]
= ch;
words[j+1] = '\0';
sign
break;
= 506;
}
case'}':{
words[j]
= ch;
words[j+1] = '\0';
sign
break;
= 507;
}
case':':{
words[j]
= ch;
words[j+1] = '\0';
sign
break;
= 508;
}
case'"':{
words[j] = ch;
words[j+1] = '\0';
sign = 509;
break;
}
case'%':{
if (ch == '%')
words[j++] = ch;
words[j]
= '\0';
ch
= program[i++];
if (ch == '=')
{
words[j++] = ch;
= '\0';
words[j]
sign
= 510;
}
else
{
i--;
sign
}
= 511;
break;
}
case',':{
words[j] = ch;
words[j+1] = '\0';
sign = 512;
break;
}
case'#':{
words[j] = ch;
words[j+1] = '\0';
sign = 513;
break;
}
case'@':{
words[j] = '#';
words[j+1] = '\0';
sign = 0;
break;
}
default:{
sign = -1;
break;
}
}
return sign;
}
void display()
{printf("Autuor: zhouhong\n");
2008-06-10\n");
printf("calss: computer 0502\n");
printf("time:
printf("Email: zhouhong206521@126.com\n");
printf("QQ:
}
454350734\n");
int main()
{
i=0; run=1;
while(run)
{
int ent;
int j;
ent=autuor=sum=0;
for( j = 0; j < 500; j++)
/* 初始化 */
program[j] = ' ';
for( j = 0; j < 10; j++)
words[j]
=
'
';
printf("enter the program end with '@':\n");
do
{
ch
program[i++] = ch;
= getchar();
}while(ch != '@');
i = 0;
do{sign = Scan(program);
if (sign == 200)
{printf("(number
",number,sign);
sum++; ent=1;
}
else if (sign == -1)
{ printf("(error
sum++; ent=1;
}
",sign);
,
%8d,
%5d)
,
%5d)
/* 错误 */
else if(sign>=1&&sign<=38)
{printf("(keyword
,
%8s,
%5d)
",words,sign);
sum++; ent=1;
}
else if(sign>=401&&sign<=421)
{
",words,sign);
printf("(operator,
%8s,
sum++; ent=1;
%5d)
}
else if(sign>=501&&sign<=513)
{printf("(symbol
,
%8s,
%5d)
",words,sign);
sum++; ent=1;
}
if(sum%2==0&&ent==1) puts("\r"),ent=0;
}while (sign != 0);
puts("\n");
if(autuor==1)display(), puts("\n");
printf("continue?(Yor N):");
getchar();
ch=getchar();
if(ch=='N'||ch=='n')
run=0;
} return 0;
getchar();
}