预测分析法

来源:互联网 发布:网络采购平台 编辑:程序博客网 时间:2024/04/29 23:28

一、实验项目名称

实验二、预测分析法

二、实验目的

根据某一LL(1)文法编制调试预测分析程序,以便对任意输入的符号串进行分析。本次实验的目的主要是加深对预测分析法的理解。

三、实验环境

Cfree5.0

四、实验内容

本次实验的LL(1)文法为表达式文法:

E→E+T | TT→T*F | FF→i | (E)


 

编写识别表达式文法的合法句子的预测分析程序,对输入的任意符号串,给出分析过程及分析结果。分析过程要求输出步骤、分析栈、剩余输入串和所用产生式。如果该符号串不是表达式文法的合法句子,要给出尽量详细的错误提示。

五、实验步骤

了解预测分析法的算法,分析需要的数据结构,进行编写程序。

六、源程序清单、测试数据、结果

代码清单:

 

#include<stdio.h>#include<malloc.h>#include<stdlib.h>#include<string.h>char *pro[][6]={    "Te",""," ","Te"," "," ",    "","+Te"," "," ","$","$",    "Ft",""," ","Ft"," "," ",    " ","$","*Ft","","$","$",    "i",""," ","(E)"," "," "      };#define maxsize 50typedef char DateType;typedef struct{   DateType data[maxsize];   int top;}SeqStack;//定义栈   int i,now=0;DateType e,string1[maxsize];  void InitSt(SeqStack* L)//初始化{     L->top=0;}  char push(SeqStack* L,DateType e)//入栈{  if(L->top==maxsize)   {      printf("\n\t\t\t栈已满!");       return 0;   }  L->data[L->top]=e;   L->top++;   return 1;}  char pop(SeqStack* L)//出栈{   i=L->top;  e=L->data[i-1];   if(L->top==0)   {      printf("\n\t\t\t栈为空!");       return 0;   }   L->top--;   return 1;}  DateType getTop(SeqStack* L)//取栈顶元素{    DateType x;    i=L->top;   if(L->top==0)    {      printf("\n\t\t\t栈为空!");    }    else    {       i--;       x=L->data[i];    }    return x;} void display(SeqStack* L)//显示元素{   if(L->top==0)    {      printf("\n\t\t\t栈为空!");    }    else    {       for(i=0;i<L->top;i++)       {         printf("%c",L->data[i]);       }    }} void display1(){    int j;    for(j=now;;)    {        printf("%c",string1[j]);        j++;        if(string1[j]== '\0')            break;    }} int row(DateType x) //列匹配{    switch(x)    {        case'i':return 0;break;        case'+':return 1;break;        case'*':return 2;break;        case'(':return 3;break;        case')':return 4;break;        case'#':return 5;break;        default:return -1;    }} int col(DateType x)//行匹配{    switch(x)    {        case'E':return 0;break;        case'e':return 1;break;        case'T':return 2;break;        case't':return 3;break;        case'F':return 4;break;        default:return -1;    }} void production1(DateType b,DateType t){    int x,y;    x=col(b);    y=row(t);    printf("%c  -> %s ",b,pro[x][y]); } void match(SeqStack* L){    char ch;    ch =getTop(L);    printf("\"%c\" 匹配",ch);    pop(L);}void tuidao(SeqStack *L){    DateType stackbot,*p;    stackbot =getTop(L);    p=&string1[now];    if( *p ==stackbot)        match(L);    else        production1(stackbot,*p);  } int main(){    int time,x,y,b;    DateType X,a,str[5];    SeqStack L;    time = 0;    char c[]={" "};    InitSt(&L);//初始化栈    push(&L,'#');    push(&L,'E');    printf("请输入字符串:");    gets(string1);one:       X =getTop(&L);    a =string1[now];    b = row(X);    if( b ==0 ||b==1 || b==2 || b==3 || b==4 )        goto two;    else        goto three;two:    if ( X == a)        {            time++;            printf("%d\t",time);            display(&L);            putchar('\t');            display1();            putchar('\t');            tuidao(&L);            putchar(10);            now++;            goto one;        }    else        goto error1;/////three:    if(X == '#')        {            if(X!= a)                goto error1;            else                goto five;        }    else        goto fou;fou:    x = col(X);    y = row(a);    if(strcmp(pro[x][y],c)==0)/////        goto error1;///////    else    {        time++;        printf("%d\t",time);        display(&L);        putchar('\t');        display1();        putchar('\t');        tuidao(&L);        putchar(10);        strcpy(str,pro[x][y]);//////        pop(&L);        for(int n=strlen(str);n>0;n--)        {            push(&L,str[n-1]);            if(getTop(&L)== '$')                pop(&L);        }        goto one;    }five:    time++;    printf("%d\t",time);    display(&L);    putchar('\t');    display1();    putchar('\t');    printf("接受\n");    goto out;error1:    printf("\n出错!!\n");    goto out;out:           return 0;}  

测试数据: i+i*i#


                        海译翻译公司

原创粉丝点击