Pyparser语法无法正确解析

时间:2014-05-04 19:50:53

标签: pyparsing

这是我的语法:

from pyparsing import Combine, Forward, Group, Literal, Optional, Word
from pyparsing import alphas, delimitedList, infixNotation, nums, oneOf, opAssoc,     operatorPrecedence, quotedString, removeQuotes

integer = Combine(Optional(oneOf("+ -")) + Word(nums)).setParseAction(lambda t: int(t[0]))
real = Combine(Optional(oneOf("+ -")) + Word(nums) + "." +     Optional(Word(nums))).setParseAction(
lambda t: float(t[0]))
variable = Word(alphas)
qs = quotedString.setParseAction(removeQuotes)
lt_brac = Literal('[').suppress()
rt_brac = Literal(']').suppress()

exp_op = Literal('^')
mult_op = oneOf('* /')
plus_op = oneOf('+ -')
relation = oneOf('== != < >')
regex_compare = Literal('~')

function_call = Forward()
operand = function_call | qs | real | integer | variable

expr = operatorPrecedence(operand,
                      [
                          (":", 2, opAssoc.LEFT),
                          (exp_op, 2, opAssoc.RIGHT),
                          (regex_compare, 2, opAssoc.LEFT),
                          (mult_op, 2, opAssoc.LEFT),
                          (plus_op, 2, opAssoc.LEFT),
                          (relation, 2, opAssoc.LEFT)
                      ])

bool_operand = expr
bool_expr = infixNotation(bool_operand,
                      [
                          ("not", 1, opAssoc.RIGHT),
                          ("and", 2, opAssoc.LEFT),
                          ("or", 2, opAssoc.LEFT),
                          ])

function_param = function_call | expr | variable | integer | real
function_call <<= Group(variable + lt_brac + Group(Optional(delimitedList(function_param))) + rt_brac)

final_expr = Group(function_call | bool_expr | expr  )
final_expr.enablePackrat()


def parse(expression):
    return final_expr.parseString(expression)

上面的语法假设解析算术表达式,关系语句如(&lt;,&gt;,!=,==)操作数可以是算术表达式,bool表达式(或者,而不是)操作数可以算术或关系声明。

语法支持[]形式的函数。参数可以是算术表达式。

在大多数情况下这很好用。但是我有以下问题,当我尝试解析时使用上面的语法

print(parse(""abs[abc:sec - abc:sge] > 1")

我得到以下输出

  

[[[&#39; abs&#39;,[[[&#39; abc&#39;,&#39;:&#39;,&#39; sec&#39;],&#39 ; - &#39;,[&#39; abc&#39;,&#39;:&#39;,&#39; sge&#39;]]]]]]

为什么&#39; &GT; 1&#39;忽略?

1 个答案:

答案 0 :(得分:1)

由于final_expr的定义,它被忽略了:

final_expr = Group(function_call | bool_expr | expr  )

为什么用这种方式定义这个表达式? expr是一个简单的bool_exprfunction_call是一个简单的expr。就这样做:

final_expr = bool_expr

你将解析你给定的表达式:

[[['abs', [[['abc', ':', 'sec'], '-', ['abc', ':', 'sge']]]], '>', 1]]