SLR(1)分析法

目录

1.基本概念

2.如何构造SLR(1)分析表

3.ACTION表和GOTO表的构造步骤

4.冲突解决办法


1.基本概念

(1)按上述方法构造出的ACTION与GOTO表如果不含多重入口,则称该文法为SLR(1)文法。

(2)使用SLR表的分析器叫做一个SLR分析器。

(3)每个SLR(1)文法都是无二义的。但也存在许多无二义文法不是SLR(1)的。

(4)LR(0)∈ SLR(1)∈无二义文法 


 

2.如何构造SLR(1)分析表

(1)把G拓广为G'

(2)对G'构造

        1.LR(O)项目集规范族C 

        2.活前缀识别自动机的状态转换函数GO

(3)使用C和GO,构造SLR分析表

        1.令每个项目集&的下标k作为分析器的状态,包含项目S'→·S的集合I的下标k为分析器             的初态。

        2.构造分析表的ACTION和GOTO子表


 

3.ACTION表和GOTO表的构造步骤

前面我在一篇文章中也讲到了如何来构造ACTION和GOTO,当时是为了构造LR(0)表https://blog.csdn.net/qq_55168827/article/details/124385670https://blog.csdn.net/qq_55168827/article/details/124385670现在我们是为了构造SLR(1),他们的过程有很多的相同点,但是也有不同点,下面的表就具体的说明了他们的构造过程。

大家可以看出,他们的第二步有所不同,在专业术语中,SLR(1)多了一步解决冲突的步骤,具体是怎么来解决冲突的,我后面会和大家讲解。


 

4.冲突解决办法

大家现在看了可能有点懵,我马上来结合一道例题和大家讲解。

5.例题分析

怎么根据文法G来画出识别活前缀的DFA和LR(0)项目集规范族是怎么求出来的,我们现在不做过多阐述,今天我们主要来看如何去构造SLR(0)分析表,我们第一步就是要解决冲突,我们分析已经求出来的LR(0)项目集规范族可以看出I1,I2,I9的第一个项目都是规约项目,而第二个项目都是移进项目,产生了冲突,需要解决冲突。

 

这是刚开始的ACTION和GOTO表,我们先来看状态0,还是老规矩,遇到移进就填入ACTION表中,遇到规约就去找是第几个推导式,然会填入GOTO表中

