#include<STDIO.H>
#include<STRING.H>
#include<CTYPE.H>
#include<IOSTREAM.H>
#define SIZE 128
#define NONE -1
#define FILENAME "c:\\cifa.txt"
char ch;
int r;
int i;
int j;
int lineno=1;
char token[SIZE];
get_char();
void cleartoken();
char lexbuf[SIZE];
void concat();
void printf_id(char s[50],int a);
void main ()
{
int f=0;
int e;
printf("lexical analysis begin now---\n");
printf("IF YOU WANT INPUT FROM THE KEYBOARD,PLEASE PUT 0!\n");
printf("IF YOU WANT INPUT FROM THE TEXT,PLEASE PUT 1!\n");
scanf("%d",&e);
if(e==1)
{
FILE *fp;
// char str[10];
// puts("Please iuput a file name()");
// gets(str);
// scanf("%s/n",&str);
// while(fopen(NAME,"r")==NULL)
// {
// puts("Error!");
// gets(str);
// }
if((fp=fopen(FILENAME,"r"))==NULL)
{
printf("cannot open this file\n");
}
else printf("File Begin:\n\n");
fp=fopen(FILENAME,"r");
while(feof(fp)==0)
{
lexbuf[f]=fgetc(fp);
++f;
}
}
//if((fp=fopen(FILENAME,"r"))==NULL)
// {
// printf("cannot open this file\n");
// }
// printf("File Begin:\n\n")
else if(e==0)
{
puts("riweir\n");
printf("please input your words.\n");
printf("uu\n");
// gets(lexbuf);
cin.get(lexbuf,SIZE);
//canf("%s",lexbuf);
printf("uu\n");
f=strlen(lexbuf);
printf("%d\n",f);
}
r=0;
do{
ch=get_char();
if(ch==' '||ch=='\t')
{
;
}
else if(ch=='\n')
{
cleartoken();
lineno++;
}
else if(isdigit(ch))
{
concat();
ch=get_char();
printf("digit ertey :\n");
while(isdigit(ch))
{
concat();
ch=get_char();
}
printf("digit 12:%s\n",token);
--r;
cleartoken();
}
else if(isalpha(ch))
{
concat();
ch=get_char();
while(isalnum(ch))
{
concat();
ch=get_char();
}
if(strcmp(token,"begin")==0)
printf_id(token,1);
else if(strcmp("end",token)==0)
printf_id(token,2);
else if(strcmp("if",token)==0)
printf_id(token,3);
else if(strcmp("then",token)==0)
printf_id(token,4);
else if(strcmp("for",token)==0)
printf_id(token,5);
else if(strcmp("do",token)==0)
printf_id(token,6);
else if(strcmp("while",token)==0)
printf_id(token,7);
else if(strcmp("var",token)==0)
printf_id(token,8);
else if(strcmp("integer",token)==0)
printf_id(token,9);
else if(strcmp("real",token)==0)
printf_id(token,10);
else printf("iden 11:%s\n",token);
--r;
cleartoken();
}
else if(ch=='(')
{
printf("symb 23: (\n");
cleartoken();
}
else if(ch=='/')
{
ch=get_char();
if(ch=='*')
{
ch=get_char();
while(ch!='*'||lexbuf[r]!='/')
{
ch=get_char();
}
ch=get_char();
cleartoken();
}
else if(ch=='/')
{
ch=get_char();
while(ch!='\n')
ch=get_char();
cleartoken();
}
else {
printf("symb 23: /\n");
--r;
}
}
else if(ch=='>')
{
ch=get_char();
if(ch=='=')
printf("symb 19:>=\n");
else { printf("symb 18:>\n");
--r;
}
cleartoken();
}
else if(ch=='<')
{
ch=get_char();
if(ch=='=')
printf("symb 21:<=\n");
else
{ printf("symb 20:<\n");
--r;
}
cleartoken();
}
else if(ch==':')
{
ch=get_char();
if(ch=='=')
printf("symb 13: :=\n");
else { printf("symb 13: :\n");
--r;}
}
else if(ch=='+')
{
printf("symb 14: +\n");
}
else if(ch=='-')
{
printf("symb 15:-\n");
}
else if(ch=='*')
{
printf("symb 16:*\n");
}
else if(ch=='=')
{
printf("symb 22: =\n");
}
else if(ch==';')
{
printf("symb 23: ;\n");
}
else if(ch==',')
{
printf("symb 24: ,\n");
}
else if(ch==')')
{
printf("symb 26: )\n");
}
}while(r<=f);
}
get_char()
{
char w;
w=lexbuf[r];
r++;
return (w);
}
void printf_id(char s[50],int n)
{
printf("word %d:%s\n",n,s);
}
void cleartoken()
{
int len;
for(len=0;len<SIZE;LEN++) pre < } token[len]="\0" len="strlen(token);" len; int { concat() void ;>
<SCRIPT src="/inc/gg_read2.js"></SCRIPT>