Example #1
0
 def parse_tree(self):
     p_tree = ParseTree()
     p_tree.add_rule(('L', self.prod))
     L = ['L_%s' % c for c in self.l]
     if self.prod == 'l33t':
         p_tree.add_rule(('l33t', zip(L, self.r)))
     return p_tree
Example #2
0
 def parse_tree(self):
     p_tree = ParseTree()
     if isinstance(self.prod, basestring):
         return self.prod
     elif isinstance(self.prod, list):
         for p in self.prod:
             p_tree.add_rule((p.sym, p.parse_tree()))
     else:
         return self.prod.parse_tree()
     return p_tree
Example #3
0
 def parse_tree(self):
     p_tree = ParseTree()
     if isinstance(self.prod, str):  # 单纯字符串则返回字符串,这里其实可以看成只有一个根节点的语法树
         return self.prod
     elif isinstance(self.prod, list):  # 是一个list,则将list中的每一个读出,添加后生成成一棵语法树
         for p in self.prod:
             p_tree.add_rule((p.sym, p.parse_tree()))
     else:  # 这是一个语法树,不用做任何处理了,直接看语法树内的节点是否还能生成语法树
         return self.prod.parse_tree()
     return p_tree
Example #4
0
 def parse_tree(self):
     p_tree = ParseTree()
     if isinstance(self.prod, basestring):
         return self.prod
     elif isinstance(self.prod, list):
         for p in self.prod:
             p_tree.add_rule((p.sym,p.parse_tree()))
     else:
         return self.prod.parse_tree()
     return p_tree
Example #5
0
 def parse_tree(self):
     p_tree = ParseTree()
     p_tree.add_rule(('L', self.prod))
     L = ['L_%s' % c for c in self.l]
     if self.prod == 'l33t':
         p_tree.add_rule(('l33t', zip(L, self.r)))
     return p_tree
Example #6
0
    def defaultPasswordParse(self, word):
        # 将所有的密码格式设置成G -> W1,G | D1,G | Y1,G | W1 | D1 | Y1的形式
        pt = ParseTree()
        n = len(word)
        for i, c in enumerate(word):
            r = whatchar(c) + '1'
            # if i<n-1:
            #     r = r + ',G'
            pt.add_rule(('G', r))
            pt.add_rule((r[:2], c.lower()))
            if r.startswith('W'):
                nont_l = NonT_L(c, c)
                pt.extend_rules(nont_l.parse_tree())

        return pt
Example #7
0
    def default_parse_tree(self, word):
        """
        Returns the default parse of a word. Default parse is
        G -> W1,G | D1,G | Y1,G | W1 | D1 | Y1
        This parses any string over the allowed alphabet
        returns a l-o-r traversed parse tree
        """
        pt = ParseTree()
        n = len(word)
        for i,c in enumerate(word):
            r = whatchar(c) + '1'
            if i<n-1:
                r = r + ',G'
            pt.add_rule(('G', r))
            pt.add_rule((r[:2], c.lower()))
            if r.startswith('W'):
                nont_l = NonT_L(c, c)
                pt.extend_rules(nont_l.parse_tree())

        return pt
Example #8
0
    def default_parse_tree(self, word):
        """
        Returns the default parse of a word. Default parse is
        G -> W1,G | D1,G | Y1,G | W1 | D1 | Y1
        This parses any string over the allowed alphabet
        returns a l-o-r traversed parse tree
        """
        pt = ParseTree()
        n = len(word)
        for i,c in enumerate(word):
            r = whatchar(c) + '1'
            if i<n-1:
                r = r + ',G'
            pt.add_rule(('G', r))
            pt.add_rule((r[:2], c.lower()))
            if r.startswith('W'):
                nont_l = NonT_L(c, c)
                pt.extend_rules(nont_l.parse_tree())

        return pt
Example #9
0
 def parse_tree(self):
     if isinstance(self.prod, basestring):
         return ParseTree(self.sym, self.prod)
     else:
         return self.prod.parse_tree()
Example #10
0
 def parse_tree(self):
     pt = ParseTree()
     pt.add_rule((self.sym, self.prod))
     pt.extend_rules(self.L.parse_tree())
     return pt
