Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.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
Python pyparsing解析由布尔值组成的字符串_Python_Pyparsing - Fatal编程技术网

Python pyparsing解析由布尔值组成的字符串

Python pyparsing解析由布尔值组成的字符串,python,pyparsing,Python,Pyparsing,我想使用非常好的pyparsing包来解析以下类型的字符串 atomname*和atomindex 1,2,3 原子名xxx,yyy或原子类型rrr,sss 硫醇 不是atomindex 1,2,3 非(atomindex 4,5,6)或atomname* 基于此解析,我将把匹配项链接到将执行的特定函数调用 原子的选择 所有选择关键字(atomname、AtomMindex、thiol…)都存储在列表中(即selkwds) 我试过了,但失败了: keyword = oneOf(selkwds,c

我想使用非常好的pyparsing包来解析以下类型的字符串

atomname*和atomindex 1,2,3

原子名xxx,yyy或原子类型rrr,sss

硫醇

不是atomindex 1,2,3

非(atomindex 4,5,6)或atomname*

基于此解析,我将把匹配项链接到将执行的特定函数调用 原子的选择

所有选择关键字(atomname、AtomMindex、thiol…)都存储在列表中(即
selkwds

我试过了,但失败了:

keyword = oneOf(selkwds,caseless=True).setParseAction(self.__parse_keyword)

func_call = Forward()

func_call << (keyword + commaSeparatedList).setParseAction(self.__parse_expression)

func_call = operatorPrecedence(func_call, [(NOT, 1, opAssoc.RIGHT, self.__not),
                                           (AND, 2, opAssoc.LEFT , self.__and),
                                           (OR , 2, opAssoc.LEFT , self.__or)])
keyword=oneOf(selkwds,caseless=True)。setParseAction(self.\uu parse\u关键字)
func_call=Forward()

func_call请参见此修改版解析器中的嵌入注释:

from pyparsing import *

selkwds = "atomname atomindex atomtype thiol".split()
func_name = MatchFirst(map(CaselessKeyword, selkwds))
NOT,AND,OR = map(CaselessKeyword,"NOT AND OR".split())
keyword = func_name | NOT | AND | OR

func_call = Forward()

integer = Word(nums).setParseAction(lambda t: int(t[0]))
alphaword = Word(alphas,alphanums)

# you have to be specific about what kind of things can be an arg,
# otherwise, an argless function call might process the next
# keyword or boolean operator as an argument;
# this kind of lookahead is commonly overlooked by those who
# assume that the parser will try to do some kind of right-to-left
# backtracking in order to implicitly find a token that could be
# mistaken for the current repetition type; pyparsing is purely
# left-to-right, and only does lookahead if you explicitly tell it to
# I assume that a func_call could be a function argument, otherwise
# there is no point in defining it as a Forward
func_arg = ~keyword + (integer | func_call | alphaword)

# add Groups to give structure to your parsed data - otherwise everything
# just runs together - now every function call parses as exactly two elements:
# the keyword and a list of arguments (which may be an empty list, but will
# still be a list)
func_call << Group(func_name + Group(Optional(delimitedList(func_arg) | '*')))

# don't name this func_call, its confusing with what you've 
# already defined above
func_call_expr = operatorPrecedence(func_call, [(NOT, 1, opAssoc.RIGHT),
                                           (AND, 2, opAssoc.LEFT),
                                           (OR , 2, opAssoc.LEFT)])
印刷品:

atomname * and atomindex 1,2,3
[[['atomname', ['*']], 'AND', ['atomindex', [1, 2, 3]]]]

atomname xxx,yyy or atomtype rrr,sss
[[['atomname', ['xxx', 'yyy']], 'OR', ['atomtype', ['rrr', 'sss']]]]

thiol
[['thiol', []]]

not atomindex 1,2,3
[['NOT', ['atomindex', [1, 2, 3]]]]

not (atomindex 4,5,6) or atomname *
[[['NOT', ['atomindex', [4, 5, 6]]], 'OR', ['atomname', ['*']]]]
atomname * and atomindex 1,2,3
[[['atomname', ['*']], 'AND', ['atomindex', [1, 2, 3]]]]

atomname xxx,yyy or atomtype rrr,sss
[[['atomname', ['xxx', 'yyy']], 'OR', ['atomtype', ['rrr', 'sss']]]]

thiol
[['thiol', []]]

not atomindex 1,2,3
[['NOT', ['atomindex', [1, 2, 3]]]]

not (atomindex 4,5,6) or atomname *
[[['NOT', ['atomindex', [4, 5, 6]]], 'OR', ['atomname', ['*']]]]