from typing import List, Dict, Tuple
def compute_select_sets(grammar: Dict[str, List[List[str]]], first_sets: Dict[str, set], follow_sets: Dict[str, set]) -> Dict[Tuple[str, int], set]:
"""
Compute select sets for all productions in the given grammar.
:param grammar: A dictionary representing the grammar, where key is a non-terminal symbol and value is a list of right-hand-side productions.
:param first_sets: A dictionary representing the first sets for all symbols (both terminals and non-terminals).
:param follow_sets: A dictionary representing the follow sets for all non-terminal symbols.
:return: A dictionary representing the select sets for all productions, where key is a tuple (non-terminal, index) and value is the select set.
"""
select_sets = {}
for non_terminal, productions in grammar.items():
for i, production in enumerate(productions):
# Compute the first set of the right-hand-side of the production
first_set = set()
for symbol in production:
first_set |= first_sets[symbol]
if 'ε' not in first_sets[symbol]:
break
# Compute the select set of the production
select_set = first_set - {'ε'}
if 'ε' in first_sets[production[0]]:
select_set |= follow_sets[non_terminal]
select_sets[(non_terminal, i)] = select_set
return select_sets
其中,grammar 参数是非终结符到产生式列表的字典,first_sets 参数是符号到 first 集合的字典,follow_sets 参数是非终结符到 follow 集合的字典,返回值是产生式到 select 集合的字典。
消除左递归后要重新计算 FIRST 集合、FOLLOW 集合和 SELECT 集合,以保证文法符合 LL(1) 文法的要求。
User
这里消除间接左递归的转换规则可以讲一下吗
Assistant
消除间接左递归时,需要按照下面的转换规则处理:
假设存在的间接左递归为 S → Aα | B。
创建一个新的非终结符 S',将 S 的产生式改为 S → Aα S' | B S'。
计算新的非终结符 S' 的产生式:
对于原来的非终结符 A 的产生式 A → β1 | β2 | ... | βn,将其中所有以 B 开头的产生式替换成 B S'。
对于 A 的每个产生式 A → γ1 | γ2 | ... | γm,将其中所有以 S 开头的产生式替换成 S → S'。
对于不含 S 开头的产生式 A → β1 | β2 | ... | βn,保持不变。
添加S' → ε到新的文法中。
对于上述规则,在处理过程中需要注意,因为新的非终结符产生式的 LEFT 端的所有前缀都以 S 开头,所以要特别处理以 S 开头的产生式,避免产生第二次间接左递归的情况。例如,如果 A → Sγ,就必须将 S → γ 这个产生式重新处理为 S → γ S' 的样式,并且一定不能再出现 γ → Sβ 的产生式。这样,就保证了处理完之后文法中不再存在间接左递归。
Not yet included in a published release.
def eliminate_left_recursion(grammar: Dict[str, List[List[str]]]) -> Dict[str, List[List[str]]]:
"""
Eliminate left recursion in the given grammar.
:param grammar: A dictionary representing the grammar, where key is a non-terminal symbol and value is a list of right-hand-side productions.
:return: A dictionary representing the modified grammar with left recursion removed.
"""
new_grammar = {}
for non_terminal, productions in grammar.items():
# Divide productions into left-recursive and non-left-recursive
left_recursive_productions = []
non_left_recursive_productions = []
for production in productions:
if production[0] == non_terminal:
left_recursive_productions.append(production)
else:
non_left_recursive_productions.append(production)
# If no left-recursive productions, keep the same
if len(left_recursive_productions) == 0:
new_grammar[non_terminal] = productions
continue
# Create a new non-terminal symbol to replace the left-recursive part
new_non_terminal = non_terminal + "'"
# Add non-left-recursive productions with new suffix
new_productions = []
for production in non_left_recursive_productions:
new_productions.append(production + [new_non_terminal])
# Add new ε production for new suffix
new_productions.append(['ε'])
# Add left-recursive productions with new suffix
for left_production in left_recursive_productions:
new_production = left_production[1:] + [new_non_terminal]
new_productions.append(new_production)
new_grammar[non_terminal] = new_productions
# Add new non-terminal symbol with left-recursive productions
new_productions = []
for left_production in left_recursive_productions:
new_production = left_production[1:] + [new_non_terminal]
new_productions.append(new_production)
new_productions.append(['ε'])
new_grammar[new_non_terminal] = new_productions
return new_grammar