下面我们来看看状态1的情况,状态里面有两个项目,一个是归约项目,一个是移进项目,因此有冲突,我们检查一下要归约生成的Follow(S'),里面有#号,所以说这个归约项目告诉我们在状态1面临#的时候应该选择归约,而这个归约呢又是个接受项目。所以就是整个分析结束的接受标志。而第二个项目告诉我们,这是个移进项目,从I1出发识别+之后,DFA告诉我们应该转入6,所以1这一行+这一列选的应该是移入,转入6,虽然状态1里面有一个移进项目,一个规约项目,但是我们通过检查Follow集合,我时把这两个动作给它区分来了。没有造成冲突。

 

 

下面我们接着看状态2,状态2也有一个规约项目,一个移进项目,E的FOLLOW集合有三个元素,+,),#,所以说第一个项目告诉我们,当状态2处于栈顶,面临的输入符号是+,),#,你就可以按照第一个项目规约,我们可以发现它是第二个产生式,所以说在2的这一行,+,),#这些列都写上用第二个产生式做规约,填上r2,再看第二个项目,当状态2处于栈顶面临*的时候,就应该是移入了,它正好与E的FOLLOW集元素不重叠,可以移入。 

 后面的步骤都与之前的是一样的,我直接提供答案了。

  • 21
    点赞
  • 101
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
SLR(1)分析法是一种自底向上的语法分析方法,可以通过一个确定性有限自动机(DFA)来实现。以下是一个简单的SLR(1)分析器的C++代码示例: ```cpp #include <iostream> #include <stack> #include <vector> #include <unordered_map> using namespace std; // 语法规则 struct Production { char lhs; // 左部 string rhs; // 右部 Production(char lhs, string rhs) : lhs(lhs), rhs(rhs) {} }; // LR(0) 项 struct Item { Production prod; // 产生式 int dot; // 点的位置 Item(Production prod, int dot = 0) : prod(prod), dot(dot) {} // 判断两个项是否相等 bool operator==(const Item& other) const { return prod.lhs == other.prod.lhs && prod.rhs == other.prod.rhs && dot == other.dot; } }; // 计算闭包 void closure(Item item, const vector<Production>& prods, unordered_map<char, vector<Item>>& items) { // 初始状态 items[item.prod.lhs].push_back(item); // 计算闭包 stack<Item> stk; stk.push(item); while (!stk.empty()) { Item cur = stk.top(); stk.pop(); if (cur.dot < cur.prod.rhs.size()) { // 当前项还有符号未处理 char next_sym = cur.prod.rhs[cur.dot]; if (isupper(next_sym)) { // 下一个符号是非终结符 for (Production prod : prods) { if (prod.lhs == next_sym) { // 找到所有以该符号为左部的产生式 Item next_item(prod, 0); if (find(items[next_item.prod.lhs].begin(), items[next_item.prod.lhs].end(), next_item) == items[next_item.prod.lhs].end()) { items[next_item.prod.lhs].push_back(next_item); stk.push(next_item); } } } } } } } // 计算GOTO函数 unordered_map<char, unordered_map<char, vector<Item>>> goto_func(const vector<Production>& prods, const unordered_map<char, vector<Item>>& items) { // 初始化GOTO unordered_map<char, unordered_map<char, vector<Item>>> goto_map; for (auto& item_list : items) { char lhs = item_list.first; for (Item item : item_list.second) { if (item.dot < item.prod.rhs.size()) { char next_sym = item.prod.rhs[item.dot]; if (goto_map[lhs].count(next_sym) == 0) { goto_map[lhs][next_sym] = vector<Item>(); } } } } // 计算GOTO for (auto& item_list : items) { char lhs = item_list.first; for (Item item : item_list.second) { if (item.dot < item.prod.rhs.size()) { char next_sym = item.prod.rhs[item.dot]; if (isupper(next_sym)) { // 下一个符号是非终结符 for (Item next_item : items.at(next_sym)) { if (next_item.prod.lhs == next_sym) { goto_map[lhs][next_sym].push_back(next_item); } } } } } } return goto_map; } // 构造LR(1)自动机 unordered_map<int, unordered_map<char, int>> construct_lr1(const vector<Production>& prods) { // 计算所有LR(0)项 Item start_item(Production('S', "E"), 0); unordered_map<char, vector<Item>> items{{start_item.prod.lhs, vector<Item>{start_item}}}; closure(start_item, prods, items); // 计算GOTO函数 unordered_map<char, unordered_map<char, vector<Item>>> goto_map = goto_func(prods, items); // 初始化DFA unordered_map<int, unordered_map<char, int>> dfa{{0, unordered_map<char, int>()}}; stack<int> stk; stk.push(0); // 构造DFA int cnt = 0; while (!stk.empty()) { int cur_state = stk.top(); stk.pop(); for (auto& sym_items : goto_map) { char sym = sym_items.first; unordered_map<char, vector<Item>>& items = sym_items.second; if (items.empty()) continue; unordered_map<char, vector<Item>> new_items; for (Item item : items[sym]) { new_items[item.prod.lhs].push_back(Item(item.prod, item.dot + 1)); } closure(Item(Production('\0', ""), 0), prods, new_items); int next_state; if (dfa.count(cur_state) && dfa[cur_state].count(sym)) { next_state = dfa[cur_state][sym]; } else { next_state = ++cnt; dfa[cur_state][sym] = next_state; dfa[next_state] = unordered_map<char, int>(); stk.push(next_state); } for (auto& item_list : new_items) { char lhs = item_list.first; int next_state2 = next_state; if (item_list.second[0].dot == item_list.second[0].prod.rhs.size()) { next_state2 = -lhs; // 接受状态 } dfa[next_state][lhs] = next_state2; } } } return dfa; } // 计算SLR(1)分析表 unordered_map<int, unordered_map<char, string>> construct_table(const vector<Production>& prods) { unordered_map<int, unordered_map<char, int>> dfa = construct_lr1(prods); // 计算ACTION和GOTO表 unordered_map<int, unordered_map<char, string>> table; for (auto& state_trans : dfa) { int cur_state = state_trans.first; unordered_map<char, int>& trans = state_trans.second; for (auto& sym_state : trans) { char sym = sym_state.first; int next_state = sym_state.second; if (isupper(sym)) { // 非终结符,填入GOTO表 table[cur_state][sym] = to_string(next_state); } else { // 终结符,填入ACTION表 if (next_state > 0) { // 移进 table[cur_state][sym] = "s" + to_string(next_state); } else if (next_state < 0) { // 接受 table[cur_state][sym] = "acc"; } else { // 错误 table[cur_state][sym] = "err"; } } } } return table; } int main() { // 例子:文法 S->E, E->E+T | T, T->T*F | F, F->(E) | id vector<Production> prods{{'S', "E"}, {'E', "E+T"}, {'E', "T"}, {'T', "T*F"}, {'T', "F"}, {'F', "(E)"}, {'F', "id"}}; // 构造SLR(1)分析表 unordered_map<int, unordered_map<char, string>> table = construct_table(prods); // 输出分析表 cout << "ACTION" << endl; for (auto& row : table) { int state = row.first; cout << state << ": "; for (auto& entry : row.second) { char sym = entry.first; string action = entry.second; cout << sym << "=" << action << " "; } cout << endl; } return 0; } ``` 这段代码实现了一个简单的SLR(1)分析器,并输出了分析表。具体分析过程可参考CSDN博客:[SLR(1)语法分析算法的C++实现](https://blog.csdn.net/clover_hxy/article/details/120316783)。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值