当前位置:   article > 正文

输入输出LL(1)语法分析程序_ll1文法判别c++代码

ll1文法判别c++代码

注:编译原理

题目描述

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

代码实现

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

//  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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/644604
推荐阅读
相关标签
  

闽ICP备14008679号