当表达式有多种可能的形式时,如何为表达式编写语法

时间:2017-04-05 14:12:36

标签: parsing grammar pyparsing

我有一些句子需要转换为正则表达式代码,我试图使用Pyparsing。这些句子基本上都是搜索规则,告诉我们要搜索什么。

句子的例子 -

  1. LINE_CONTAINS this is a phrase - 这是一个示例搜索规则,告知您搜索的行应该包含短语this is a phrase

  2. LINE_STARTSWITH However we - 这是一个示例搜索规则,告知您要搜索的行应以短语However we

  3. 开头
  4. 规则也可以合并,例如 - LINE_CONTAINS phrase one BEFORE {phrase2 AND phrase3} AND LINE_STARTSWITH However we

  5. 可以找到所有实际句子的列表(如有必要)here 所有行都以上面提到的2个符号中的任何一个开头(称为line_directives)。现在,我试图解析这些句子,然后将它们转换为正则表达式代码。我开始为我的语法写一个BNF,这就是我想出的 -

    lpar ::= '{'
    rpar ::= '}'
    line_directive ::= LINE_CONTAINS | LINE_STARTSWITH
    phrase ::= lpar(?) + (word+) + rpar(?) # meaning if a phrase is parenthesized, its still the same
    
    upto_N_words ::= lpar + 'UPTO' + num + 'WORDS' + rpar
    N_words ::= lpar + num + 'WORDS' + rpar
    upto_N_characters ::= lpar + 'UPTO' + num + 'CHARACTERS' + rpar
    N_characters ::= lpar + num + 'CHARACTERS' + rpar
    
    JOIN_phrase ::= phrase + JOIN + phrase
    AND_phrase ::= phrase (+ JOIN + phrase)+
    OR_phrase ::= phrase (+ OR + phrase)+
    BEFORE_phrase ::= phrase (+ BEFORE + phrase)+
    AFTER_phrase ::= phrase (+ AFTER + phrase)+
    
    braced_OR_phrase ::= lpar + OR_phrase + rpar
    braced_AND_phrase ::= lpar + AND_phrase + rpar
    braced_BEFORE_phrase ::= lpar + BEFORE_phrase + rpar
    braced_AFTER_phrase ::= lpar + AFTER_phrase + rpar
    braced_JOIN_phrase ::= lpar + JOIN_phrase + rpar
    
    rule ::= line_directive + subrule
    final_expr ::= rule (+ AND/OR + rule)+
    

    问题是subrule,基于我的经验数据,我已经能够提出以下所有表达式 -

    subrule ::= phrase
            ::= OR_phrase
            ::= JOIN_phrase
            ::= BEFORE_phrase
            ::= AFTER_phrase
            ::= AND_phrase
            ::= phrase + upto_N_words + phrase
            ::= braced_OR_phrase + phrase
            ::= phrase + braced_OR_phrase
            ::= phrase + braced_OR_phrase + phrase
            ::= phrase + upto_N_words + braced_OR_phrase
            ::= phrase + upto_N_characters + phrase
            ::= braced_OR_phrase + phrase + upto_N_words + phrase
            ::= phrase + braced_OR_phrase + upto_N_words + phrase
    

    举个例子,我的一个句子是LINE_CONTAINS the objective of this study was {to identify OR identifying} genes upregulated。为此,如上所述的子规则是phrase + braced_OR_phrase + phrase

    所以我的问题是我如何为subrule编写一个简单的BNF语法表达式,以便我能够使用Pyparsing轻松地为它编写语法?此外,关于我现有技术的任何意见都是绝对受欢迎的。

    编辑:在应用了@Paul所阐述的原则后,这里是代码的 MCVE 版本。它需要一个要解析的句子列表hrrsents,解析每个句子,将其转换为相应的正则表达式并返回正则表达式字符串列表 -

    from pyparsing import *
    import re
    
    
    def parse_hrr(hrrsents):
        UPTO, AND, OR, WORDS, CHARACTERS = map(Literal, "UPTO AND OR WORDS CHARACTERS".split())
        LBRACE,RBRACE = map(Suppress, "{}")
        integer = pyparsing_common.integer()
    
        LINE_CONTAINS, PARA_STARTSWITH, LINE_ENDSWITH = map(Literal,
            """LINE_CONTAINS PARA_STARTSWITH LINE_ENDSWITH""".split()) # put option for LINE_ENDSWITH. Users may use, I don't presently
        BEFORE, AFTER, JOIN = map(Literal, "BEFORE AFTER JOIN".split())
        keyword = UPTO | WORDS | AND | OR | BEFORE | AFTER | JOIN | LINE_CONTAINS | PARA_STARTSWITH
    
        class Node(object):
            def __init__(self, tokens):
                self.tokens = tokens
    
            def generate(self):
                pass
    
        class LiteralNode(Node):
            def generate(self):
                return "(%s)" %(re.escape(''.join(self.tokens[0]))) # here, merged the elements, so that re.escape does not have to do an escape for the entire list
    
        class ConsecutivePhrases(Node):
            def generate(self):
                join_these=[]
                tokens = self.tokens[0]
                for t in tokens:
                    tg = t.generate()
                    join_these.append(tg)
                seq = []
                for word in join_these[:-1]:
                    if (r"(([\w]+\s*)" in word) or (r"((\w){0," in word): #or if the first part of the regex in word:
                        seq.append(word + "")
                    else:
                        seq.append(word + "\s+")
                seq.append(join_these[-1])
                result = "".join(seq)
                return result
    
        class AndNode(Node):
            def generate(self):
                tokens = self.tokens[0]
                join_these=[]
                for t in tokens[::2]:
                    tg = t.generate()
                    tg_mod = tg[0]+r'?=.*\b'+tg[1:][:-1]+r'\b)' # to place the regex commands at the right place
                    join_these.append(tg_mod)
                joined = ''.join(ele for ele in join_these)
                full = '('+ joined+')'
                return full
    
        class OrNode(Node):
            def generate(self):
                tokens = self.tokens[0]
                joined = '|'.join(t.generate() for t in tokens[::2])
                full = '('+ joined+')'
                return full
    
        class LineTermNode(Node):
            def generate(self):
                tokens = self.tokens[0]
                ret = ''
                dir_phr_map = {
                    'LINE_CONTAINS': lambda a:  r"((?:(?<=^)|(?<=[\W_]))" + a + r"(?=[\W_]|$))456", 
                    'PARA_STARTSWITH':
                        lambda a: ( r"(^" + a + r"(?=[\W_]|$))457") if 'gene' in repr(a)
                        else (r"(^" + a + r"(?=[\W_]|$))458")}
    
                for line_dir, phr_term in zip(tokens[0::2], tokens[1::2]):
                    ret = dir_phr_map[line_dir](phr_term.generate())
                return ret
    
        class LineAndNode(Node):
            def generate(self):
                tokens = self.tokens[0]
                return '&&&'.join(t.generate() for t in tokens[::2])
    
        class LineOrNode(Node):
            def generate(self):
                tokens = self.tokens[0]
                return '@@@'.join(t.generate() for t in tokens[::2])
    
        class UpToWordsNode(Node):
            def generate(self):
                tokens = self.tokens[0]
                ret = ''
                word_re = r"([\w]+\s*)"
                for op, operand in zip(tokens[1::2], tokens[2::2]):
                    # op contains the parsed "upto" expression
                    ret += "(%s{0,%d})" % (word_re, op)
                return ret
    
        class UpToCharactersNode(Node):
            def generate(self):
                tokens = self.tokens[0]
                ret = ''
                char_re = r"\w"
                for op, operand in zip(tokens[1::2], tokens[2::2]):
                    # op contains the parsed "upto" expression
                    ret += "((%s){0,%d})" % (char_re, op)
                return ret
    
        class BeforeAfterJoinNode(Node):
            def generate(self):
                tokens = self.tokens[0]
                operator_opn_map = {'BEFORE': lambda a,b: a + '.*?' + b, 'AFTER': lambda a,b: b + '.*?' + a, 'JOIN': lambda a,b: a + '[- ]?' + b}
                ret = tokens[0].generate()
                for operator, operand in zip(tokens[1::2], tokens[2::2]):
                    ret = operator_opn_map[operator](ret, operand.generate()) # this is basically calling a dict element, and every such element requires 2 variables (a&b), so providing them as ret and op.generate
                return ret
    
    ## THE GRAMMAR
        word = ~keyword + Word(alphas, alphanums+'-_+/()')
        uptowords_expr = Group(LBRACE + UPTO + integer("numberofwords") + WORDS + RBRACE).setParseAction(UpToWordsNode)
        uptochars_expr = Group(LBRACE + UPTO + integer("numberofchars") + CHARACTERS + RBRACE).setParseAction(UpToCharactersNode)
        some_words = OneOrMore(word).setParseAction(' '.join, LiteralNode)
        phrase_item = some_words | uptowords_expr | uptochars_expr
    
        phrase_expr = infixNotation(phrase_item,
                                    [
                                    ((BEFORE | AFTER | JOIN), 2, opAssoc.LEFT, BeforeAfterJoinNode), # was not working earlier, because BEFORE etc. were not keywords, and hence parsed as words
                                    (None, 2, opAssoc.LEFT, ConsecutivePhrases),
                                    (AND, 2, opAssoc.LEFT, AndNode),
                                    (OR, 2, opAssoc.LEFT, OrNode),
                                    ],
                                    lpar=Suppress('{'), rpar=Suppress('}')
                                    ) # structure of a single phrase with its operators
    
        line_term = Group((LINE_CONTAINS|PARA_STARTSWITH)("line_directive") +
                          (phrase_expr)("phrases")) # basically giving structure to a single sub-rule having line-term and phrase
        #
        line_contents_expr = infixNotation(line_term.setParseAction(LineTermNode),
                                           [(AND, 2, opAssoc.LEFT, LineAndNode),
                                            (OR, 2, opAssoc.LEFT, LineOrNode),
                                            ]
                                           ) # grammar for the entire rule/sentence
    ######################################
        mrrlist=[]
        for t in hrrsents:
            t = t.strip()
            if not t:
                continue
            try:
                parsed = line_contents_expr.parseString(t)
            except ParseException as pe:
                print(' '*pe.loc + '^')
                print(pe)
                continue
    
    
            temp_regex = parsed[0].generate()
            final_regexes3 = re.sub(r'gene','%s',temp_regex) # this can be made more precise by putting a condition of [non-word/^/$] around the 'gene'
            mrrlist.append(final_regexes3)
        return(mrrlist)
    

