文法四元组的英文全称

    一个文法G是一个四元组 (V,T,P,S)
其中:

(1)V是变元的有限集, 英文全称是:variable。
(2)T:是 终结符的有限集 ,英文全称是:terminal symbol。
(3)P:是 产生式的有限集,其中每个产生式都是α→β的形式,其中α∈(V∪T) + ,且其中至少有一个V中的符号,β∈(V∪T)*。 英文全称是:production。
(4) S∈V,称为文法G的 开始符号。(S很重要,决定这组规则最终要定义什么), 英文全称是:starting symbol。
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是输入文法四元组,判断是否为LL(1)文法的代码实现: ```python # 输入文法四元组 grammer = { 'S': ['aA', 'bB'], 'A': ['cA', 'd'], 'B': ['eB', 'f'] } terminals = ['a', 'b', 'c', 'd', 'e', 'f'] non_terminals = ['S', 'A', 'B'] # 构建FIRST集合 first = {} for symbol in terminals + non_terminals: first[symbol] = set() for terminal in terminals: first[terminal].add(terminal) for non_terminal in non_terminals: first[non_terminal] = set() for non_terminal in non_terminals: for production in grammer[non_terminal]: if production[0] in terminals: first[non_terminal].add(production[0]) else: first[non_terminal] |= first[production[0]] # 构建FOLLOW集合 follow = {} for non_terminal in non_terminals: follow[non_terminal] = set() follow['S'].add('$') while True: updated = False for non_terminal in non_terminals: for production in grammer[non_terminal]: for i in range(len(production)): if production[i] in non_terminals: if i == len(production) - 1: if follow[non_terminal] != follow[production[i]]: follow[production[i]] |= follow[non_terminal] updated = True else: first_set = first[production[i+1]].copy() if '' in first_set: first_set.remove('') if follow[non_terminal] != follow[production[i]] | first_set: follow[production[i]] |= follow[non_terminal] | first_set updated = True else: if follow[non_terminal] != first_set: follow[production[i]] |= first_set updated = True if not updated: break # 构建LL(1)预测分析表 table = {} for non_terminal in non_terminals: table[non_terminal] = {} for terminal in terminals: table[non_terminal][terminal] = [] for non_terminal in non_terminals: for production in grammer[non_terminal]: first_set = set() for i in range(len(production)): if production[i] in terminals: first_set = {production[i]} break else: first_set |= first[production[i]] if '' not in first[production[i]]: break for terminal in first_set: table[non_terminal][terminal].append(production) if '' in first_set: for terminal in follow[non_terminal]: table[non_terminal][terminal].append(production) # 判断文法是否为LL(1)文法 is_ll1_grammer = True for non_terminal in non_terminals: for terminal in terminals: if len(table[non_terminal][terminal]) > 1: is_ll1_grammer = False print(f"({non_terminal}, {terminal}): {table[non_terminal][terminal]}") print("该文法是LL(1)文法" if is_ll1_grammer else "该文法不是LL(1)文法") ``` 上述代码构建了文法的FIRST集合、FOLLOW集合和LL(1)预测分析表,并通过预测分析表判断该文法是否为LL(1)文法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值