4

我对 pyparsing 和 Python 很陌生,所以这是一个警告,我可能做错了什么。

我想做的是构建一个 SQL 解析器并构建带有节点的树,然后我可以遍历这些节点。

我正在尝试从 yacc/bison 语法文件中复制这种东西:

| scalar_exp '^' scalar_exp   
         { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opPOW,  $1, $3);
           joinTmpNodes($$,$1,$3); }

这是我在 Python 中的代码:

LPAR = Suppress('(')
RPAR = Suppress(')')
COMMA = Suppress(',')

AND = CaselessKeyword('AND')
ASC = CaselessKeyword('ASC')
DESC = CaselessKeyword('DESC')
ON = CaselessKeyword('ON')
USING = CaselessKeyword("USING")
INNER = CaselessKeyword("INNER")
JOIN = CaselessKeyword("JOIN")
AS = CaselessKeyword("AS")
NOT = CaselessKeyword("NOT")
SELECT = CaselessKeyword("SELECT")
FROM = CaselessKeyword("FROM")
WHERE = CaselessKeyword("WHERE")
GROUP = CaselessKeyword("GROUP")
BY = CaselessKeyword("BY")
ORDER = CaselessKeyword("ORDER")
LIMIT = CaselessKeyword("LIMIT")
BETWEEN = CaselessKeyword("BETWEEN")

UNARY = 1
BINARY = 2
TERNARY = 3

keyword = MatchFirst(( ASC, DESC, ON, USING, INNER,
 JOIN, AS, NOT, SELECT, FROM, WHERE, GROUP, BY,
 ORDER, BY, LIMIT,BETWEEN))

identifier = ~keyword + Word(alphas, alphanums+"_")
collation_name = identifier.copy()
column_name = Suppress('[') + ~keyword + Word(alphas, alphanums+"_") + Suppress(']')
column_alias = identifier.copy()
table_name = identifier.copy()
table_alias = identifier.copy()
index_name = identifier.copy()
function_name = identifier.copy()
parameter_name = identifier.copy()

expr = Forward().setName("expression")
select_stmt = Forward().setName("select statement")

integer = Regex(r"[+-]?\d+")
numeric_literal = Regex(r"\d+(\.\d*)?([eE][+-]?\d+)?")
string_literal = QuotedString("'")
literal_value = ( numeric_literal | string_literal)

expr_term = (
    function_name + LPAR + Optional(delimitedList(expr)) + RPAR |
    literal_value |
    identifier |
    column_name
    )

expr << operatorPrecedence(expr_term,
    [
    (oneOf('- + ~') | NOT, UNARY, opAssoc.LEFT, setObject),
    ('||', BINARY, opAssoc.LEFT),
    (oneOf('* / %'), BINARY, opAssoc.LEFT,setObject),
    (oneOf('+ -'), BINARY, opAssoc.LEFT),
    (oneOf('<< >> & |'), BINARY, opAssoc.LEFT),
    (oneOf('< <= > >='), BINARY, opAssoc.LEFT),
    (oneOf('= == != <>') , BINARY, opAssoc.LEFT),
    ('||', BINARY, opAssoc.LEFT),
    ((BETWEEN,AND), TERNARY, opAssoc.LEFT),
    ])

ordering_term = expr + Optional(ASC | DESC)

join_constraint = ON + expr('join_expression')

join_op = COMMA | (INNER + JOIN)

join_source = Forward()
single_source = ( table_name("table") +
                    Optional(Optional(AS) + table_alias("table_alias")))

join_source << single_source + Group(ZeroOrMore(join_op + single_source + Optional(join_constraint)))("join")

result_column = "*" | table_name + "." + "*" | (expr + Optional(Optional(AS) + column_alias))
select_core = (SELECT + Group(delimitedList(result_column))("columns") +
                Optional(FROM + join_source).setParseAction(setObject) +
                Optional(WHERE + expr("where_expr")) +
                Optional(GROUP + BY + Group(delimitedList(ordering_term)("group_by_terms")))
                )

select_stmt << (select_core + ZeroOrMore(select_core) +
                Optional(ORDER + BY + Group(delimitedList(ordering_term))("order_by_terms"))
                )

注意:这是 Paul McGuire 的 select_parser.py 的精简版

我想我必须使用 setParseAction 但每当我这样做时,我总是在我调用的方法中得到 None 。我得到了完整的字符串和位置,但没有令牌。

调用 setParseAction 来复制 yacc/bison 逻辑的最佳位置在哪里?

4

1 回答 1

2

您在 operatorPrecedence 操作列表中的第一个条目应该是右关联的,而不是左关联的。进行更改(并启用 Packrat 解析)后,此解析器开始为我工作。

但至于构建语法树,我会让 pyparsing 为您构建树。将解析操作附加到返回您将在 setObject 中创建的类的实例的不同语法元素。像:

class ExpressionNode(object):
    def __init__(self, tokens):
        self.tokens = tokens

    def __repr__(self):
        return "%s:\n%s" % (self.__class__.__name__, self.tokens.dump(indent='  '))

    def __getattr__(self, attr):
        return getattr(self.tokens, attr)

class SelectNode(ExpressionNode): pass

select_stmt.setParseAction(SelectNode)
stmtobj = select_stmt.parseString("SELECT * FROM B")[0]
print stmtobj.columns
于 2011-06-30T13:18:02.883 回答