码迷,mamicode.com
首页 > 其他好文 > 详细

LR--用栈实现移进--归约分析(demo)

时间:2018-11-10 22:40:38      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:iostream   sam   cst   alt   分享图片   als   pac   namespace   ace   

1.考虑文法

\(E->E+E\)
\(E->E*E\)
\(E->id\)

2.最右推导

不难看出,这个文法是而二义的,所以有多个最右推导

3.移进归约

用一个栈存文法符号,用输入缓存区保存要分析的输入串,用$标记栈底

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<stack>
using namespace std;
const int maxn = 15;
stack<string> stk, tmp;
string w;
bool flag = false;
int main(void) {
    cin >> w;
    w += "$";
    printf("----------|----------|----------\n");
    printf("    栈    |   输入   |    动作  \n");
    printf("----------|----------|----------\n");
    int now = 0;
    while (!flag) {
        now = 0;
        if (stk.empty()) {
            stk.push("$");
            cout << "$         |";
            cout.setf(ios::right);
            cout.width(10);
            cout << w;
            cout<< "|移进" << endl;
            printf("----------|----------|----------\n");
            string tt;
            if (w[now] == ‘i‘) {
                tt = "id";
                now = 2;
            }
            else {
                tt = w[now];
                now = 1;
            }
            stk.push(tt);
            w = w.substr(now, w.size() - now);
            continue;
        }
        while (!stk.empty()) {
            tmp.push(stk.top());
            stk.pop();
        }
        while (!tmp.empty()) {
            cout << tmp.top();
            stk.push(tmp.top());
            tmp.pop();
        }
        if (stk.top() == "id") {
            cout.width(10-stk.size());
            cout << "|";
            cout.setf(ios::right);
            cout.width(10);
            cout << w;
            cout<< "|按E-->id进行归约" << endl;
            printf("----------|----------|----------\n");
            stk.pop();
            stk.push("E");
            continue;
        }
        if (w[now]==‘$‘&&stk.size() == 2 && stk.top() == "E") {
            flag = true;
            cout<< "        |         $|接受"<< endl;
            printf("----------|----------|----------\n");
            continue;
        }
        if (w[now]!=‘$‘) {
            string tp;
            if (w[now] == ‘i‘) {
                tp = "id";
                now = 2;
            }
            else {
                tp = w[now];
                now = 1;
            }
            cout.width(11 - stk.size());
            cout << "|";
            cout.setf(ios::right);
            cout.width(10);
            cout << w;
            cout<< "|移进" << endl;
            printf("----------|----------|----------\n");
            stk.push(tp);
            w = w.substr(now, w.size() - now);
            continue;
        }
        if (w[now] == ‘$‘  &&!flag) {
            string  tc;
            tc = stk.top();
            if (tc == "E")
                stk.pop();
            tc += stk.top();
            if (stk.top() != "E") {
                stk.pop();
                tc += stk.top();
                cout.setf(ios::right);
                cout.width(9- stk.size());
                cout << "|";
                cout << "         $|";
                cout << "按E-->"<<tc<<"归约" << endl;
                printf("----------|----------|----------\n");
                stk.pop();
                stk.push("E");
            }
        }
    }
    return 0;
}

4.Sample

输入

id*id+id

技术分享图片

5.To be continued.

LR--用栈实现移进--归约分析(demo)

标签:iostream   sam   cst   alt   分享图片   als   pac   namespace   ace   

原文地址:https://www.cnblogs.com/FlyerBird/p/9940723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!