具体来说,我想编写一个通用的parseaction函数来验证数字的范围.我想将min&max传递给函数.这可能吗 ?
我正在构建一个语法分析器,以对使用点表示法标识的对象执行简单的操作,如下所示:
DISABLE ALL;
ENABLE A.1 B.1.1 C
Run Code Online (Sandbox Code Playgroud)
但在DISABLE ALL关键字ALL中却匹配为3,Regex(r'[a-zA-Z]') => 'A', 'L', 'L'我用来匹配参数。
如何使用正则表达式制作单词?我无法A.1.1使用Word的AFAIK
请参见下面的示例
import pyparsing as pp
def toggle_item_action(s, loc, tokens):
'enable / disable a sequence of items'
action = True if tokens[0].lower() == "enable" else False
for token in tokens[1:]:
print "it[%s].active = %s" % (token, action)
def toggle_all_items_action(s, loc, tokens):
'enable / disable ALL items'
action = True if tokens[0].lower() == "enable" else False
print "it.enable_all(%s)" …Run Code Online (Sandbox Code Playgroud) I'd like to be able to parse two (or any number) of expressions, each with their own set of variable definitions or other context.
There doesn't seem to be an obvious way to associate a context with a particular invocation of pyparsing.ParseExpression.parseString(). The most natural way seems to be to use an instancemethod of some class as the parse actions. The problem with this approach is that the grammar must be redefined for each parse context (for instance, in …
我有类似于YAML的数据,需要使用Pyparsing为它创建语法.与Python一样,Yaml的数据范围由空白定义
数据:
object : object_name
comment : this object is created first
methods:
method_name:
input:
arg1: arg_type
arg2: arg2_type
output:
methond2_name:
input:
output:
arg1 : arg_type
Run Code Online (Sandbox Code Playgroud)
解析上面的内容之后,它应该输出类似于这样的东西:
{'comment': 'this object is created first',
'object': 'object_name',
'methods': {'method_name': {'input': {'arg1': 'arg_type', 'arg2': 'arg2_type'},
'output': None}, 'methond2_name': {'input': None, 'output': {'arg1': 'arg_type'}}}}
Run Code Online (Sandbox Code Playgroud)
[编辑]数据类似于YAML但不完全相同.所以YAML Python解析器无法解析它.我留下了一些细节,以使示例数据更简单
我的 PyParsing 语法有以下测试代码:
from pyparsing import Word, nums, alphas, delimitedList, Group, oneOf
from pprint import pprint
field = Word(alphas)("field")
operator = oneOf("= +")("operator")
string_value = Word(alphas)("string")
int_value = Word(nums).setParseAction(lambda t: int(t[0]))("int")
value = (string_value | int_value )("value")
expression = Group(field + operator + value)("expression")
grammar = Group(delimitedList(expression, delim="&&"))("expr_list")
def test(s):
print "Parsing '{0}'".format(s)
tokenized = grammar.parseString(s)
for f in tokenized:
e = f.expression
pprint(dict(e.items()))
if __name__ == "__main__":
test("foo=1")
test("foo=1 && bar=2")
test("foobar=2 && snakes=4")
Run Code Online (Sandbox Code Playgroud)
输出非常出乎意料 - 似乎我只得到了最后一个表达式 …
我刚开始使用pyparsing这个晚上,我已经构建了一个复杂的语法,它描述了我正在非常有效地工作的一些来源.它非常简单而且非常强大.但是,我在使用时遇到了一些麻烦ParsedResults.我需要能够按照它们被找到的顺序迭代嵌套的标记,并且我发现它有点令人沮丧.我已将问题抽象为一个简单的案例:
import pyparsing as pp
word = pp.Word(pp.alphas + ',.')('word*')
direct_speech = pp.Suppress('“') + pp.Group(pp.OneOrMore(word))('direct_speech*') + pp.Suppress('”')
sentence = pp.Group(pp.OneOrMore(word | direct_speech))('sentence')
test_string = 'Lorem ipsum “dolor sit” amet, consectetur.'
r = sentence.parseString(test_string)
print r.asXML('div')
print ''
for name, item in r.sentence.items():
print name, item
print ''
for item in r.sentence:
print item.getName(), item.asList()
Run Code Online (Sandbox Code Playgroud)
据我所见,这应该有用吗?这是输出:
<div>
<sentence>
<word>Lorem</word>
<word>ipsum</word>
<direct_speech>
<word>dolor</word>
<word>sit</word>
</direct_speech>
<word>amet,</word>
<word>consectetur.</word>
</sentence>
</div>
word ['Lorem', 'ipsum', 'amet,', 'consectetur.']
direct_speech [['dolor', 'sit']]
Traceback …Run Code Online (Sandbox Code Playgroud) 我的PyParsing任务的继续是解析嵌套的三元表达式(例如(x == 1 ? true : (y == 10 ? 100 : 200))).因此,我构造了以下表达式.对我来说,这看起来很直观.但是我没有匹配:
any = Word(printables)
conditional = Forward()
sub_exp = (conditional | any)
conditional = Literal('(') + sub_exp + Literal('?') + sub_exp + Literal(':') + sub_exp + Literal(')')
for exp in conditional.scanString(block_str):
print exp
Run Code Online (Sandbox Code Playgroud)
最初我认为问题是印刷品消耗一切; 我将excludeChars设置为不匹配:?)(,但这也没有帮助.另一种方法是构造嵌套表达式,每个用于" (? "," ?: "和" :) "块.但这种方法非常混乱.有人对解析三元表达式有什么建议吗?
更新 使用下面的答案,但修改为使用scanString:
但是,当使用scanString时,它也会返回许多其他匹配(基本上,与atom匹配的任何内容).
lpar = Literal('(').suppress()
rpar = Literal(')').suppress()
any = Combine(OneOrMore(Word(printables, excludeChars='()?:') | White(' ', max=1)))
expr = Forward()
atom = any …Run Code Online (Sandbox Code Playgroud) 我正在编写一个解析器来解析数学表达式,其中包含变量。我想要所有捕获到的变量的列表。但是我只得到最后捕获的变量。下面是显示问题的最小示例。
>>> from pyparsing import *
>>> var = Word(alphas)
>>> expr = Forward()
>>> expr << var('var') + ZeroOrMore(Literal('+') + expr)
>>> foo = expr.parseString("x + y + z")
>>> foo
(['x', '+', 'y', '+', 'z'], {'var': [('x', 0), ('y', 2), ('z', 4)]})
>>> foo['var']
'z'
Run Code Online (Sandbox Code Playgroud)
我期待着['x','y','z']。我正在使用pyparsing 2.1版。
我无法将这个 EBNF 表达式翻译成 Pyparsing,有什么想法吗?
token:: [A-Z]
P:: !|token;P|(P^P)|(P*P)
Run Code Online (Sandbox Code Playgroud)
问题是当使用递归时,解释器会失败。像这样的表达式应该是有效的:
(ASD;!^FFF;!)
A;B;C;!
(((A;!^B;!)^C;D;!)*E;!)
Run Code Online (Sandbox Code Playgroud) 我正在研究一种非常简单的“查询语法”,供具有适当技术技能的人员使用(即,本身不是编码人员,但可以触及该主题)
他们将在表格上输入的典型示例是:
address like street
AND
vote = True
AND
(
(
age>=25
AND
gender = M
)
OR
(
age between [20,30]
AND
gender = F
)
OR
(
age >= 70
AND
eyes != blue
)
)
Run Code Online (Sandbox Code Playgroud)
用
我正在使用pyparsing(好吧,无论如何尝试)并达到以下目标:
from pyparsing import *
OPERATORS = [
'<',
'<=',
'>',
'>=',
'=',
'!=',
'like'
'regexp',
'between'
]
unicode_printables = u''.join(unichr(c) for c in xrange(65536)
if not unichr(c).isspace())
# user_input is the text sent by …Run Code Online (Sandbox Code Playgroud)