[代码] [C/C++]代码
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define FILENAME "e:/a.java"
enum type
{
blz,//保留字
bzf,//标识符
cs,//常数
zfcl,//字符常量
zfccl,//字符串常量
dzf,//单字符
szf//双字符
};
typedef struct output
{
int type;
union{
char *_strvalue;
char _charvalue;
int _intvalue;
float _floatvalue;
double _doublevalue;
}value;
struct output *pnext;
struct output *ppre;
} output;
int linenumber=0;
char getAChar(FILE *pfile);
char *getString(FILE *pfile);
char isDzf(char c);
output *createOuput(output *srcolutput);
char *getTypeChars(char c,FILE *pfile);
output *process(FILE *pfile);
char *constans_pool[]={
"package",
"class",
"int",
"double",
"float",
"byte",
"boolean",
"char",
"short",
"public",
"private",
"protocted",
"synchronized",
"instanceof",
"extends",
"implements",
"throw",
"throws",
"if",
"else",
"for",
"while",
"return",
"continue",
"break",
"switch",
"case","void","keyword"};
int main()
{
output *phead=NULL,*pcurt=NULL;
FILE *pfile=fopen(FILENAME,"r");
if(pfile)
phead=process(pfile);
if(phead)
{
pcurt=phead;
while(pcurt)
{
switch(pcurt->type)
{
case blz:
printf("<b>%s<b>\t",pcurt->value._strvalue);
break;
case bzf:
case zfccl:
case szf:
printf("%s\t",pcurt->value._strvalue);
break;
case zfcl:
case dzf:
printf("%c\t",pcurt->value._charvalue);
break;
case cs:
printf("%d\t",pcurt->value._intvalue);
break;
}
pcurt=pcurt->pnext;
}
}
return 0;
}
output *process(FILE *pfile)
{
char nowchar=0,prechar=0;
output *phead=NULL,*pcurt=NULL,*pnext=NULL,*ppre=NULL;
output tmp;
char *p=NULL;
char *pnowglz=NULL;
int i=0;
while(!feof(pfile))
{
prechar=nowchar;
nowchar=getAChar(pfile);
//如果是字幕
if(isalpha(nowchar))
{
p=getTypeChars(nowchar,pfile);
tmp.type=bzf;
while(strcmp(constans_pool[i],"keyword"))
{
if(strcmp(constans_pool[i],p))
{
tmp.type=blz;
break;
}
}
tmp.value._strvalue=p;
}
//如果是数字
else if(isdigit(nowchar))
{
p=getTypeChars(nowchar,pfile);
tmp.type=cs;
tmp.value._intvalue=atoi(p);
if(phead==NULL)
phead=pcurt;
}
else if(nowchar=='/')
{
nowchar=fgetc(pfile);
if(nowchar=='*')//表明是注释;
{
do
{
while(nowchar=getAChar(pfile)!='*'&&nowchar>0);
nowchar=fgetc(pfile);
}
while(nowchar!='/');
continue;
}
else if(nowchar=='/')//单行注释
{
while((nowchar=fgetc(pfile))!='\n'&&nowchar>0);
continue;
}
else
{
tmp.type=dzf;
tmp.value._charvalue='/';
ungetc(nowchar,pfile);
}
}
else if(nowchar=='=')
{
nowchar=getAChar(pfile);
if(nowchar=='=')
{
tmp.type=bzf;
*(tmp.value._strvalue)='=';
*(tmp.value._strvalue+1)='=';
}
else
{
ungetc(nowchar,pfile);
continue;
}
}
else if(nowchar=='\'')
{
nowchar=fgetc(pfile);
if(fgetc(pfile)!='\'')
printf("此处应该是单引号");
else
{
tmp.type=zfcl;
tmp.value._charvalue=nowchar;
}
}
else if(nowchar=='\"')
{
p=getString(pfile);
tmp.type=zfccl;
tmp.value._strvalue=p;
}
else if(isDzf(nowchar))
{
tmp.type=dzf;
tmp.value._charvalue=nowchar;
}
else if(nowchar<0)
// printf("end");
continue;
else
{
printf("\nerror%c\t%d\n",nowchar,linenumber);
continue;
}
phead=createOuput(&tmp);
}
return phead;
}
//判断是不是单字符
char isDzf(char c)
{
char dzfarr[]={'+','-','*','(',')','[',']','{','}',';','?','.','!','%'};
unsigned int i=0;
for(;i<sizeof(dzfarr)/sizeof(char);i++)
{
if(dzfarr[i]==c)
return dzfarr[i];
}
return 0;
}
//得到字符串常量
char *getString(FILE *pfile)
{
char nowchar[]={0,0};
char *p=NULL;
unsigned int size=4;
while((nowchar[0]=fgetc(pfile))>0&&nowchar[0]!='\"')
{
if(!p)
{
p=(char *)malloc(sizeof(char)*size);
*p=0;
}
strcat(p,nowchar);
if(strlen(p)>=size)
{
p=realloc(p,size*=2);
*(p+size/2)=0;
}
}
return p;
}
//读取同一个类型的字符加入到指针只到出现不同类型的字符
char *getTypeChars(char c,FILE *pfile)
{
char nowchar[]={c,'\0'};
char *p=NULL;
int (*pchartype)(int)=NULL;
unsigned int str_size=4;
if(isdigit(c))
pchartype=isdigit;
else if(isalnum(c))
pchartype=isalnum;
do
{
if(!p)
{
p=(char *)malloc(sizeof(char)*str_size);
*p=0;
}
strcat(p,nowchar);
if(strlen(p)>=str_size)
{
p=(char *)realloc(p,str_size*=2);
*(p+str_size/2)='\0';
}
nowchar[0]=fgetc(pfile);
}while(nowchar[0]>0&&pchartype(nowchar[0]));
ungetc(nowchar[0],pfile);
return p;
}
//创建output的链表
output *createOuput(output *srcolutput)
{
static output *pnext=NULL;
static output *ppre=NULL;
static output*phead=NULL;
output *pout=NULL;
pout=(output *)malloc(sizeof(output));
pout->pnext=NULL;
pout->type=srcolutput->type;
switch(srcolutput->type)
{
case blz:
case bzf:
case zfccl:
case szf:
pout->value._strvalue=(char *)malloc(sizeof(char)*strlen(srcolutput->value._strvalue));
*(pout->value._strvalue)=0;
strcpy(pout->value._strvalue,srcolutput->value._strvalue);
free(srcolutput->value._strvalue);
break;
case zfcl:
case dzf:
pout->value._charvalue=srcolutput->value._charvalue;
break;
case cs:
pout->value._intvalue=srcolutput->value._intvalue;
break;
}
if(!phead)
phead=pout;
if(ppre)
ppre->pnext=pout;
ppre=pout;
return phead;
}
{
jave语言}
//返回一个字符
//这个字符不为空字符。
char getAChar(FILE *pfile)
{
char c=0;
if(!pfile)
return EOF;
while((c=fgetc(pfile))=='\t'||c=='\0'||c==' '||c=='\n')
{
if(c=='\n')
linenumber++;
}
return c;
}