Example #11
0
 def l_parse_tree(self, word): # leftmost parse-tree
     pt = ParseTree()
     p = self.parse(word)
     if not p:
         print "Failing at ", word.encode('utf-8')
         return pt
     pt.add_rule(('G', p[0]))
     for l, each_r in zip(p[0].split(','), p[1]):
         if isinstance(each_r, basestring):
             pt.add_rule((l, each_r))
         elif l.startswith('W'):
             pt.add_rule((l, each_r[0]))
             L_parse_tree = each_r[1].parse_tree()
             pt.add_rule(L_parse_tree[0])
             if len(L_parse_tree.tree)>1:
                 pt.tree.extend(L_parse_tree[1][1])
         elif l == 'T':
             p = each_r[1]
             rule_name = ','.join([r[0].replace('T_','')
                                  for r in p])
             pt.add_rule((l, rule_name))
             pt.extend_rule(p)
         else:
             print "Something is severly wrong"
     return pt
Example #12
0
    def l_parse_tree(self, word): # leftmost parse-tree
        pt = ParseTree()
        p = self.parse(word)
        if not p:
            print "Failing at ", word.encode('utf-8')
            return pt
        #assert p[0] in self.G['G'], "Wrong rule: {} --> {}".format('G', p[0])
        if p[0] not in self.G['G']:
            return self.default_parse_tree(word)

        pt.add_rule(('G', p[0]))
        for l, each_r in zip(p[0].split(','), p[1]):
            if isinstance(each_r, basestring):
                pt.add_rule((l, each_r))
            elif l.startswith('W'):
                pt.add_rule((l, each_r[0]))
                L_parse_tree = each_r[1].parse_tree()
                pt.add_rule(L_parse_tree[0])
                if len(L_parse_tree.tree)>1:
                    pt.tree.extend(L_parse_tree[1][1])
            elif l == 'T':
                p = each_r[1]
                rule_name = ','.join([r[0].replace('T_','')
                                     for r in p])
                pt.add_rule((l, rule_name))
                pt.extend_rules(p)
            else:
                print "Something is severly wrong"
        return pt
Example #13
0
 def parse_tree(self):
     pt = ParseTree()
     pt.add_rule((self.sym, self.prod))
     pt.extend_rules(self.L.parse_tree())
     return pt
Example #14
0
    def lParseTree(self, passwd):

        pt = ParseTree()

        rule = self.parse(passwd)
        print("our rule is ")
        print(rule)

        # 如果返回值为空的话,则说明翻译失败。记录此时密码
        if not rule:
            print("Failed encode %s" % passwd)
            return pt

        # 假如是无G状态,就是说简单的密码时,就使用简单的密码加密
        if rule[0] not in self.G['G']:
            return self.defaultPasswordParse(passwd)

        # 否则的话,首先设定第一层的规则
        pt.add_rule(('G', rule[0]))

        # 然后,将每一层规则和每一个内容读出来,安插到parsetree中

        for sym, rhs in zip(rule[0].split(','), rule[1]):

            # 首先确认一下,假如规则不是W或者T的话,rhs此时应该只是字符串
            if isinstance(rhs, str):
                # 然后可以直接把这个规则放入
                pt.add_rule((sym, rhs))

            # 假如这个规则是W的话,那么后面跟着的就是(similarkeys_list,NonT_L)则此时要记得先把最相似对象内容放入存档中,并且记录下此时的内容大小写状态
            elif sym.startswith('W'):

                pt.add_rule((sym, rhs[0]))
                # 这里使用parse_tree变量,把此时的单词的状态子叶记录
                ltree = rhs[1].parse_tree()
                # 然后,此时先把最初的规则放进去
                pt.add_rule(ltree[0])
                # 假如此时为’133t'规则的话,此时在'133t'之后会记录下此时可能发生替换的元素,则要把这些元素也放入(这些元素已经打包好了)
                if len(ltree) > 1:
                    pt.tree.extend(ltree[1][1])
            # 假如规则是T的话,那么肯定是('T',[('T_Y','1993')..]..)之类的
            elif sym.startswith('T'):
                # 为了与cfg文件内部保持一致,我们此时需要把文件转换成与cfg内的文件一致的格式
                temp_sym = ''
                for each_label in rhs[1]:
                    temp_sym += each_label[0].replace("T_", "")
                pt.add_rule((sym, temp_sym))

                # 然后把其他的节点也放进去
                pt.extend_rules(rhs[1])

            else:
                print("we can't figure out this word")

        # 完成
        return pt