编译原理 之 语法分析器(自上而下分析法)

#include 
#include 
#include 

#define fprint(fpw,k) { if(k>='a'&&k<='z') fprintf(fpw,"%c",k); else fprintf(fpw,"temp%d",k); }

int PLACE=1;
int index=1;

typedef struct _info{
    int place;
    int op;
}info;

int advance(FILE *fpr)
{
    char t;
    t=fgetc(fpr);
    if(t==EOF)
        return '#';
    return t;
}

int E(FILE *fpr,FILE *fpw,int *character,info *k);
int V(FILE *fpr,FILE *fpw,int *character,info *k)
{
    int flag=0;
    if(*character>='a'&&*character<='z')
        flag=1;
    if(flag==0)
    {
        printf("error in V!\n");
        return 0;
    }
    k->place=*character;
    *character=advance(fpr);
    return 1;
}

int F(FILE *fpr,FILE *fpw,int *character,info *k)//F=(E)|V
{
    info v;
    v.place=0;
    if(*character=='(')
    {
        *character=advance(fpr);
        if(E(fpr,fpw,character,&v)==0)
            return 0;
        if(*character!=')')
        {
            printf("error in F!\n");
            return 0;
        }
        *character=advance(fpr);
    }
    else
    {
        if(V(fpr,fpw,character,&v)==0)
            return 0;;

    }
    k->place=v.place;
    return 1;
}

int T1(FILE *fpr,FILE *fpw,int *character,info *k)// T1=(*|/)F T1
{
    info f,t1;
    f.place=t1.place=0;
    if(*character=='*'||*character=='/')
    {
        if(*character=='*')
            k->op=0;
        else
            k->op=1;
        *character=advance(fpr);
        if(F(fpr,fpw,character,&f)==0)
            return 0;
        if(T1(fpr,fpw,character,&t1)==0)
            return 0;
        if(t1.place==0)
            k->place=f.place;
        else
        {
            k->place=PLACE++;
            fprintf(fpw,"%3d: ",index++);
            fprintf(fpw,"'%c',",t1.op==0?'*':'/');
            fprint(fpw,f.place);
            fprintf(fpw,",");
            fprint(fpw,t1.place);
            fprintf(fpw,",");
            fprint(fpw,k->place);
            fprintf(fpw,"\n");
        }
    }
    return 1;
}

int T(FILE *fpr,FILE *fpw,int *character,info *k)// T=F T1
{
    info f,t1;
    f.place=t1.place=0;
    if(F(fpr,fpw,character,&f)==0)
        return 0;
    if(T1(fpr,fpw,character,&t1)==0)
        return 0;
    if(t1.place==0)
        k->place=f.place;
    else
    {
        k->place=PLACE++;
        fprintf(fpw,"%3d: ",index++);
        fprintf(fpw,"'%c',",t1.op==0?'*':'/');
        fprint(fpw,f.place);
        fprintf(fpw,",");
        fprint(fpw,t1.place);
        fprintf(fpw,",");
        fprint(fpw,k->place);
        fprintf(fpw,"\n");
    }
    return 1;
}

int E1(FILE *fpr,FILE *fpw,int *character,info *k)// E1=(+|-)T E1
{
    info t,e1;
    t.place=e1.place=0;
    if(*character=='+'||*character=='-')
    {
        if(*character=='+')
            k->op=0;
        else
            k->op=1;
        *character=advance(fpr);
        if(T(fpr,fpw,character,&t)==0)
            return 0;
        if(E1(fpr,fpw,character,&e1)==0)
            return 0;
        if(e1.place==0)
            k->place=t.place;
        else
        {
            k->place=PLACE++;
            fprintf(fpw,"%3d: ",index++);
            fprintf(fpw,"'%c',",e1.op==0?'+':'-');
            fprint(fpw,t.place);
            fprintf(fpw,",");
            fprint(fpw,e1.place);
            fprintf(fpw,",");
            fprint(fpw,k->place);
            fprintf(fpw,"\n");
        }
    }
    return 1;
}

int E(FILE *fpr,FILE *fpw,int *character,info *k)// E=T E1
{
    info t,e1;
    t.place=e1.place=0;
    if(T(fpr,fpw,character,&t)==0)
        return 0;
    if(E1(fpr,fpw,character,&e1)==0)
        return 0;
    if(e1.place==0)
        k->place=t.place;
    else
    {
        k->place=PLACE++;
        fprintf(fpw,"%3d: ",index++);
        fprintf(fpw,"'%c',",e1.op==0?'+':'-');
        fprint(fpw,t.place);
        fprintf(fpw,",");
        fprint(fpw,e1.place);
        fprintf(fpw,",");
        fprint(fpw,k->place);
        fprintf(fpw,"\n");
    }
    return 1;
}

int A(FILE *fpr,FILE *fpw,int *character)// V=E
{
    info e,v;
    e.place=v.place=0;
    if(V(fpr,fpw,character,&v)==0)
        return 0;
    if(*character!='=')
    {
        printf("error in A!\n");
        return 0;
    }
    *character=advance(fpr);
    if(E(fpr,fpw,character,&e)==0)
        return 0;
    fprintf(fpw,"%3d: ",index++);
    fprintf(fpw,"'=',");
    fprint(fpw,e.place);
    fprintf(fpw,",-,");
    fprint(fpw,v.place);
    fprintf(fpw,"\n");
    return 1;
}

void scan(FILE *fpr,FILE *fpw)
{
    int character;
    index=1;
    PLACE=1;
    character=advance(fpr);
    if(A(fpr,fpw,&character)==0)
        return;
    printf("succeed!\n");
}

int main()
{
    int n;
    system("color 1E");
    FILE *fpr,*fpw;
    char filepath[256];
    while(1)
    {
    printf("选择文件输入的请按 1,结束请 2!\n");
    scanf("%d",&n);
    switch(n)
    {
        case 1:
    {
    A: printf("请输入源文件路径: ");
    scanf("%s",filepath);
    fpr=fopen(filepath,"rt");
    if(fpr==NULL)
    {
        printf("打开源文件失败!\n");
        goto A;
    }
    putchar(10);
    B:printf("请输入输出文件路径: ");
    scanf("%s",filepath);
    fpw=fopen(filepath,"wt");
    putchar(10);
    if(fpw==NULL)
    {
        printf("创建目标文件失败!\n");
        goto B;
    }
    scan(fpr,fpw);
    fclose(fpr);
    fclose(fpw);
    fpw=fopen(filepath,"rt");
    char ch;
    while(!feof(fpw))
    {
        ch=fgetc(fpw);
        printf("%c",ch);
    }
    fclose(fpw);
    break;
    }

    case 2:
    {
      printf("谢谢使用!\n\n\n");
      system("pause");
      return 0;
    }
    default: printf("输入数字错误,请重新输入数字:!\n\n\n");
    }
    }
    return 0;
}

你可能感兴趣的:(编译原理)