词法分析程序流程图
#include
#include
//#include
charprog[80],token[8];
charch;
int c=21;
intsyn,p,m=0,n,row,sum=0;
char *rwtab[6]={"begin","if","then","while","do","end"};
#define MAX 200
void getbc()//每次调用时,检查ch中的字符是否为空白字符,若是空白字符,取下一个字符,直至ch中进入一个非空白字符为止。
{
while(ch==' ')
{
ch = prog[p++];
}
}
int letter(char ch)
{
if((ch>='a'&&ch='A'&&ch
return 1;
else
return 0;
}
int digit(char ch)
{
if(ch>='0'&&ch
return 1;
else
return 0;
}
void concat()//每次调用把当前ch中的字符与token中的字符串联接。例如,假定token字符数组中原有值为“ab”, ch中存放着“c”,经调用concat( )后,token数组中的值变为“abc”。 {
token[m++]=ch;
// token[m]='\0';
}
int reserve()//对token中的字符串查关键字表,若它是一个关键字, 则回送它的编码,否则回送标识符的种别码10。
{
for(n=0;n
if(strcmp(token,rwtab[n])==0)
{
syn=n+1;
break;
}
return n;
}
void retract()//读字符指针回退一个字符。
{
p--;
}
void dtb() //+进制转换函数, 它将token中的数字串转换成十进制数值表示
{
sum=0;
while(digit(ch))
{
sum=sum*10+ch-'0';
ch=prog[p++];
}
}
voidScaner()
{ for(n=0;n
ch = prog[p++];
getbc();
if(letter(ch))
{
m=0;
while(letter(ch) || digit(ch))
{ concat();
ch=prog[p++];
}
token[m++]='\0';
retract();
c=reserve( );
if(c!=21) syn=c;
else syn=21;
}
else if(digit(ch))
{
/* while(digit(ch))
{
concat();
ch=prog[p++];
}
token[m++]='\0';
sum=0;
while((ch>='0'&&ch
{
sum=sum*10+ch-'0';
ch=prog[p++];
}*/
dtb();
syn = 22;
retract();
}
else switch(ch)
{
case'+':token[0]=ch;syn=34; break ; case'-':token[0]=ch;syn=35; break ;
case'*':token[0]=ch;syn=36; break ;
case'/':token[0]=ch;syn=37; break ;
case'
if(ch=='=')
{
token[m++]=ch; syn = 42;
}
else if(ch== '>') {token[m++]=ch; syn =41;}
else {
retract();syn = 43;
}
break;
case':': m=0;token[m++]=ch;ch=prog[p++];
if(ch== '=')
{
token[m++]=ch;syn=44;
}
else{
retract(); syn = 45;
}
break;
case'>':m=0;token[m++]=ch;
ch=prog[p++];
if(ch=='=')
{
syn=43;
token[m++]=ch;
}
else
{
syn=40;
retract();
}
break;
case';': syn = 24;token[0]=ch; break;
case'(':syn=27;token[0]=ch;break;
case')':syn=28;token[0]=ch;break;
case'#':syn=0;token[0]=ch;break;
case'\n':syn=-2;break;
default: syn = -1; break;
}
}
void run()
{
p=0;
row=1;
do
{
Scaner();
switch(syn)
{
case 22: printf("(%d,%d)\n",syn,sum); break; case -1: printf("Error in row %d\n",row); break; case -2: row=row++;break;
default: printf("(%d,%s)\n",syn,token);break; }
}
while (syn!=0);
}
int main()
{
int flag;
char buf[4096]; /*缓冲区*/ FILE *fp; /*文件指针*/ intlen,i; /*行字符个数*/ char address[100]; printf("键盘输入选择1\n"); printf("文件输入选择2\n"); scanf("%d",&flag); if(flag==1) { printf("Please input string:\n"); p=0; do { scanf("%c",&ch); prog[p++]=ch; } while(ch!='#'); run(); } else if(flag==2) { printf("请输入源文件名(包括路径和后缀名)\n"); scanf("%s",&address); if((fp=fopen(address,"r"))==NULL) { perror("fail to read"); return -1; } int line=0; while(fgets(buf,4096,fp) != NULL)
} } else { { len = strlen(buf); // buf[len-1] = '\0'; /*去掉换行符*/ printf("第%d行 %s\n",++line,buf); for(i=0;i
词法分析程序流程图
#include
#include
//#include
charprog[80],token[8];
charch;
int c=21;
intsyn,p,m=0,n,row,sum=0;
char *rwtab[6]={"begin","if","then","while","do","end"};
#define MAX 200
void getbc()//每次调用时,检查ch中的字符是否为空白字符,若是空白字符,取下一个字符,直至ch中进入一个非空白字符为止。
{
while(ch==' ')
{
ch = prog[p++];
}
}
int letter(char ch)
{
if((ch>='a'&&ch='A'&&ch
return 1;
else
return 0;
}
int digit(char ch)
{
if(ch>='0'&&ch
return 1;
else
return 0;
}
void concat()//每次调用把当前ch中的字符与token中的字符串联接。例如,假定token字符数组中原有值为“ab”, ch中存放着“c”,经调用concat( )后,token数组中的值变为“abc”。 {
token[m++]=ch;
// token[m]='\0';
}
int reserve()//对token中的字符串查关键字表,若它是一个关键字, 则回送它的编码,否则回送标识符的种别码10。
{
for(n=0;n
if(strcmp(token,rwtab[n])==0)
{
syn=n+1;
break;
}
return n;
}
void retract()//读字符指针回退一个字符。
{
p--;
}
void dtb() //+进制转换函数, 它将token中的数字串转换成十进制数值表示
{
sum=0;
while(digit(ch))
{
sum=sum*10+ch-'0';
ch=prog[p++];
}
}
voidScaner()
{ for(n=0;n
ch = prog[p++];
getbc();
if(letter(ch))
{
m=0;
while(letter(ch) || digit(ch))
{ concat();
ch=prog[p++];
}
token[m++]='\0';
retract();
c=reserve( );
if(c!=21) syn=c;
else syn=21;
}
else if(digit(ch))
{
/* while(digit(ch))
{
concat();
ch=prog[p++];
}
token[m++]='\0';
sum=0;
while((ch>='0'&&ch
{
sum=sum*10+ch-'0';
ch=prog[p++];
}*/
dtb();
syn = 22;
retract();
}
else switch(ch)
{
case'+':token[0]=ch;syn=34; break ; case'-':token[0]=ch;syn=35; break ;
case'*':token[0]=ch;syn=36; break ;
case'/':token[0]=ch;syn=37; break ;
case'
if(ch=='=')
{
token[m++]=ch; syn = 42;
}
else if(ch== '>') {token[m++]=ch; syn =41;}
else {
retract();syn = 43;
}
break;
case':': m=0;token[m++]=ch;ch=prog[p++];
if(ch== '=')
{
token[m++]=ch;syn=44;
}
else{
retract(); syn = 45;
}
break;
case'>':m=0;token[m++]=ch;
ch=prog[p++];
if(ch=='=')
{
syn=43;
token[m++]=ch;
}
else
{
syn=40;
retract();
}
break;
case';': syn = 24;token[0]=ch; break;
case'(':syn=27;token[0]=ch;break;
case')':syn=28;token[0]=ch;break;
case'#':syn=0;token[0]=ch;break;
case'\n':syn=-2;break;
default: syn = -1; break;
}
}
void run()
{
p=0;
row=1;
do
{
Scaner();
switch(syn)
{
case 22: printf("(%d,%d)\n",syn,sum); break; case -1: printf("Error in row %d\n",row); break; case -2: row=row++;break;
default: printf("(%d,%s)\n",syn,token);break; }
}
while (syn!=0);
}
int main()
{
int flag;
char buf[4096]; /*缓冲区*/ FILE *fp; /*文件指针*/ intlen,i; /*行字符个数*/ char address[100]; printf("键盘输入选择1\n"); printf("文件输入选择2\n"); scanf("%d",&flag); if(flag==1) { printf("Please input string:\n"); p=0; do { scanf("%c",&ch); prog[p++]=ch; } while(ch!='#'); run(); } else if(flag==2) { printf("请输入源文件名(包括路径和后缀名)\n"); scanf("%s",&address); if((fp=fopen(address,"r"))==NULL) { perror("fail to read"); return -1; } int line=0; while(fgets(buf,4096,fp) != NULL)
} } else { { len = strlen(buf); // buf[len-1] = '\0'; /*去掉换行符*/ printf("第%d行 %s\n",++line,buf); for(i=0;i