日常练习代码

来源:互联网 发布:重庆时时彩平刷软件 编辑:程序博客网 时间:2024/05/18 03:42

上下文无关文法(LR(0)文法)

文法G(S’) S’→E
E→aA|bB
A→cA|d
B→cB|d

LR(0)分析表:
这里写图片描述

#include<stdio.h>#include<string.h>char *action[12][5]={"s2#","s3#",NULL,NULL,NULL,/*ACTION表*/                      NULL,NULL,NULL,NULL,"acc",                      NULL,NULL,"s4#","s10#",NULL,                      NULL,NULL,"s5#", "s11",NULL,                      NULL,NULL,"s4#","s10#",NULL,                      NULL,NULL,"s5#", "s11",NULL,                      "r1#","r1#","r1#","r1#","r1#",                      "r2#","r2#","r2#","r2#","r2#",                      "r3#","r3#","r3#","r3#","r3#",                      "r5#","r5#","r5#","r5#","r5#",                      "r4#","r4#","r4#","r4#","r4#",                      "r6#","r6#","r6#","r6#","r6#"};int goto1[12][3]={1,0,0,/*GOTO表*/                  0,0,0,                  0,6,0,                  0,0,7,                  0,8,0,                  0,0,9,                  0,0,0,                  0,0,0,                  0,0,0,                  0,0,0,                  0,0,0,                  0,0,0};char vt[5]={'a','b','c','d','#'};/*存放终结符*/char vn[3]={'E','A','B'};        /*存放非终结符*/Char *LR[7]={"S’->E","E->aA","E->bB","A->cA","A->d","B->cB","B->d"};/*存放产生式*/int a[20];//数组a实现状态栈char b[20],c[20],c1;//数组b实现符号栈,数组c存放输入的字符串int top1,top2,top3,top,m,n;int main(){    int g,h,i,j,k,l,p,y,z,count;    char x,copy[20],copy1[20];    top1=0;top2=0;top3=0;top=0;    a[0]=0;y=a[0];b[0]='#';    count=0;z=0;    //输入要识别的字符串    printf("请输入表达式\n");    do{        scanf("%c",&c1);        c[top3]=c1;        top3=top3+1;    }while(c1!='#');    //输出分析结果    printf("步骤\t状态\t\t符号\t\t输入串\t\tACTION\n");    do{        y=z;m=0;n=0;        g=top;j=0;k=0;        x=c[top];        count++;        printf("%d\t",count);//输出步骤序号        while(m<=top1)        {                    /*输出状态*/            printf("%d",a[m]);            m=m+1;        }        printf("\t\t");        while(n<=top2)        {                    /*输出符号*/            printf("%c",b[n]);            n=n+1;        }        printf("\t\t");        while(g<=top3)        {                    /*输出输入串*/            printf("%c",c[g]);            g=g+1;        }        printf("\t\t");        while(x!=vt[j]&&j<=4)            j++;        if(j==4&&x!=vt[j])        {            printf("error\n");            return 0;        }        if(action[y][j]==NULL){            printf("error\n");            return 1;        }        else            strcpy(copy,action[y][j]);        if(copy[0]=='s')        {            z=copy[1]-'0';            top1=top1+1;            top2=top2+1;            a[top1]=z;//数组a实现状态栈            b[top2]=x;//数组b实现符号栈            top=top+1;//输入符号串数组c的顶            i=0;            while(copy[i]!='#')//输出ACTION            {                printf("%c",copy[i]);                i++;            }            printf("\n");        }        if(copy[0]=='r')        {            i=0;            while(copy[i]!='#')//输出ACTION            {                printf("%c",copy[i]);                i++;            }            h=copy[1]-'0';            strcpy(copy1,LR[h]);            while(copy1[0]!=vn[k])                k++;            l=strlen(LR[h])-4;            top1=top1-l+1;            top2=top2-l+1;            y=a[top1-1];            p=goto1[y][k];            a[top1]=p;            b[top2]=copy1[0];            z=p;            printf("\t");            printf("%d\n",p);        }    }while(action[y][j]!="acc");    printf("acc\n");    return 2;}

这里写图片描述

原创粉丝点击