用python写一个能够提取左公共因子、消除直接左递归、判断是否为ll1文法、能够利用预测分析表来判断句子能否产生的完整可运行代码

时间: 2023-12-30 18:06:52 浏览: 30
下面是一个能够提取左公共因子、消除直接左递归、判断是否为 LL(1) 文法、利用预测分析表来判断句子能否产生的完整 Python 代码: ```python from collections import defaultdict def extract_left_factor(grammar): non_terminals = grammar.keys() for A in non_terminals: productions = grammar[A] first_symbols = set([production[0] for production in productions]) for a in first_symbols: common_prefix = [] for production in productions: if production[0] == a: common_prefix.append(production) if len(common_prefix) > 1: # extract left factor new_non_terminal = A + "'" new_productions = [(common_prefix[0][0], new_non_terminal)] for production in common_prefix: new_productions.append((production[1:],)) grammar[new_non_terminal] = new_productions grammar[A] = [production for production in productions if production not in common_prefix] grammar[A].append((a, new_non_terminal)) return True return False def eliminate_left_recursion(grammar): non_terminals = grammar.keys() for i, A in enumerate(non_terminals): productions = grammar[A] for j, beta in enumerate(productions): if beta[0] == A: # eliminate left recursion new_non_terminal = A + "'" new_productions = [production[1:] + (new_non_terminal,) for production in productions[:j]] new_productions.append((new_non_terminal,)) for production in productions[j+1:]: new_productions.append(production[1:] + (new_non_terminal,)) grammar[A] = [(production[1:], A+"'") for production in new_productions] grammar[new_non_terminal] = [(production[1:], A+"'") for production in productions if production[0] != A] + [((),)] return True return False def is_ll1_grammar(grammar): first_sets = compute_first_sets(grammar) follow_sets = compute_follow_sets(grammar, first_sets) for A in grammar.keys(): productions = grammar[A] for i in range(len(productions)): for j in range(i+1, len(productions)): if first_sets[A][i].intersection(first_sets[A][j]): return False if set(productions[i][1:]).intersection(follow_sets[A]).intersection(set(productions[j][1:])): return False return True def compute_first_sets(grammar): first_sets = defaultdict(lambda: set()) for A in grammar.keys(): productions = grammar[A] for beta in productions: if beta == (): first_sets[A].add(()) elif beta[0] not in grammar.keys(): first_sets[A].add(beta[0]) else: for symbol in beta: if symbol not in grammar.keys(): first_sets[A].add(symbol) break if () not in grammar[symbol]: first_sets[A] = first_sets[A].union(first_sets[symbol]) break else: first_sets[A] = first_sets[A].union(first_sets[symbol].difference({()})) if symbol != beta[-1]: continue else: first_sets[A].add(()) break return first_sets def compute_follow_sets(grammar, first_sets): follow_sets = defaultdict(lambda: set()) start_symbol = list(grammar.keys())[0] follow_sets[start_symbol].add('$') while True: is_updated = False for A in grammar.keys(): productions = grammar[A] for beta in productions: for i, B in enumerate(beta): if B in grammar.keys(): if i == len(beta) - 1: follow_sets[B] = follow_sets[B].union(follow_sets[A]) else: for b in first_sets[beta[i+1]]: if b != (): if b not in follow_sets[B]: follow_sets[B].add(b) is_updated = True if () in first_sets[beta[i+1]] and i == len(beta) - 2: follow_sets[B] = follow_sets[B].union(follow_sets[A]) is_updated = True if not is_updated: break return follow_sets def construct_predictive_table(grammar): first_sets = compute_first_sets(grammar) follow_sets = compute_follow_sets(grammar, first_sets) table = defaultdict(lambda: defaultdict(list)) for A in grammar.keys(): productions = grammar[A] for i, beta in enumerate(productions): for a in first_sets[A][i]: table[A][a].append(i) if () in first_sets[A][i]: for b in follow_sets[A]: table[A][b].append(i) return table def parse_sentence(sentence, table): stack = ['$'] sentence = sentence.split() sentence.append('$') current_symbol = stack[-1] i = 0 while True: if current_symbol == sentence[i]: stack.pop() i += 1 else: if current_symbol not in table.keys(): return False if sentence[i] not in table[current_symbol].keys(): return False production = table[current_symbol][sentence[i]][0] stack.pop() if grammar[current_symbol][production] != (): stack += list(reversed(grammar[current_symbol][production])) if stack == ['$'] and sentence[i] == '$': return True elif stack == [] or i >= len(sentence): return False else: current_symbol = stack[-1] # example grammar grammar = { 'S': [('A', 'B'), ('B', 'a')], 'A': [('A', 'c'), ()], 'B': [('B', 'b'), ()] } # extract left factor while extract_left_factor(grammar): pass # eliminate left recursion while eliminate_left_recursion(grammar): pass # check if it is LL(1) grammar if is_ll1_grammar(grammar): table = construct_predictive_table(grammar) print(table) # parse sentence sentence = 'a b $' is_accepted = parse_sentence(sentence, table) print(is_accepted) else: print("Not an LL(1) grammar") ``` 在上面的代码中,我们首先定义了四个函数: - `extract_left_factor`:用于提取左公共因子; - `eliminate_left_recursion`:用于消除直接左递归; - `is_ll1_grammar`:用于判断是否为 LL(1) 文法; - `compute_first_sets` 和 `compute_follow_sets`:用于计算 First 集和 Follow 集。 然后,我们使用给定的文法进行左公共因子提取和直接左递归消除,如果得到的文法是 LL(1) 文法,则构造预测分析表,并使用给定的句子进行语法分析,判断该句子是否能够被该文法所接受。

