#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define M 1000
struct
key
{
char
g[20];
};
typedef
struct
key KEY;
int
main()
{
KEY k[6];
int
i=0,j=0;
char
a[M],b[M];
strcpy(k[0].g,
"begin"
);
strcpy(k[1].g,
"if"
);
strcpy(k[2].g,
"then"
);
strcpy(k[3].g,
"while"
);
strcpy(k[4].g,
"do"
);
strcpy(k[5].g,
"end"
);
printf(
"请输入源程序:
"
);
gets(a);
while
(a[i]!=
' '
)
{
j=0;
if
((isalpha(a[i]))||(a[i]==
'_'
))
{
while
((a[i]!=
' '
)&&(a[i]!=
' '
))
{
if
((isalpha(a[i]))||(isdigit(a[i])))
{
b[j]=a[i];
j++;
i++;
}
else
break
;
}
if
((a[i]==
' '
)||(a[i]==
' '
))
{
b[j]=
' '
;
if
(strcmp(b,k[0].g)==0)
printf(
"begin 种别码 1
"
);
else
if
(strcmp(b,k[1].g)==0)
printf(
"if 种别码 2
"
);
else
if
(strcmp(b,k[2].g)==0)
printf(
"then 种别码 3
"
);
else
if
(strcmp(b,k[3].g)==0)
printf(
"while 种别码 4
"
);
else
if
(strcmp(b,k[4].g)==0)
printf(
"do 种别码 5
"
);
else
if
(strcmp(b,k[5].g)==0)
printf(
"end 种别码 6
"
);
else
printf(
"%s 种别码 10
"
,b);
}
else
{
b[j]=
' '
;
printf(
"%s 种别码 10
"
,b);
i--;
}
}
else
if
(isdigit(a[i]))
{
j=0;
while
(isdigit(a[i]))
{
b[j]=a[i];
j++;
i++;
}
b[j]=
' '
;
printf(
"%s 种别码 11
"
,b);
i--;
}
else
{
switch
(a[i])
{
case
'+'
:
printf(
"%c 种别码 13
"
,a[i]);
break
;
case
'-'
:
printf(
"%c 种别码 14
"
,a[i]);
break
;
case
'*'
:
printf(
"%c 种别码 15
"
,a[i]);
break
;
case
'/'
:
printf(
"%c 种别码 16
"
,a[i]);
break
;
case
':'
:
if
(a[i+1]==
'='
)
{ printf(
":= 种别码 18
"
);i++;
break
;}
else
{ printf(
"%c 种别码 17
"
,a[i]);
break
;}
case
'<'
:
if
(a[i+1]==
'='
)
{ printf(
"<= 种别码 21
"
);i++;
break
;}
if
(a[i+1]==
'>'
)
{ printf(
"<> 种别码 22
"
);i++;
break
;}
else
printf(
"%c 种别码 20
"
,a[i]);
break
;
case
'>'
:
if
(a[i+1]==
'='
)
{ printf(
">= 种别码 24
"
);i++;
break
;}
else
printf(
"%c 种别码 23
"
,a[i]);
break
;
case
'='
:
printf(
"= 种别码 25
"
);
break
;
case
';'
:
printf(
"; 种别码 26
"
);
break
;
case
'('
:
printf(
"( 种别码 27
"
);
break
;
case
')'
:
printf(
") 种别码 28
"
);
break
;
case
'#'
:
printf(
"# 种别码 0
"
);
break
;
default
:
printf(
"%c 语法错误!
"
,a[i]);
break
;
}
}
i++;
}
}