递归下降分析法

#include <bits/stdc++.h>
using namespace std;
string s="i+i*i#";
string state="#E";
vector<string>v;
vector<pair<string,string>>path;
vector<string>res_v;
vector<pair<string,string>>res_path;
// G: E-〉TM
// T-〉FK
// K-〉*FK|ε
// M-〉+TM|ε
// F->i
void E(string state,string s);
bool T(string state,string s);
bool F(string state,string s);
bool K(string state,string s);
bool M(string state,string s);
void erase_ok(string &state,string &s)//一样就删除
{
    state.pop_back();
    s.erase(0, 1);
    path.push_back({state,s});
    v.push_back("");
}
void E(string state,string s)
{
    state.pop_back();
    state+="MT";
    path.push_back({state,s});
    v.push_back("E->TM");
    T(state,s);
}
bool T(string state,string s)
{
    if(state=="#"&&s=="#")
    {
        res_path=path;
        res_v=v;
        return true;
    }
    state.pop_back();
    state+="KF";
    path.push_back({state,s});
    v.push_back("T->FK");
    if(state=="#"&&s=="#")
    {
        res_path=path;
        res_v=v;
        return true;
    }
    if(F(state,s))return true;
}
bool F(string state,string s)
{
    if(state=="#"&&s=="#")
    {
        res_path=path;
        res_v=v;
        return true;
    }
    state.pop_back();
    state+="i";
    path.push_back({state,s});
    v.push_back("F->i");
    
    erase_ok(state,s);
    if(state=="#"&&s=="#")
    {
        res_path=path;
        res_v=v;
        return true;
    }
    if(K(state,s))return true;
}
bool K(string state,string s)
{
    if(state=="#"&&s=="#")
    {
        res_path=path;
        res_v=v;
        return true;
    }
    // K-〉*FK
    string c="";//临时变量,方便还原现场
    c+="K";
    state.pop_back();
    state+="KF*";
    path.push_back({state,s});
    v.push_back("K->*FK");
    //删除*
    if(state.back()==s[0])
    {
        erase_ok(state,s);
        if(state=="#"&&s=="#")
        {
            res_path=path;
            res_v=v;
            return true;
        }
        if(F(state,s))return true;
    }
    
    
    //还原现场
    for(int i=0;i<3;i++)state.pop_back();
    state+=c;
    path.pop_back();
    v.pop_back();
    
    
    state.pop_back();
    path.push_back({state,s});
    v.push_back("K->3");
    char head=state.back();
    if(state=="#"&&s=="#")
    {
        res_path=path;
        res_v=v;
        return true;
    }
    if(head=='T'&&T(state,s))return true;
    if(head=='F'&&F(state,s))return true;
    if(head=='K'&&K(state,s))return true;
    if(head=='M'&&M(state,s))return true;
    return false;
}
bool M(string state,string s)
{
    if(state=="#"&&s=="#")
    {
        res_path=path;
        res_v=v;
        return true;
    }
    // K-〉*FK
    string c="";//临时变量,方便还原现场
    c+="M";
    state.pop_back();
    state+="MT+";
    path.push_back({state,s});
    v.push_back("M->+TM");
    //删除*
    if(state.back()==s[0])
    {
        erase_ok(state,s);
        if(state=="#"&&s=="#")
        {
            res_path=path;
            res_v=v;
            return true;
        }
        if(T(state,s))return true;
    }
    
    
    //还原现场
    for(int i=0;i<3;i++)state.pop_back();
    state+=c;
    path.pop_back();
    v.pop_back();
    
    
    state.pop_back();
    path.push_back({state,s});
    v.push_back("M->3");
    char head=state.back();
    if(state=="#"&&s=="#")
    {
        res_path=path;
        res_v=v;
        return true;
    }
    if(head=='T'&&T(state,s))return true;
    if(head=='F'&&F(state,s))return true;
    if(head=='K'&&K(state,s))return true;
    if(head=='M'&&M(state,s))return true;
    return false;
}
int main()
{
    path.push_back({state,s});
    v.push_back("");
    E(state,s);
    for(int i=0;i<res_path.size();i++)
    {
        cout<<i<<' '<<res_path[i].first<<' '<<res_path[i].second<<' '<<res_v[i]<<endl;
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leimingzeOuO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值