相关推荐

最新推荐

recommend-type

Python入门程序 函数应用(判断素数、递归求n的阶乘、x的n次方、最大最小值、插入排序法)

Python入门程序 函数应用(判断素数、递归求n的阶乘、x的n次方、最大最小值、插入排序法) 1.判断素数 #编写函数,判断一个数是否是素数。 def isprime(n): if n==1: return False for i in range(2, n): if n ...
recommend-type

python 使用递归实现打印一个数字的每一位示例

今天小编就为大家分享一篇python 使用递归实现打印一个数字的每一位示例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

python 使用turtule绘制递归图形(螺旋、二叉树、谢尔宾斯基三角形)

主要介绍了python 使用turtule绘制递归图形(螺旋、二叉树、谢尔宾斯基三角形) ,需要的朋友可以参考下
recommend-type

python 使用递归回溯完美解决八皇后的问题

今天小编就为大家分享一篇python 使用递归回溯完美解决八皇后的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

python使用递归的方式建立二叉树

主要介绍了python使用递归的方式建立二叉树,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz

REALTEK 8188FTV 8188eus 8188etv linux驱动程序稳定版本, 支持AP,STA 以及AP+STA 共存模式。 稳定支持linux4.0以上内核。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

:YOLOv1目标检测算法:实时目标检测的先驱,开启计算机视觉新篇章

![:YOLOv1目标检测算法:实时目标检测的先驱,开启计算机视觉新篇章](https://img-blog.csdnimg.cn/img_convert/69b98e1a619b1bb3c59cf98f4e397cd2.png) # 1. 目标检测算法概述 目标检测算法是一种计算机视觉技术,用于识别和定位图像或视频中的对象。它在各种应用中至关重要,例如自动驾驶、视频监控和医疗诊断。 目标检测算法通常分为两类:两阶段算法和单阶段算法。两阶段算法,如 R-CNN 和 Fast R-CNN,首先生成候选区域,然后对每个区域进行分类和边界框回归。单阶段算法,如 YOLO 和 SSD,一次性执行检
recommend-type

设计算法实现将单链表中数据逆置后输出。用C语言代码

如下所示: ```c #include <stdio.h> #include <stdlib.h> // 定义单链表节点结构体 struct node { int data; struct node *next; }; // 定义单链表逆置函数 struct node* reverse(struct node *head) { struct node *prev = NULL; struct node *curr = head; struct node *next; while (curr != NULL) { next
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。