输入输出LL(1)语法分析程序

注:编译原理

题目描述

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

代码实现

对照分析表实现
这里写图片描述

//  sicily 2, complier course
#include <iostream>
#include <vector>
#include <cstring>
#include <string.h>
#include <iomanip>
#include <stack>
using namespace std;

#define MAXSIZE 1000

string result[MAXSIZE][MAXSIZE];
string expr[MAXSIZE];
vector<char> vt; 
vector<char> vn; 
string queStr;

int findrow(char ch) {
    int n = vn.size();
    for (int i = 0; i < n; ++i) {
        if (vn[i] == ch)
            return i;
    }
}
int findcol(char ch) {
    int n = vt.size();
    for (int i = 0; i < n; ++i) {
        if (vt[i] == ch)
            return i;
    }
}
void print(int n, stack<char> st) {
    cout << "#";
    for (int i = 0; i < n; ++i) {
        cout << queStr[i];
    }
    cout << " & ";
    while (!st.empty()) {
        cout << st.top();
        st.pop();
    }
    cout << "#" << endl;
}
int main() {
    char start;
    char ch;
    // initialize
    for (int i = 0; i < MAXSIZE; ++i)
        expr[i] = "";
    for (int i = 0; i < MAXSIZE; ++i) {
        for (int j = 0; j < MAXSIZE; ++j) {
            result[i][j] = "";
        }
    }
    //  vt and vn
    int vnnum, vtnum, exprnum, pronum;
    cin >> start;
    cin >> vnnum;
    for (int i = 0; i < vnnum; ++i) {
        cin >> ch;
        vn.push_back(ch);
    }
    cin >> vtnum;
    for (int i = 0; i < vtnum; ++i) {
        cin >> ch;
        vt.push_back(ch);
    }
    vt.push_back('#');
    vtnum++;
    //  expression
    cin >> exprnum;
    int seq;
    string str;
    for (int i = 0; i < exprnum; ++i) {
        cin >> seq >> ch >> str;
        expr[seq] = str;
    }
    //  production expression
    char vnchar, vtchar;
    int relseq;
    cin >> pronum;
    for (int i = 0; i < pronum; ++i) {
        cin >> seq >> vnchar >> vtchar >> relseq;
        result[findrow(vnchar)][findcol(vtchar)] = expr[relseq];
    }
    //  string
    string temp = "";
    char head;
    cin >> queStr;
    int strSize = queStr.size();
    stack<char> st;
    stack<char> printSt;
    st.push('E');
    int i = 0;
    print(i, st);
    while (queStr[i] != '#') {
        ch = queStr[i];
        while (!st.empty()) {
            head = st.top();
            //  match sucessfully
            st.pop();
            if (head == ch) {
                i++;
                print(i, st);
                break;
            } else {
                //  substitute
                temp = result[findrow(head)][findcol(ch)];
                if (temp != "k" && temp != "") {
                    for (int k = temp.length() - 1; k >= 0; --k) {
                        st.push(temp[k]);
                    }
                }
                print(i, st);
            }
        }
    }
    //  pop the stack until it is empty
    while (!st.empty()) {
        head = st.top();
        st.pop();
        print(i, st);
    }
    return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值