Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未能生成S=>;Ba来自给定的python代码_Python_Python 3.x_Grammar - Fatal编程技术网

未能生成S=>;Ba来自给定的python代码

未能生成S=>;Ba来自给定的python代码,python,python-3.x,grammar,Python,Python 3.x,Grammar,给定S=>BaB、B=>B和null(B),代码应该生成S=>BaB | aB | Ba | a和B=>B,但它没有生成Ba,即输出是S=>BaB | aB | a和B=/code>,这是不正确的 < P> >如果终端不在中间,则代码可以正确生成值,例如“代码> S>= BBA < /代码>, B= > B给出 S= > BBA B > 唯一不起作用的情况是终端处于重复空生产的中间 null(B) S=>aBa,B=>B发出S=>aBa | aa和B=>B S=>aaB,B=>B发出S=>aa

给定
S=>BaB
B=>B
null(B)
,代码应该生成
S=>BaB | aB | Ba | a
B=>B
,但它没有生成
Ba
,即输出是
S=>BaB | aB | a
B=/code>,这是不正确的

< P> >如果终端不在中间,则代码可以正确生成值,例如“代码> S>= BBA < /代码>,<代码> B= > B<代码>给出<代码> S= > BBA 和<代码> B= > B<代码> > 唯一不起作用的情况是终端处于重复空生产的中间

null(B)

  • S=>aBa
    B=>B
    发出
    S=>aBa | aa
    B=>B

  • S=>aaB
    B=>B
    发出
    S=>aaB | aa和B=>B

  • 我希望语法
    S=>BaB
    B=>B
    的输出是

    Null productions: ['B']
    
    Productions:[['S', '=', 'B', 'a', 'B'], ['B', '=', 'b']]
    
    Resultant Productions
    ['S', '=', 'B', 'a', 'B']
    ['B', '=', 'b']
    ['S', '=', 'a', 'B']
    ['S', '=', 'B', 'a']
    ['S', '=', 'a']
    
    但结果是

    Null productions: ['B']
    
    Productions:[['S', '=', 'B', 'a', 'B'], ['B', '=', 'b']]
    
    Resultant Productions
    ['S', '=', 'B', 'a', 'B']
    ['B', '=', 'b']
    ['S', '=', 'a', 'B']
    ['S', '=', 'a']
    

    这里的问题是
    temp.remove(null)
    只会删除
    temp
    null
    的第一个实例。因此,最终添加的是'S=>aB',而不是'S=>Ba'。您需要遍历右侧的所有符号,并替换空值的每个实例

    然而,天真地这样做会导致重复的结果(例如,'S=>aB'和'S=>Ba'都会给出'S=>a')。通过使用集合跟踪已经生成的产品的元组,可以避免此问题(必须使用元组而不是列表,因为集合中的项必须是不可变的)。然后,您可以对照此集合进行检查,以确保未追加已添加的产品

    下面是一些为“s=>BaB”和“s=>BBa”生成预期输出的工作代码。我还将
    null\u productions
    更改为函数的参数,以便更容易更改其值

    grammar = ["S = B a B", "B = b"]
    def remove_null_productions(grammar, null_productions=None):
        if null_productions is None:
            null_productions = ["B"]
        print("Null productions: {0}".format(null_productions))
        new_productions = []
    
        seen = set()
        for rule in grammar:
            if('$' in rule):
                continue
            else:
                new_productions.append(rule.split(" "))
    
        print("\nProductions:{0}".format(new_productions))
        for null in null_productions:
    
            for param in new_productions:
                for i, word in enumerate(param):
                    if i < 2:   # don't degenerate LHS
                        continue
                    if word == null:
                        temp = param[:i] + param[i+1:]
                        temp_tup = tuple(temp)
                        if len(temp) > 2 and temp_tup not in seen:
                            new_productions.append(temp)
                            seen.add(temp_tup)
    
        print("\nResultant Productions")
        for rule in new_productions:
            print(rule)
        return new_productions
    
    
    remove_null_productions(grammar)
    
    grammar2 = ["S = B B a", "B = b"]
    
    remove_null_productions(grammar2)
    
    语法2的输出(即“S=>BBa”):


    我对你正在做的语法生成感到困惑。我不知道你能否就预期的产出作出一点澄清?给定句子
    S=BaB
    什么意思?
    null(B)
    ?S=>Bab是一种语法,其中S和B是非终结符,“a”和“B”是终结符。null(B)表示B为null,应将B从RHS上的规则中删除。如果我们删除了a B,原始产品应该保持原样,在B被删除到同一产品后,你必须附加新的值。好的,现在我明白了。您之前使用了B=B而不是B=>B,因此被混淆了。我将行
    if len(temp)>0和temp_tup not in seen
    修改为
    if len(temp)>2和temp_tup not in seen
    ,这样就不会附加空规则,例如S=>XYZ nulls(X,Y,Z)应该给出S=>XYZ | X | Y | Z>0和temp_tup not in seen
    S=>XYZ nulls(X,Y,Z)给出S=>XYZ | XZ | XY | YZ | X | Y | Z |{empty| rule}我已经更新了答案,不生成空规则(与您所做的更改完全相同)。
    grammar = ["S = B a B", "B = b"]
    def remove_null_productions(grammar, null_productions=None):
        if null_productions is None:
            null_productions = ["B"]
        print("Null productions: {0}".format(null_productions))
        new_productions = []
    
        seen = set()
        for rule in grammar:
            if('$' in rule):
                continue
            else:
                new_productions.append(rule.split(" "))
    
        print("\nProductions:{0}".format(new_productions))
        for null in null_productions:
    
            for param in new_productions:
                for i, word in enumerate(param):
                    if i < 2:   # don't degenerate LHS
                        continue
                    if word == null:
                        temp = param[:i] + param[i+1:]
                        temp_tup = tuple(temp)
                        if len(temp) > 2 and temp_tup not in seen:
                            new_productions.append(temp)
                            seen.add(temp_tup)
    
        print("\nResultant Productions")
        for rule in new_productions:
            print(rule)
        return new_productions
    
    
    remove_null_productions(grammar)
    
    grammar2 = ["S = B B a", "B = b"]
    
    remove_null_productions(grammar2)
    
    Null productions: ['B']
    
    Productions:[['S', '=', 'B', 'a', 'B'], ['B', '=', 'b']]
    
    Resultant Productions
    ['S', '=', 'B', 'a', 'B']
    ['B', '=', 'b']
    ['S', '=', 'a', 'B']
    ['S', '=', 'B', 'a']
    ['S', '=', 'a']
    
    Null productions: ['B']
    
    Productions:[['S', '=', 'B', 'B', 'a'], ['B', '=', 'b']]
    
    Resultant Productions
    ['S', '=', 'B', 'B', 'a']
    ['B', '=', 'b']
    ['S', '=', 'B', 'a']
    ['S', '=', 'a']