用移进--规约给出对输入串abbcde#的分析过程

Posted lintianxiajun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用移进--规约给出对输入串abbcde#的分析过程相关的知识,希望对你有一定的参考价值。

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define MAXSIZE 100
typedef struct{
    char *base;
    char *top;
}Sqstack;

//手动构造预测分析表  42个字符串
char* str[5][7]={" " , "a"  , "b" , "c" , "d" , "e" , "#" ,
                 "S" , "aAcBe" , " " , " " , " " , " " , " ",
                 "A" , " " , "bH" , " " ," " , " " ," ",
                 "H" , " " , "bH" , "k" , " " , " " , " ",
                 "B" , " " , " " , " " , "d" , " " , " ",
                };


/*初始化顺序栈*/
void Creatstack(Sqstack &S)
{
    S.base = new char[MAXSIZE];/*base指向第0个元素*/
    S.top = S.base;
}

/*判断栈是否为空*/
int stackempty(Sqstack &S)
{
    if(S.base == S.top)
        return 1;
    else
        return 0;
}

/*入栈*/
void push(Sqstack &S,char e)
{
    *S.top++ = e;
}

/*出栈*/
void pop(Sqstack &S)
{
    --S.top;
}

/*获取栈顶元素*/
char gettop(Sqstack &S)
{
    --S.top;
    return *S.top;

}

/*把栈里的内容输出*/
void showstack(Sqstack &S)
{
    int i = 0;//记步
    while(S.top != S.base)
    {
        printf("%c",*S.base);
        S.base++;
        i++;
    }
    //把栈还原成原来的样子
    while(i != 0)
    {
        S.base--;
        i--;
    }
}

void showinput(char *p)
{
    int i = 0;
    while(*p != )
    {
        printf("%c ",*p);
        p++;
        i++;
    }
    //将p指向原来的位置
    while(i != 0)
    {
        p--;
        i--;
    }
}

void main()
{
    
    int test = 0;
    //建立栈
    Sqstack S;
    char ch;
    char *p;
    char *s;
    int i,j;
    int a,b;
    int z;
    int k;
    //要验证的i+i*i
    char *input = "abbcde#";
    p = input;
    Creatstack(S);

    printf("
");
    //将#入栈
    push(S,#);
    //文法开始符入栈
    push(S,S);
    while(S.base != S.top)
    {

        showstack(S);
        printf("                  ");
        showinput(p);
        printf("                  ");
        
        //找栈顶元素的和输入串的首元素
        ch = gettop(S);
        //获取之后还要还原
        S.top++;
        
        if(ch == *p && *p != )
        {
            pop(S);
            p++;
            printf("匹配成功");
            printf("
");
        }
        
        else{
                for(i = 0;i < 5;i++)
                {
                    
                    if(*str[i][0] == ch) 
                    {
                        a = i;
                    }
                }
                
                for(j = 0;j < 7;j++)
                {
                    
                    if(*str[0][j] == *p) 
                    {
                        b = j;            
                    }
                }
                
                if(str[a][b] == "k")
                {
                    pop(S);
                }
                else
                {
                    pop(S);
                    //逆序
                    s = str[a][b];
                    z = 0;
                    while(*s != )
                    {
                        s++;
                        z++;
                    }
                    s--;
                    //z--;
                    while(z != 0)
                    {
                        push(S,*s);
                        s--;
                        z--;
                    }
                    
                }
                printf("%c -> %s 
",ch,str[a][b]);//find  
        }
    }
}

显示结果:

#S                  a b b c d e #                   S -> aAcBe
#eBcAa                  a b b c d e #                   匹配成功
#eBcA                  b b c d e #                   A -> bH
#eBcHb                  b b c d e #                   匹配成功
#eBcH                  b c d e #                   H -> bH
#eBcHb                  b c d e #                   匹配成功
#eBcH                  c d e #                   H -> k
#eBc                  c d e #                   匹配成功
#eB                  d e #                   B -> d
#ed                  d e #                   匹配成功
#e                  e #                   匹配成功
#                  #                   匹配成功
Press any key to continue

 

以上是关于用移进--规约给出对输入串abbcde#的分析过程的主要内容,如果未能解决你的问题,请参考以下文章

自下而上语法分析

自下而上语法分析

13自下而上语法分析

LR分析法

编译原理随笔4(自下而上的语法分析-递归法)

12.4自下而上语法分析