?? jp jp(0).c
字號:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<conio.h>
#include<malloc.h>
#define INPUT getch()
struct bl
{
char name[20];
int type;
int start;
int line[20];
int layer;
struct bl *next;
};
struct cl
{
char name[20];
int type;
int a;
float b;
char c[80];
int line[20];
int layer;
struct cl *next;
};
struct shz
{
char name[20];
int type;
int start;
int line[20];
int length;
int layer;
struct shz *next;
};
struct hsh
{
char name[20];
int type;
int line[21];
int layer;
int legal;
struct {
void *p;
int style;
}csh[5];
struct bl *a;
struct cl *b;
struct shz *c;
struct hsh *next;
};
struct cjbl
{
int layer;
struct bl *a;
struct cl *b;
struct shz *c;
struct cch *f;
struct hsh *next;
};
struct cch
{
char name[20];
char why[100];
int line[20];
int layer;
struct cch *next;
};
/*定義全局變量*/
int llayer;
int addr=0;
struct cjbl *head;
/*取單詞函數用到的變量*/
FILE * in, * out;
int fl=0,ta=0,hh=1;
char c,idl[20];
char *ch;
int doorkey=0;
/*定義結構數組*/
struct jtb
{
char name[20];
int number;
}word[47]={{"",0},{"int",1},{"char",2},{"float",3},{"void",4},{"const",5},
{"",6},{"if",7},{"else",8},{"then",9},{"repeat",10},{"until",11},{"read",12},
{"write",13},{"",14},{"",15},{"",16},{"",17},{"",18},{"",19},{"",20},{"+",21},
{"-",22},{"*",23},{"/",24},{"(",25},{")",26},{"[",27},{"]",28},{"<",29},
{">",30},{"=",31},{",",32},{";",33},{"{",34},{"}",35},{":",36},{"'",37},
{"\"",38},{".",39},{"%",40},{"?",41},{":",42},{"<=",43},{">=",44},{" ",45},{"",46}};
void getchh(void)/*取字符函數*/
{
if(in==NULL){printf("can not open the file");exit(0);}
if(feof(in)==0) c=fgetc(in);
if(feof(in)!=0){c='(';doorkey=doorkey+1;}
}
struct dqzh/*定義單詞棧*/
{
int t;
int line;
char name[20];
}getdq[3],hchzh[3]={{0,0,"@"},{0,0,"@"},{0,0,"@"}};
int chjtb(char * name)
{
int i=0;
while(i<46&&strcmp(name,word[i].name)!=0)
i=i+1;
if(i==46) return(0);
}
void getsy(void)/*取單詞函數*/
{
int state,i;
state=0;
i=0;
while(hchzh[0].t!=0)
{
fprintf(out,"\n%d %d",7,-1);hchzh[0].t--;
hchzh[0].line++;
}
if (ta!=1)
{
while(c==' ')INPUT;
while(state!=100)
{
switch(state)
{
case 0:
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) state=1;
else if (c>='0'&&c<='9') state=3;
else if (c==10) state=99;
else if (c=='<') state=16;
else if (c=='>') state=18;
else if (c==':') state=20;
else if (c=='"') state=22;
else if((c>='!'&&c<='/')||(c>='{'&&c<='~')) state=15;
else if(c>=';'||c<='?'||c=='='||(c>='['&&c<='_')) state=15;
else {state=15;}
break;
case 1:
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9'))
{idl[i]=c;i++;INPUT;}
else state=2;
break;
case 2:
state=100;
idl[i]='\0';
ch=idl;fl=1;
i=0;
break;
case 3:
if(c>='0'&&c<='9'){idl[i]=c;fl=2;i++;INPUT;}
else if(c=='.'){idl[i]=c;i++;state=5;INPUT;}
else if(c=='E'){idl[i]=c;i++;fl=2;state=7;INPUT;}
else if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){idl[i]=c;i++;fl=0;INPUT;}
else {state=4;}
break;
case 4:
state=100;
ch=idl;idl[i]='\0';
i=0;
break;
case 5:
if(c>='0'&&c<='9'){idl[i]=c;i++;INPUT;}
else if(c=='E'){idl[i]=c;i++;state=7;INPUT;}
else state=4;fl=4;
break;
case 7:
if(c>='0'&&c<='9'){idl[i]=c;i++;INPUT;}
else if(c=='+'||c=='-'){idl[i]=c;i++;INPUT;}
else state=4;
break;
case 11:
state=100;
ch=idl;;INPUT;
break;
case 15:
state=100;
idl[i]=c;i++;idl[i]='\0';
fl=3;ch=idl;i=0;INPUT;
break;
case 16:
state=17;
idl[i]=c;i++;INPUT;
break;
case 17:
state=100;
fl=3;
if(c=='>'){idl[i]=c;i++;idl[i]='\0';INPUT;}
else if(c=='='){idl[i]=c;i++;idl[i]='\0';INPUT;}
else idl[i]='\0';
break;
case 18:
state=19;
idl[i]=c;i++;INPUT;
break;
case 19:
state=100;fl=3;
if(c=='='){idl[i]=c;i++;idl[i]='\0';INPUT;}
else idl[i]='\0';
break;
case 20:
state=21;
idl[i]=c;i++;INPUT;
break;
case 21:
state=100;fl=3;
if(c=='='){idl[i]=c;i++;idl[i]='\0';INPUT;}
else idl[i]='\0';
break;
case 22:
state=100;fl=3;
idl[i]=c;i++;idl[i]='\0';
ta++;INPUT;
break;
case 99:
state=0;
strcpy(hchzh[2].name,"@");hchzh[2].t++;
i++;hh++;
INPUT;i=0;fl=7;
while(c=='')INPUT;
break;
default:state=100;
}
}
hchzh[0]=hchzh[1];
hchzh[1]=hchzh[2];
hchzh[2].t=0;
hchzh[2].line=hh;
getdq[0]=getdq[1];
getdq[1]=getdq[2];
getdq[2].t=fl;
getdq[2].line=hh;
strcpy(getdq[2].name,idl);
/*printf("\n");*/
while(c=='')INPUT;
fl=0;
}
else{
if(c=='"')ta=0;
idl[i]=c;i++;idl[i]='\0';
getdq[0]=getdq[1];
getdq[1]=getdq[2];
if(ta==0) getdq[2].t=5;
getdq[2].line=hh;
strcpy(getdq[2].name,idl);
INPUT;
if(c==10){hh++;INPUT;}
}
}
void * set(int,int *,char *);/*Set()函數聲明*/
struct cch * yfchc(char *pn,char *p)/*填出錯節點*/
{
int i;
struct cch * cchh,** cchn;
cchh=malloc(sizeof(struct cch));
i=6;
cchn=set(0,&i,"");
*cchn=cchh;
strcpy(cchh->name,pn);
strcpy(cchh->why,p);
cchh->line[0]=getdq[0].line;
cchh->line[1]=-1;
cchh->layer=llayer;
cchh->next=NULL;
return(cchh);
}/* yfchc()*/
void * set(int layer,int *type,char * name)
{
int door=0;
int ttype,i;
void *temp;
union all
{
struct bl * a;
struct cl *b;
struct shz *c;
struct hsh *d;
struct cjbl *e;
struct cch *f;
}pp,ll;
if(layer==-1)
{
if(*type==4&&strlen(name)!=0)
door=2;
}
else if(* type==0)
{
if(strlen(name)==0)
door=3;
else door=4;
}
else if(strlen(name)==0)
door=5;
else door=6;
switch(door)
{
case 2:if(head->next==NULL)
return(NULL);
pp.d=head->next;
while(pp.d!=NULL&&strcmp(pp.d->name,name)!=0)
pp.d=pp.d->next;
return(pp.d);
case 3: if(head->layer==layer){
* type=5;
return(head);
}
else{
pp.d=head->next;
while(pp.d!=NULL&&pp.d->layer!=layer)
pp.d=pp.d->next;
if(pp.d!=NULL&&pp.d->layer==layer)
{
* type=4;
return(pp.d);
}
else return(NULL);
}
case 4: i=1;
while(i<7&&set(layer,&i,name)==NULL)
i=i+1;
if(i<7)
{
* type=i;
return(set(layer,&i,name));
}
else return(NULL);
case 5: ttype=0;
temp=set(layer,&ttype," ");/*定位*/
if(temp==NULL)
return(NULL);
else{
if(ttype==5)
{
ll.e=temp;
switch(* type)
{
case 1:if(ll.e->a==NULL)
return(&ll.e->a);
else{
pp.a=ll.e->a;
while(pp.a->next!=NULL)
pp.a=pp.a->next;
return (pp.a->next);
}
case 2:if(ll.e->b==NULL)
return(&ll.e->b);
else{
pp.b=ll.e->b;
while(pp.b->next!=NULL)
pp.b=pp.b->next;
return(pp.b->next);
}
case 3:if(ll.e->c==NULL)
return(&ll.e->c);
else{
pp.c=ll.e->c;
while(pp.c->next!=NULL)
pp.c=pp.c->next;
return(pp.c->next);
}
case 6:if(ll.e->f==NULL)
return(&ll.e->f);
else{
pp.f=ll.e->f;
while(pp.f->next!=NULL)
pp.f=pp.f->next;
return(pp.f->next);
}
default:return(NULL);
}/*end switch*/
}/*end if*/
else{
ll.d=temp;
switch(* type)
{
case 1:if(ll.d->a==NULL)
return(&ll.d->a);
else{
pp.a=ll.d->a;
while(pp.a->next!=NULL)
pp.a=pp.a->next;
return(pp.a->next);
}
case 2:if(ll.d->b==NULL)
return(&ll.d->b);
else{
pp.b=ll.d->b;
while(pp.b->next!=NULL)
pp.b=pp.b->next;
return(pp.b->next);
}
case 3:if(ll.d->c==NULL)
return(&ll.d->c);
else{
pp.c=ll.d->c;
while(pp.c->next!=NULL)
pp.c=pp.c->next;
return(pp.c->next);
}
default:return(NULL);
}/*end switch*/
}/*end else*/
}/*end else*/
case 6:ttype=0;
temp=set(layer,&ttype," ");/*定位層次*/
if(temp==NULL)
return(NULL);
else{
if(ttype==5)
ll.e=temp;
else ll.d=temp;
if(ttype==4)
switch( * type){
case 1:pp.a=ll.d->a;
while(pp.a!=NULL&&strcmp(pp.a->name,name)!=0)
pp.a=pp.a->next;
return(pp.a);/*如果pp.a不等于空,說明已經找到節點,如果
pp.a等于空,剛返回空*/
case 2:pp.b=ll.d->b;
while(pp.b!=NULL&&strcmp(pp.b->name,name)!=0)
pp.b=pp.b->next;
return(pp.b);/*如果pp.b不等于空,說明已經找到節點,如果
pp.b等于空,剛返回空*/
case 3:pp.c=ll.d->c;
while(pp.c!=NULL&&strcmp(pp.c->name,name)!=0)
pp.c=pp.c->next;
return(pp.c);/*如果pp.c不等于空,說明已經找到節點,如果
pp.c等于空,剛返回空*/
default:return(NULL);
}/*end switch*/
else switch( * type)
{
case 1:pp.a=ll.e->a;
while(pp.a!=NULL&&strcmp(pp.a->name,name)!=0)
pp.a=pp.a->next;
return(pp.a);/*如果pp.a不等于空,說明已經找到節點,如果
pp.a等于空,剛返回空*/
case 2:pp.b=ll.e->b;
while(pp.b!=NULL&&strcmp(pp.b->name,name)!=0)
pp.b=pp.b->next;
return(pp.b);/*如果pp.b不等于空,說明已經找到節點,如果
pp.b等于空,剛返回空*/
case 3:pp.c=ll.e->c;
while(pp.c!=NULL&&strcmp(pp.c->name,name)!=0)
pp.c=pp.c->next;
return(pp.c);/*如果pp.c不等于空,說明已經找到節點,如果
pp.c等于空,剛返回空*/
case 6:pp.f=ll.e->f;
while(pp.f!=NULL&&strcmp(pp.f->name,name)!=0)
pp.f=pp.f->next;
return(pp.f);/*如果pp.f不等于空,說明已經找到節點,如果
pp.f等于空,剛返回空*/
default:return(NULL);
}/*end switch*/
}/*end else*/
}/*end switch*/
}/*end set*/
void bldy(int type)/*變量定義處理函數,參數接收保留字的代碼*/
{
int i;
struct shz * shzh,** shzn;
struct bl *blh, ** bln;
struct cl *clh,** cln;
struct cch *cchh,** cchn;
int style;
int typee,record;
fprintf(out,"\n%d %d",0,type);/*輸出變量類型*/
getsy();/*重讀一個單詞到輸入棧*/
record=type;
while(1)
{
typee=chjtb(getdq[0].name);
if(getdq[0].t==1&&typee==0)/*定義的變量命合法,進行變量定義處理*/
{
if (chjtb(getdq[1].name)==27)
{
shzh=malloc(sizeof(struct shz));
style=3;/*連接數組節點*/
shzn=set(llayer,&style," ");
*shzn=shzh;
strcpy(shzh->name,getdq[0].name);
shzh->type=record;
shzh->line[0]=getdq[0].line;
shzh->line[1]=-1;
shzh->layer=llayer;
shzh->next=NULL;
fprintf(out,"\n%d %d",3,shzh);/*output array name*/
getsy();/*重新讀入一個章詞到輸入棧*/
fprintf(out,"\n%d %d",0,27);/*output'['*/
getsy();/*重新讀入一個章詞到輸入棧*/
if(getdq[0].t==2&&atoi(getdq[0].name)>0)/*如果是數字并且大于0,則長度有效*/
{
shzh->length=atoi(getdq[0].name);
shzh->start=addr;
switch(record)/* count the next var`s begain address*/
{
case 1:addr=addr+2*shzh->length;break;
case 2:addr=addr+1+shzh->length;break;
case 3:addr=addr+4*shzh->length;
}/*end count*/
clh=malloc(sizeof(struct cl));/* requst a const node ,and link*/
style=2;
cln=set(llayer,&style," ");
* cln=clh;
strcpy(clh->name," ");
clh->type=1;
clh->a=shzh->length;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -