Pyparsing生成带组的二进制AST树

时间:2012-10-30 09:47:16

标签: python parsing pyparsing

您好我正在为流程演算开发解析器。我已经开发了一个使用setParseAction和自定义AST类但现在我想重写它以使用dicts和list。

from pyparsing import *                                                                                                                                                               

integer = Word(nums).setParseAction(lambda t:int(t[0]))                                                                                                                               
variable = Word(alphas,exact=1)                                                                                                                                                       
operand = Word(alphas.upper(), alphanums+"_")                                                                                                                                         
semicol = Literal(";").suppress()                                                                                                                                                     
equals = Literal("=")  

expr = operatorPrecedence( operand,                                                                                                                                                   
[(".", 2, opAssoc.LEFT),                                                                                                                                                          
 ("+", 2, opAssoc.LEFT),]                                                                                                                                                         
)

rmdef = operand + equals + expr                                                                                                                                                       
expr_def = rmdef + semicol                                                                                                                                                                          

test = ["P = A.(B.C + E.D);",                                                                                                                                                         
"P = A.B.C;"                                                                                                                                                                  
]   

返回:

['P', '=', ['A', '.', [['B', '.', 'C'], '+', ['E', '.', 'D']]]]
['P', '=', ['A', '.', 'B', '.', 'C']]

我的问题是关于第二个例子。我希望它是,我该怎么办

['P', '=', ['A', '.' [ 'B', '.', 'C' ]]]

1 个答案:

答案 0 :(得分:2)

expr中,您已定义了'。'运算符是左关联的,但在您想要的输出中,它是右关联的。变化:

expr = operatorPrecedence(operand,
    [(".", 2, opAssoc.LEFT),
     ("+", 2, opAssoc.LEFT),]
    )

expr = operatorPrecedence(operand,
    [(".", 2, opAssoc.RIGHT),
     ("+", 2, opAssoc.LEFT),]
    )

现在这段代码:

for t in test:
    print expr_def.parseString(t).asList()

打印

['P', '=', ['A', '.', [['B', '.', 'C'], '+', ['E', '.', 'D']]]]
['P', '=', ['A', '.', ['B', '.', 'C']]]