编译原理实验1-词法分析
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#define KEYWORDEND "waiting for your expanding"
typedef struct
{
int typenum;
char *word;
} WORD;
char input[255];
char token[255]="";
int pinput;
int ptoken;
char ch;
char *rwtab[]= {"begin","if","then","while","do","end",KEYWORDEND};
WORD *scaner();
int main()
{
int over=1;
WORD *oneword=new WORD;
printf ("enter your words(end with #):");
scanf("%[^#]s",input);
pinput=0;
printf ("your words:\n%s\n",input);
while(over<1000 && over!=-1)
{
oneword=scaner();
if (oneword->typenum<1000)
printf ("(%d,%s)",oneword->typenum,oneword->word);
over=oneword->typenum;
}
printf("\npress # to exit:");
scanf("%[^#]s",input);
return 0;
}
char mgetch() //读取一个字符到ch
{
ch=input[pinput];
pinput=pinput+1;
return(ch);
}
void getbc() //去掉空白符号
{
while(ch==' ' || ch==10)
{
ch=input[pinput];
pinput=pinput+1;
}
}
void concat()
{
token[ptoken]=ch;
ptoken=ptoken+1;
token[ptoken]='\0';
}
int letter()
{
if (ch>='a' && ch<='z'||ch>='A'&&ch<='z')return 1;
else
return 0;
}
int digit()
{
if (ch>='0' && ch<='9') return 1;
else return 0;
}
int reserve()
{
int i=0;
while(strcmp(rwtab[i],KEYWORDEND))
{
if(!strcmp(rwtab[i],token))
{
return i+1;
}
i=i+1;
}
return 10;
}
void retract()
{
pinput=pinput-1;
}
char *dtb()
{
return NULL;
}
WORD *scaner()
{
WORD *myword=new WORD;
myword->typenum=10;
myword->word="";
ptoken=0;
mgetch();
getbc();
if (letter())
{
while(letter() || digit())
{
concat();
mgetch();
}
retract();
myword->typenum=reserve();
myword->word=token;
return(myword);
}
else if(digit())
{
while(digit())
{
concat();
mgetch();
}
retract();
myword->typenum=11;
myword->word=token;
return(myword);
}
else switch(ch)
{
case'=':
mgetch();
if (ch=='=')
{
myword->typenum=39;
myword->word="==";
return(myword);
}
retract();
myword->typenum=21;
myword->word="=";
return(myword);
break;
case'+':
myword->typenum=13;
myword->word="+";
return(myword);
break;
case'-':
myword->typenum=14;
myword->word="-";
return(myword);
break;
case'*':
补充:软件开发 , C++ ,