1 个答案:

答案 0 :(得分:2)

这里有一个双层语法,所以你最好一次只关注一个层,我们已经在你的一些其他问题中介绍过了。较低层是phrase_expr的层,后面将作为line_directive_expr的参数。因此,首先定义短语表达式的示例 - 从完整语句示例列表中提取它们。您为phrase_expr完成的BNF将具有最低级别的递归,如下所示:

phrase_atom ::= <one or more types of terminal items, like words of characters 
                 or quoted strings, or *possibly* expressions of numbers of 
                 words or characters>  |  brace + phrase_expr + brace`

(其他一些问题:是否有可能在没有运算符的情况下一个接一个地有多个phrase_items?这表示什么?如何解析?解释?这隐含的操作应该是它自己的优先级吗?)

这足以循环你的短语表达式的递归 - 你的BNF中不需要任何其他braced_xxx元素。 AND,OR和JOIN显然是二元运算符 - 在正常运算优先级中,AND在OR之前进行求值,您可以自己决定JOIN应该在哪里。写一些没有括号的示例短语,使用AND和JOIN,以及OR和JOIN,并考虑在您的域中有哪些评估顺序。

完成后,line_directive_expr应该很简单,因为它只是:

line_directive_item ::= line_directive phrase_expr | brace line_directive_expr brace
line_directive_and ::= line_directive_item (AND line_directive_item)*
line_directive_or ::= line_directive_and (OR line_directive_and)*
line_directive_expr ::= line_directive_or

然后,当您转换为pyparsing时,一次添加一组的组和结果名称!不要立即对所有内容进行分组或命名。通常我建议大量使用结果名称,但在中缀表示法语法中,许多结果名称可能会使结果混乱。让Group(最终是节点类)进行结构化,节点类中的行为将指导您想要结果名称的位置。就此而言,结果类通常会得到如此简单的结构,以至于在类init或evaluate方法中进行列表解包通常更容易。 从简单的表达式开始,然后处理复杂的表达式。(看看"LINE_STARTSWITH gene" - 这是你最简单的测试用例之一,但是你把它作为#97?)如果你只是对它进行排序按长度顺序列出,这将是一个很好的粗略切割。或者通过增加运营商数量来排序。但是在你使用简单的工作之前处理复杂的案例,你会有太多的选择来调整或改进应该去的地方,并且(从个人经验来讲)你很可能会把它弄错,因为它是正确的 - 除非当你弄错了,只是让下一个问题更难解决。

再次,正如我们在其他地方所讨论的那样,第二层中的魔鬼正在对各种线指令项进行实际解释,因为有一个隐含的顺序来评估LINE_STARTSWITH和LINE_CONTAINS,它们会覆盖它们可能被找到的顺序在初始字符串中。因为你是这个特定领域的语言设计师,所以这个球完全在你的法庭上。