slrdfa.h

来自「编译原理SLR(1)语法分析器」· C头文件 代码 · 共 55 行

H
55
字号
#include "DATA.h"

void SLRDFA()
{
    int top_state(0),top_sign(0),i=0;
    memset(State_S,0,sizeof(State_S));
    memset(Sign_S,0,sizeof(Sign_S));
	cout<<endl<<endl<<"请输入符号串:"<<endl;
    while(cin>>a){
    top_state=0;
    top_sign=0;
    i=0;
    a[strlen(a)]='#';
    State_S[top_state++]=0;
    Sign_S[top_sign++]='#';
	cout<<endl<<"SLR分析器工作过程如下:"<<endl;
    cout<<"State          Sign          A"<<endl;
    while(1)
    {
        if(ACTION[State_S[top_state-1]][a[i]]>0 && ACTION[State_S[top_state-1]][a[i]]!=ERROR)
        {
            State_S[top_state]=ACTION[State_S[top_state-1]][a[i]];
            top_state++;
            Sign_S[top_sign++]=a[i];
            i++;
        }
        else if(ACTION[State_S[top_state-1]][a[i]]<0)
        {
            int t=-ACTION[State_S[top_state-1]][a[i]];
            t--;
            top_state-=g[t].l;
            top_sign-=g[t].l;
            State_S[top_state]=GOTO[State_S[top_state-1]][g[t].left];
            top_state++;
            Sign_S[top_sign]=g[t].left;
            top_sign++;
        }
        else if(ACTION[State_S[top_state-1]][a[i]]==0)
        {
            cout<<"ACCEPTED"<<endl;
            break;
        }
        else {cout<<"ERROR!"<<endl;break;}
        
        for(int j=0;j<top_state;j++) cout<<State_S[j];
        for(int j=0;j<15-top_state;j++) cout<<' ';
        for(int j=0;j<top_sign;j++) cout<<Sign_S[j];
        for(int j=0;j<14-top_sign;j++) cout<<' ';
        for(int j=i;j<strlen(a);j++) cout<<a[j];
        cout<<endl;
    }
	cout<<endl<<endl<<"请输入符号串:"<<endl;
}
}
 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?