精华内容
下载资源
问答
  • LL1文法

    2008-06-02 12:36:39
    C语言写的LL1文法,编译原理
  • LL1文法实验.rar

    2021-02-20 18:01:48
    C++实现LL1文法: 1)在def.ll1中填写文法规则; 2)在test.free中填写识别字符串(会转换为token序列); 3)会打印出FIRST、FOLLOW和预测表; 4)会检测出错位置; 5)文法不能出现左递归和二义性;
  • LL1文法判断.rar

    2019-05-27 11:37:15
    该程序可对编译原理中LL1文法进行判别,如有错误,欢迎指正。
  • LL1文法程序

    2014-07-20 11:44:00
    LL1文法分析程序,基于Win32控制台程序,编译原理课程作业
  • 编译原理LL1文法实验 编译原理LL1文法实验 PAGE / NUMPAGES 编译原理LL1文法实验 实验二自上而下语法分析 实验目的和要求 根据某一文法编制调试递归下降分析程序以便对任意输入的符号串进行分析选做 根据某一文法...
  • ll1文法分析器实现c++

    2020-12-24 10:53:32
    ll1文法分析器实现c++
  • ll1文法

    2016-12-06 22:04:00
    <program>-><external_declaration> | <program> <external_declaration> <external_declaration> -><function_definition> | <...function_defin...

    <program>-><external_declaration> | <program> <external_declaration>

    <external_declaration> -><function_definition> | <declaration>

    <function_definition> -><type_specifier> <declarator> <compound_statement>

    <type_specifier>->void | char | int | float

    <declarator>-><pointer direct_declarator> | <direct_declarator>

    <Pointer>->'*'| '*' pointer

    <direct_declarator>-><IDENTIFIER> |<direct_declarator’[‘ ‘]’>
    |<direct_declarator ’[’ constant_expression ’]’>
    | <identifier '(' parameter_list ')'>
    | <IDENTIFIER '(' ')'>|<direct_declarator ‘,’ identifier_list>

    <identifier_list>-><: IDENTIFIER>| <identifier_list ',' IDENTIFIER>

    <constant_expression>-><conditional_expression>

    <parameter_list>-><parameter_declaration>| <parameter_list> <','> <parameter_declaration>

    <parameter_declaration>-><declaration_specifiers><IDENTIFIER>

    <compound_statement>-><'{'< >'}'> | <'{'><statement_list><'}'>| <'{'><declaration_list statement_list ><'}'

    <declaration_list>-><declaration>| <declaration_list><declaration>

    <Declaration>-><init_declarator>| <init_declarator_list>< ','> <init_declarator>

    <init_declarator>-><declarator>| <declarator> <'='> <initializer>

    <Initializer >-><assignment_expression>| <'{'>< initializer_list>< '}>' |< '{'>< initializer_list>< ','> < '}'>

    <initializer_program_list>-><initializer_program>| <initializer_program_list>< ','> <initializer_program>

    <statement_list>-><statement>| <statement_list> <statement>

    <Statement >->| <compound_statement> | <expression_statement> | <selection_statement> | <iteration_statement> | <jump_statement>

    <expression_statement> -> < ';'> | <expression>< ';'>

    <selection_statement> -> <:>< IF>< '('>< expression ><')'> <statement>
    | <if> <'('>< expression ><')'> <statement><else> <statement>

    <iteration_statement>-><while>< '('>< expression ><')'>< statement>
    | <for> <'('>< expression_statement>< expression_statement>< ')'>< statement>
    | <for> <'('>< expression_statement>< expression_statement>< expression ><')'>< statement>

    <jump_statement>-> | CONTINUE< ';'> | BREAK< ';'> | RETURN <';'> | RETURN <expression> <';'>

    <expression>-> <: ><assignment_expression> | <expression> <','> <assignment_expression>

    <assignment_expression>-><conditional_expression>| <unary_expression>< assignment_operator>< assignment_expression>

    <conditional_expression >-> <logical_or_expression>
    | <logical_or_expression>< '?'>< expression ><':'>< conditional_expression>

    <logical_or_expression >->< logical_and_expression> | <logical_or_expression> OR_OP <logical_and_expression>

    <logical_and_expression> -> <:>< inclusive_or_expression> | <logical_and_expression> AND_OP <inclusive_or_expression>

    <inclusive_or_expression>-> <exclusive_or_expression> | <inclusive_or_expression ><'> | <'> <exclusive_or_expression>

    <exclusive_or_expression>-><:> <and_expression>| <exclusive_or_expression> <'><^'> <and_expression>

    <and_expression>-> <:>< equality_expression> | <and_expression ><'><&><'>< equality_expression>

    <equality_expression>-><:>< relational_expression> | <equality_expression >EQ_OP <relational_expression> | <equality_expression> NE_OP <relational_expression>

    <relational_expression><:> <shift_expression> | <relational_expression> <'><<><'> <shift_expression>

    | <relational_expression '>' shift_expression>| <relational_expression LE_OP shift_expression>| <relational_expression GE_OP shift_expression>

    <shift_expression>-> <:>< additive_expression> | <shift_expression LEFT_OP additive_expression> | <shift_expression RIGHT_OP additive_expression>

    <additive_expression>-><:> <multiplicative_expression> 
    | <additive_expression> <'+'> <multiplicative_expression>
    | <additive_expression> <'-'> <multiplicative_expression>

    <multiplicative_expression> -> <:> <cast_expression>
    | <multiplicative_expression> <'*'> <cast_expression>
    | <multiplicative_expression> <'/'> <cast_expression>
    | <multiplicative_expression> <'%'> <cast_expression>

    <cast_expression>-><:>< unary_expression> | <'('>< type_name ><')'>< cast_expression>

    <unary_expression> ->< :> <postfix_expression>
    | <INC_OP unary_expression>
    | <DEC_OP unary_expression>
    | <unary_operator>< cast_expression>
    | <SIZEOF unary_expression>
    | <SIZEOF> '('>< type_name ><')'>

    <postfix_expression >-><:> <primary_expression>
    |< postfix_expression> <'['>< expression ><']'>
    | <postfix_expression ><'('>< ')'>
    | <postfix_expression '(' argument_expression_list ')'>
    | <postfix_expression '.' IDENTIFIER>
    | <postfix_expression PTR_OP IDENTIFIER>
    | <postfix_expression INC_OP>
    | <postfix_expression DEC_OP>

    <primary_expression>->IDENTIFIER | CONSTANT | STRING_LITERAL | <'('>< expression ><')'>

    <argument_expression_list>-> < :>< assignment_expression> | <argument_expression_list> <','> <assignment_expression>

    <unary_operator>-> <:>< '&'> | <'*'> | <'+'> | <'-'>| <'~'> | <'!'>

    <storage_class_specifier >-> TYPEDEF | EXTERN | STATIC | AUTO | REGISTER

    <struct_or_union_specifier> ->< :>< struct_or_union IDENTIFIER>< '{'>< struct_declaration_list>< '}'>
    | <struct_or_union ><'{' ><struct_declaration_list>< '}'>
    | <struct_or_union> IDENTIFIER

    <struct_or_union>-> <: >STRUCT | UNION

    <struct_declaration_list> -> <:>< struct_declaration> | <struct_declaration_list ><struct_declaration>

    <struct_declaration> -><:>< specifier_qualifier_list>< struct_declarator_list>< ';'>

    <specifier_qualifier_list >-> < type_specifier>< specifier_qualifier_list>
    | <type_specifier>
    | <type_qualifier ><specifier_qualifier_list>
    | <type_qualifier>

    <struct_declarator_list>-><struct_declarator>
    | <struct_declarator_list>< ','>< struct_declarator>

    <struct_declarator>-> <:>< declarator> |< ':'>< constant_expression>
    | <declarator ><':'> <constant_expression>

    <enum_specifier >-> < ENUM '{' enumerator_list '}'>
    | <ENUM IDENTIFIER '{' enumerator_list '}'>
    | ENUM IDENTIFIER

    <enumerator_list >-> enumerator | enumerator_list <','> enumerator

    <Enumerator >->IDENTIFIER | IDENTIFIER <'='> <constant_expression>

    <type_qualifier >->CONST | VOLATILE

    <type_qualifier_list>-> <type_qualifier> | <type_qualifier_list> <type_qualifier>

    <parameter_type_list>-> <parameter_list> | <parameter_list ><','> ELLIPSIS

    <parameter_list>-> <:>< parameter_declaration> | <parameter_list>< ','>< parameter_declaration>

    <type_name>->< specifier_qualifier_list> | <specifier_qualifier_list>< abstract_declarator>

    <abstract_declarator>-> pointer | <direct_abstract_declarator> | <pointer direct_abstract_declarator>

    转载于:https://www.cnblogs.com/jianzhihao/p/6139300.html

    展开全文
  • LL1文法判定

    2013-05-04 20:51:45
    c实现的LL1文法的判定,按书上的步骤一步步实现的,文法支持手动输入。
  • java 编译原理 ll1 文法分析 first follow select 集的 求解
  • LL1文法分析器设计步骤 逐步知道 简单易懂 CSDN上同有相应的代码下载“LL1文法的实现”。。
  • 语法分析器(LL1文法) 语法分析器(LL1文法
  • 编译原理-LL1文法分析-java
  • 编译原理LL1文法MFC

    2015-06-30 17:02:27
    编译原理开发设计,采用LL1文法,界面MFC设计。
  • 本实验是对LL1文法的代码实现,仅供学习交流参考使用,禁止用于商业用途,如有错误,请批评斧正.同时欢迎各位大佬交流
  • LL1文法及分析表

    2012-05-11 03:45:56
    LL1文法及分析表
  • 编译原理-LL1文法分析-java
  • c++实现LL1 LL1文法 编译原理课设
  • 编译原理 LL1文法分析

    2010-12-11 16:59:01
    很好的LL1文法分析工程,详细解析了LL1文法分析的过程。
  • 编译原理LL1文法实现

    2012-12-23 16:41:08
    编译原理LL1文法实现,使用D盘下TXT文档输入字符串例子
  • C语言实现的LL1文法判别,及first、follow、select集合计算,编译原理实验要求,自己实现的,可能会有不完善的地方,欢迎讨论~~~
  • 编译原理课程设计,开发的一款基于java的语法分析器,用到LL1文法
  • 对于一个给定的文法,求出它的First和Follow集合。判断它是否为LL1文法,若不是,请给出理由。若是LL1文法,对于给定的句子,通过构造该句子的推导过程,通过推导过程,判断它是不是该LL1文法的句型。
  • C#实现LL1文法编译原理课设
  • 编译原理 ll1文法

    2009-10-28 22:37:21
    编译原理ll1文法。c#语言编写.绝对好使!
  • 本次上传的是编译原理语法分析LL1文法程序部分,耗费了我2个星期的时间,真的是煞费苦心。里面增加了很多注释,大家应该能够看懂,有需要的朋友赶紧下载吧!希望对大家有所帮助!!!
  • 用C语言编写实现编译原理实验判断文法是不是LL1文法的程序。程序简单易懂,且基本功能都实现了。
  • LL1文法分析 JAVA实现

    2010-07-03 21:26:27
    LL1文法分析程序 Java实现 图形界面
  • LL1文法分析 c++ 程序

    2010-12-01 15:55:58
    LL1 文法分析 程序 可以通过此程序 实现简单的 如:i+i*i 这些文法LL1 分析过程!
  • c++实现LL1文法

    千次阅读 2019-11-12 21:00:49
    打造简单的编译器(二)——语法分析(LL1文法) #include <iostream> #include <map> #include <vector> #include <set> #include <list> #include <cstdio> #include <...

    简介

    利用c++计算文法的firsr,follow,select 集合,然后判断文法是否为LL1文法,并构造预测分析表,对输入字符串进行分析

    参考代码

    #include <iostream>
    #include <map>
    #include <vector>
    #include <set>
    #include <list>
    #include <cstdio>
    #include <iomanip>
    using namespace std;
    
    typedef char grammarType;
    grammarType start, S;                //开始符号,S用来作为输入
    map<grammarType, int> ter;           //终结符集合
    vector<grammarType> terInx;          //通过索引查找终结符
    map<grammarType, int> nonter;        //非终结符集合
    vector<grammarType> nontInx;         //通过索引查找非终结符
    vector<grammarType> language;        //需要进行分析的语言,即输入字符串
    int terNum, nontNum, proSize, Lsize; //终结符数目,非终结符数目,产生式数目,语言的长度
    struct Production
    {
        int leftPart;          //左部非终结符的索引
        vector<int> rightPart; // 右边产生式索引
        vector<bool> flag;     //标记右边相关产生式是终结符(false),非终结符(false)
    };
    vector<Production> pro; //产生式
    vector<int> *nontPro;   // 各个非终结符的产生式的集合
    vector<bool> proEmpty;  //记录各个产生式能否推出空 true能,false不能
    set<int> *First;        //各个非终结符的First集合
    #define FirstIt(x) for (itSet = First[(x)].begin(); itSet != First[(x)].end(); ++itSet)
    set<int> *Follow; // 各个非终结符的Follow集合
    #define FollowIt(x) for (itSet = Follow[(x)].begin(); itSet != Follow[(x)].end(); ++itSet)
    set<int> *Select; //各个产生式的Select的集合
    #define SelectIt(x) for (itSet = Select[(x)].begin(); itSet != Select[(x)].end(); ++itSet)
    set<int>::iterator itSet; // set迭代集合
    struct edge
    {
        int v, s; //u->v一条边 s代表状态,1 终结符,
        edge(int v, int s) : v(v), s(s) {}
    };                         //利用关系图法求解First
    vector<int> *graph;        //图
    int *inDe;                 //计算入度
    vector<int> top;           //定义出栈顺序,即计算顺序
    vector<int> nontEmpty;     //能否推出空,0未定,1代表是,2代表否
    vector<int> *analyzeTable; //构造LL1文法的预测分析表
    struct Sign
    {
        int Inx;
        bool flag; //符合索引 表示文法分析栈中的字符是否为非终结符 true 非终结符
        Sign(int a, bool b) : Inx(a), flag(b) {}
        Sign() {}
    };
    vector<Sign> analysisStack; //用数组模拟LL1文法分析栈
    #define out(width, str) cout << setw((width)) << setiosflags(ios::left) << ((str))
    void getEmpty();        //求非终结符能否推出空集
    void geFirst();         //计算文法First集合
    void getFollow();       //计算文法Follow集合
    void getSelect();       //计算产生式的Select集合
    bool judegLL1();        //判断是否为LL1文法
    void getAnalyzeTable(); //得到LL1文法的预测分析表
    void analysis();        //对所得语言进行分析
    int main()
    {
        cout << "请输入文法开始符合:";
        cin >> start;
        cout << "请输入非终结符的数目:";
        cin >> nontNum;
        First = new set<int>[nontNum];
        Follow = new set<int>[nontNum];
        nontPro = new vector<int>[nontNum];
        cout << "请依次输入非终结符:";
        for (int i = 0; i < nontNum; ++i)
        {
            cin >> S;
            nonter[S] = i;
            nontInx.push_back(S);
        }
        cout << "请输入终结符的数目:";
        cin >> terNum;
        cout << "请依次输入终结符:(用二个符合表示空和结束符并放在末尾)";
        for (int i = 0; i < terNum; ++i)
        {
            cin >> S;
            ter[S] = i;
            terInx.push_back(S);
        }
        cout << "请依次输入产生式的数目:";
        cin >> proSize;
        Select = new set<int>[proSize];
        proEmpty.insert(proEmpty.begin(), proSize, false);
        cout << "请依次输入产生式:";
        int t;
        Production p;
        for (int i = 0; i < proSize; ++i)
        {
            cin >> S >> t;
            p.leftPart = nonter[S];
            nontPro[p.leftPart].push_back(i);
            for (int j = 0; j < t; ++j)
            {
                cin >> S;
                if (nonter.find(S) != nonter.end())
                {
                    p.rightPart.push_back(nonter[S]);
                    p.flag.push_back(true);
                }
                else
                {
                    p.rightPart.push_back(ter[S]);
                    p.flag.push_back(false);
                }
            }
            pro.push_back(p);
            p.rightPart.clear();
            p.flag.clear();
        }
        getEmpty();
        geFirst();
        getFollow();
        getSelect();
        if (judegLL1() == false)
            return 0;
        getAnalyzeTable();
        cout << "请输入要分析的语言的长度: ";
        cin >> Lsize;
        cout << "请输入要分析的语言(带输入结束符):";
        for (int i = 0; i < Lsize; ++i)
        {
            cin >> S;
            language.push_back(S);
        }
        analysis();
    }
    void getEmpty()
    {
        nontEmpty.clear();
        nontEmpty.insert(nontEmpty.begin(), nontNum, 0); //初始化
        vector<int> nonProNum(nontNum, 0);
        list<int> wait;
        int nul = terNum - 2, j, i, ltmp;
        for (int i = 0; i < proSize; ++i)
        {
            for (j = 0, ltmp = pro[i].leftPart; j < pro[i].flag.size(); ++j)
            {
                if (pro[i].flag[j] == false && pro[i].rightPart[j] == nul)
                    nontEmpty[ltmp] = 1;
                if (pro[i].flag[j] == false)
                    break;
            }
            if (j == pro[i].flag.size())
            {
                wait.push_front(i);
                nonProNum[ltmp]++;
            }
        }
        for (int i = 0; i < nontNum; ++i)
        {
            if (nontEmpty[i] == 0 && nonProNum[i] == 0)
                nontEmpty[i] = 2;
        }
        list<int>::iterator itlist = wait.begin();
        while (itlist != wait.end())
        {
            ltmp = pro[*itlist].leftPart;
            if (nontEmpty[ltmp])
                itlist = wait.erase(itlist), nonProNum[ltmp]--;
            else
                ++itlist;
        }
        itlist = wait.begin();
        while (!wait.empty())
        {
            if (itlist == wait.end())
                itlist = wait.begin();
            ltmp = pro[*itlist].leftPart;
            if (nontEmpty[ltmp])
            {
                itlist = wait.erase(itlist);
                continue;
            }
            for (j = 0, i = *itlist; j < pro[i].flag.size(); ++j)
            {
                if (nontEmpty[pro[i].rightPart[j]] == 2)
                {
                    itlist = wait.erase(itlist);
                    nonProNum[ltmp]--;
                    if (nonProNum[ltmp] == 0)
                        nontEmpty[ltmp] = 2;
                    break;
                }
                else if (nontEmpty[pro[i].rightPart[j]] == 0)
                {
                    itlist++;
                    break;
                }
            }
            if (j == pro[i].flag.size())
            {
                nontEmpty[ltmp] = 1;
                itlist = wait.erase(itlist);
            }
        }
    }
    void topSort()
    {
        int *sta = new int[nontNum];
        int p = 0, v, u;
        top.clear();
        for (int i = 0; i < nontNum; ++i)
            if (inDe[i] == 0)
                sta[p++] = i;
        while (p)
        {
            v = sta[--p];
            top.push_back(v);
            for (int i = 0; i < graph[v].size(); ++i)
            {
                u = graph[v][i];
                inDe[u]--;
                if (!inDe[u])
                    sta[p++] = u;
            }
        }
    }
    void geFirst()
    {
        int i, j, ltmp, rtmp;
        delete[] graph;
        graph = new vector<int>[nontNum];
        delete[] inDe;
        inDe = new int[nontNum];
        for (i = 0; i < nontNum; ++i)
            inDe[i] = 0;
        for (i = 0; i < proSize; ++i)
        {
            for (j = 0, ltmp = pro[i].leftPart; j < pro[i].flag.size(); ++j)
            {
                rtmp = pro[i].rightPart[j];
                if (pro[i].flag[j])
                {
                    graph[rtmp].push_back(ltmp);
                    inDe[ltmp]++;
                    if (nontEmpty[rtmp] == 2)
                        break;
                }
                else if (rtmp != terNum - 2)
                {
                    First[ltmp].insert(rtmp);
                    break;
                }
            }
        }
        topSort();
        for (i = 0; i < top.size(); ++i)
        {
            for (ltmp = top[i], itSet = First[ltmp].begin(); itSet != First[ltmp].end(); ++itSet)
            {
                for (j = 0; j < graph[ltmp].size(); ++j)
                    First[graph[ltmp][j]].insert(*itSet);
            }
        }
        for (int i = 0; i < nontNum; ++i)
            if (nontEmpty[i] == 1)
                First[i].insert(terNum - 2);
        cout << "各个非终结符的First集为: " << endl;
        for (int i = 0; i < nontNum; ++i)
        {
            cout << "First( " << nontInx[i] << " ) = { ";
            FirstIt(i) cout << terInx[*itSet] << " ";
            cout << "}\n";
        }
    }
    void getFollow()
    {
        int i, j, ltmp, rtmp, folFlag;
        delete[] graph;
        graph = new vector<int>[nontNum];
        delete[] inDe;
        inDe = new int[nontNum];
        for (i = 0; i < nontNum; ++i)
            inDe[i] = 0;
        Follow[nonter[start]].insert(terNum - 1);
        for (i = 0; i < proSize; ++i)
        {
            folFlag = true;
            ltmp = pro[i].leftPart;
            for (j = pro[i].flag.size() - 1; j > 0; --j)
            {
                rtmp = pro[i].rightPart[j];
                if (pro[i].flag[j - 1])
                {
                    if (pro[i].flag[j])
                    {
                        FirstIt(rtmp) if (*itSet != terNum - 2) Follow[pro[i].rightPart[j - 1]].insert(*itSet);
                    }
                    else
                        Follow[pro[i].rightPart[j - 1]].insert(rtmp), folFlag = false;
                }
                if (folFlag)
                {
                    graph[ltmp].push_back(rtmp);
                    inDe[rtmp]++;
                    if (nontEmpty[rtmp] == 2)
                        folFlag = false;
                }
            }
            if (pro[i].flag[0] && folFlag)
            {
                graph[ltmp].push_back(pro[i].rightPart[0]);
                inDe[rtmp]++;
            }
        }
        inDe[nonter[start]] = 0;
        topSort();
        for (i = 0; i < top.size(); ++i)
        {
            for (ltmp = top[i], itSet = Follow[ltmp].begin(); itSet != Follow[ltmp].end(); ++itSet)
            {
                for (j = 0; j < graph[ltmp].size(); ++j)
                    Follow[graph[ltmp][j]].insert(*itSet);
            }
        }
        cout << "各个非终结符的Follow集为: " << endl;
        for (i = 0; i < nontNum; ++i)
        {
            cout << "Follow( " << nontInx[i] << " ) = { ";
            FollowIt(i) cout << terInx[*itSet] << " ";
            cout << "}\n";
        }
    }
    
    void getSelect()
    {
        int i, j, ltmp, rtmp;
        for (i = 0; i < proSize; ++i)
        {
            for (j = 0, ltmp = pro[i].leftPart; j < pro[i].flag.size(); ++j)
            {
                if (pro[i].flag[j])
                {
                    rtmp = pro[i].rightPart[j];
                    FirstIt(rtmp) Select[i].insert(*itSet);
                    if (nontEmpty[rtmp] == 2)
                        break;
                }
                else
                {
                    Select[i].insert(pro[i].rightPart[j]);
                    break;
                }
            }
            if (pro[i].flag[0] == false && pro[i].rightPart[0] == terNum - 2)
            {
                FollowIt(ltmp) Select[i].insert(*itSet);
                proEmpty[i] = true;
            }
            if (j == pro[i].flag.size())
            {
                FollowIt(ltmp) Select[i].insert(*itSet);
                proEmpty[i] = true;
            }
            Select[i].erase(terNum - 2);
        }
        cout << "各个产生式的Select集为: " << endl;
        for (i = 0; i < proSize; ++i)
        {
            cout << "Select( " << nontInx[pro[i].leftPart] << "->";
            for (j = 0; j < pro[i].flag.size(); ++j)
                if (pro[i].flag[j])
                    cout << nontInx[pro[i].rightPart[j]];
                else
                    cout << terInx[pro[i].rightPart[j]];
            cout << ") = { ";
            SelectIt(i) cout << terInx[*itSet] << " ";
            cout << "}\n";
        }
    }
    
    bool judegLL1()
    {
        for (int i = 0; i < nontNum; ++i)
        {
            for (int j = 0; j < nontPro[i].size() - 1; ++j)
            {
                for (int t = j + 1; t < nontPro[i].size(); ++t)
                {
                    if (proEmpty[j] && proEmpty[t])
                        continue; //两个产生式都能产生空
                    for (itSet = Select[nontPro[i][j]].begin(); itSet != Select[nontPro[i][j]].end(); ++itSet)
                    {
                        if (Select[nontPro[i][t]].find(*itSet) != Select[nontPro[i][t]].end())
                        {
                            cout << terInx[*itSet] << "是产生式" << nontPro[i][j] << "和" << nontPro[i][t] << "的Select集合的交集,不是LL1文法\n";
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
    void getAnalyzeTable()
    {
        analyzeTable = new vector<int>[nontNum]; //初始化-1,nontNum-1 表示#
        for (int i = 0; i < nontNum; ++i)
            analyzeTable[i].insert(analyzeTable[i].begin(), terNum, -1);
        for (int i = 0; i < proSize; ++i)
        {
            SelectIt(i) analyzeTable[pro[i].leftPart][*itSet] = i;
        }
        cout << "该文法的预测分析表为:\n";
        out(10, " ");
        string outStr;
        for (int i = 0; i < terNum; ++i)
            out(10, terInx[i]);
        cout << endl;
        for (int i = 0; i < nontNum; ++i)
        {
            out(10, nontInx[i]);
            for (int j = 0; j < terNum; ++j)
                if (analyzeTable[i][j] == -1)
                    out(10, " ");
                else
                {
                    outStr = "->";
                    for (int t = analyzeTable[i][j], c = 0; c < pro[t].flag.size(); ++c)
                        if (pro[t].flag[c])
                            outStr += nontInx[pro[t].rightPart[c]];
                        else
                            outStr += terInx[pro[t].rightPart[c]];
                    out(10, outStr);
                }
            cout << endl;
        }
    }
    
    void analysis()
    {
        int i = 0, ip = 0, outW = 30;
        Sign X;
        string outS = "";
        out(10, "步骤");
        out(outW, "分析栈");
        out(outW, "剩余输入串");
        out(outW, "所用产生式");
        cout << endl;
        analysisStack.clear();
        analysisStack.push_back(Sign(terNum - 1, false));
        analysisStack.push_back(Sign(nonter[start], true)); //对分析栈进行初始化
        while (++i)
        {
            X = analysisStack.back();
            analysisStack.pop_back();
            if (X.flag == false && X.Inx == terNum - 1)
            {
                if (terInx[X.Inx] == language[ip])
                {
                    out(10, i);
                    out(outW, terInx[terNum - 1]);
                    out(outW, terInx[terNum - 1]);
                    out(outW, "接受");
                    cout << "\n";
                    return;
                }
                else
                {
                    cout << "你输入的语言该文法无法识别\n";
                    return;
                }
            }
            else if (X.flag == false)
            {
                if (terInx[X.Inx] == language[ip])
                {
                    outS = "";
                    for (int t = 0; t < analysisStack.size(); ++t)
                        if (analysisStack[t].flag)
                            outS += nontInx[analysisStack[t].Inx];
                        else
                            terInx[analysisStack[t].Inx];
                    outS += terInx[X.Inx];
                    out(10, i);
                    out(outW, outS);
                    outS = "";
                    for (int t = ip; t < language.size(); ++t)
                        outS += language[t];
                    out(outW, outS);
                    outS = "";
                    outS += language[ip];
                    out(outW, outS + "匹配");
                    cout << endl;
                    ++ip;
                }
                else
                {
                    cout << "你输入的语言该文法无法识别\n";
                    return;
                }
            }
            else
            {
                int pos = analyzeTable[X.Inx][ter[language[ip]]];
                if (pos == -1)
                {
                    cout << "你输入的语言该文法无法识别\n";
                    return;
                }
                outS = "";
                for (int t = 0; t < analysisStack.size(); ++t)
                    if (analysisStack[t].flag)
                        outS += nontInx[analysisStack[t].Inx];
                    else
                        terInx[analysisStack[t].Inx];
                outS += terInx[X.Inx];
                out(10, i);
                out(outW, outS);
                outS = "";
                for (int t = ip; t < language.size(); ++t)
                    outS += language[t];
                out(outW, outS);
                outS = "";
                outS += nontInx[X.Inx];
                outS += "->";
                for (int t = 0; t < pro[pos].flag.size(); ++t)
                    if (pro[pos].flag[t])
                        outS += nontInx[pro[pos].rightPart[t]];
                    else
                        outS += terInx[pro[pos].rightPart[t]];
                out(outW, outS);
                cout << endl;
                if (pro[pos].flag[0] == false && proEmpty[pos])
                    continue;
                for (int t = pro[pos].flag.size() - 1; t >= 0; --t)
                    if (pro[pos].flag[t])
                        analysisStack.push_back(Sign(pro[pos].rightPart[t], true));
                    else
                        analysisStack.push_back(Sign(pro[pos].rightPart[t], false));
            }
        }
    }
    

    测试

    输入数据

    S 
    5   S A B C D
    5   a b c @ # 
    10
    S 2 A B
    S 2 b C
    A 1 @
    A 1 b
    B 1 @
    B 2 a D
    C 2 A D
    C 1 b
    D 2 a S
    D 1 c
    

    截屏

    在这里插入图片描述
    在这里插入图片描述

    展开全文

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 856
精华内容 342
关键字:

ll1文法