# include <stdio.h>
# include <string.h>
# include <ctype.h>
# include<stdlib.h>
# define ID 12
# define INT 13
# define LT 14
# define LE 15
# define EQ 16
# define NE 17
# define GT 18
# define GE 19
char TOKEN[20];
FILE *fp;//=fopen("c:\\bianyi.txt","r");
FILE *fp1=fopen("c:\\bianyi1.txt","w");
extern int lookup (char* TOKEN )
{
int a;
if(strcmp(TOKEN,"begin")==0)
{
a=1;
return a;
}
if(strcmp(TOKEN,"end")==0)
{
a=2;
return a;
}
if(strcmp(TOKEN,"if")==0)
{
a=3;
return a;
}
if(strcmp(TOKEN,"then")==0)
{
a=4;
return a;
}
if(strcmp(TOKEN,"else")==0)
{
a=5;
return a;
}
if(strcmp(TOKEN,"for")==0)
{
a=6;
return a;
}
if(strcmp(TOKEN,"do")==0)
{
a=7;
return 7;
}
if(strcmp(TOKEN,"while")==0)
{
a=8;
return 8;
}
if(strcmp(TOKEN,"and")==0)
{
a=9;
return a;
}
if(strcmp(TOKEN,"or")==0)
{
a=10;
return a;
}
if(strcmp(TOKEN,"not")==0)
{
a=11;
return a;
}
else
{
a=0;
return a;
}
}
extern void out (int b, char* TOKEN)
{
if(b==12)
{
printf("<12:标识符>");
printf("%s",TOKEN);
fputs(TOKEN,fp1);
}
else if(b==13)
{
printf("13:无符号常数");
printf("%s",TOKEN);
fputs(TOKEN,fp1);
}
else if(b==14)
{
printf("14:<");
fputs("<",fp1);
}
else if(b==15)
{
printf("15:<=");
fputs("<=",fp1);
}
else if(b==16)
{
printf("16:=");
fputs("=",fp1);
}
else if(b==17)
{
printf("17:<>");
fputs("<>",fp1);
}
else if(b==18)
{
printf("18:>");
fputs(">",fp1);
}
else if(b==19)
{
printf("19:>=");
fputs(">=",fp1);
}
else if(b==20)
{
printf("20::=");
fputs(":=",fp1);
}
else if(b==21)
{
printf("21:+");
fputs("+",fp1);
}
else if(b==22)
{
printf("22:-");
fputs("-",fp1);
}
else if(b==23)
{
printf("23:*");
fputs("*",fp1);
}
else if(b==24)
{
printf("24:/");
fputs("/",fp1);
}
else if(b==25)
{
printf("25:;");
fputs(";",fp1);
}
else if(b==26)
{
printf("26:(");
fputs("(",fp1);
}
else if(b==27)
{
printf("27:)");
fputs(")",fp1);
}
// else if(b==28)
// {
// printf("28:/*");
// fputs("/*",fp1);
// }
else if(b==28)
{
printf("28:注释");
//zhushi();
}
else if(b==1)
printf("key:begin");
else if(b==2)
printf("key:end");
else if(b==3)
printf("key:if");
else if(b==4)
printf("key:then");
else if(b==5)
printf("key:else");
else if(b==6)
printf("key:for");
else if(b==7)
printf("key:do");
else if(b==8)
printf("key:while");
else if(b==9)
printf("key:and");
else if(b==10)
printf("key:or");
else if(b==11)
printf("key:not");
printf("\n");
}
extern report_error (void)
{
printf("error");
}
extern zhushi(void)
{
fclose(fp);
fp=fopen("c:\\bianyi.txt","r");
int j;
char ch;
ch=getc(fp);
TOKEN[0]=ch;
j=1;
while(ch!=EOF)
{
ch=getc(fp);
j++;
TOKEN[j]=ch;
}
fputs(TOKEN,fp1);
out(28, "");
}
int main (void)
{
int i, b;
char ch;
// FILE *fp;
// FILE *fp1;
fp=fopen("bianyi.txt","r");
fp1=fopen("bianyi1.txt","w");
ch=fgetc(fp);
while (ch!=EOF)
{
if (isalpha (ch)) /*it must be a identifier*/
{
TOKEN[0]=ch;
ch=fgetc(fp);
i=1;
while (isalnum (ch))
{
TOKEN[i]=ch;
i++;
ch=fgetc(fp);
}
TOKEN[i]= '\0' ;
fseek(fp,-1,1); /*retract*/
b=lookup (TOKEN);
if(b==0)
{
out(ID,TOKEN);
}
else
out (b," ");
//printf("%s\n",TOKEN);
}
else if(isdigit(ch))
{
TOKEN[0]=ch;
ch=fgetc(fp);
i=1;
while(isdigit(ch))
{
TOKEN[i]=ch; i++;
ch=fgetc(fp);
}
TOKEN[i]= '\0';
fseek(fp,-1,1);
out(INT,TOKEN);
}
else
switch(ch)
{
case '<': ch=fgetc(fp);
if(ch=='=')
{
//ch=fgetc(fp);
// fseek(fp,-1,1);
out(LE," ");
}
else if(ch=='>')
{
//ch=fgetc(fp);
out (NE," ");
}
else
{
fseek (fp,-1,1);
out (LT," ");
}
break;
case '=':
//ch=fgetc(fp);
out(EQ, " ");
break;
case '>': ch=fgetc(fp);
if(ch=='=')
//ch=fgetc(fp);
out(GE," ");
else
{
fseek(fp,-1,1);
out(GT," ");
}
break;
case ':' : ch=fgetc(fp);
if(ch=='=')
//ch=fgetc(fp);
out(14," ");
break;
case '/': ch=fgetc(fp);
if(ch=='*')
{
do(ch=getc(fp));
while (ch!='*');
ch=getc(fp);
if(ch=='/')
//printf("asddfghhh\n");
//ch=fgetc(fp);
{
//fseek(fp,-1,1);
zhushi();
//out(28, "");
}
else
printf("ab");
}
else
{
//ch=fgetc(fp);
out(24, "");
}
break;
case '+':
//ch=fgetc(fp);
out(21, "");
break;
case '-':
//ch=fgetc(fp);
out(22, "");
break;
case '*':
//ch=fgetc(fp);
out(23, "");
break;
case ';':
//ch=fgetc(fp);
out(25,"");
break;
case '(':
//ch=fgetc(fp);
out(26,"");
break;
case ')':
//ch=fgetc(fp);
out(27,"");
break;
default: report_error( );
break;
}
ch=fgetc (fp);
}
//ch=fgetc (fp);
//fputs(TOKEN,fp1);
getchar();
return(0);
}
//}