[Python] Python package Lark does not build the grammar correctly

Discussão em 'Python' iniciado por Stack, Fevereiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    I would need to build a Tree that would retrieve something like this using Lark package:

    start
    expr
    or_expr
    and_expr
    comp_expr
    identifier Name
    comparator eq
    value 'Milk'
    comp_expr
    identifier Price
    comparator lt
    value 2.55


    The grammar used is the following

    from lark import Lark

    odata_grammar = """
    start: expr

    expr: or_expr

    or_expr: and_expr ("or" and_expr)*
    and_expr: comp_expr ("and" comp_expr)*
    comp_expr: identifier comparator value -> comp_expr

    comparator: "eq" | "lt" | "gt" | "le" | "ge" | "ne"

    value: STRING | NUMBER
    identifier: CNAME

    STRING: /'(''|[^'])*'/
    DATE: /\d{4}-\d{2}-\d{2}/
    NUMBER: /-?\d+(\.\d+)?/

    %import common.CNAME
    %import common.WS
    %ignore WS
    """

    parser = Lark(odata_grammar, start='start', parser='lalr')
    url_filter = "Name eq 'Milk' and Price lt 2.55"
    tree = parser.parse(url_filter)
    print(tree.pretty())


    When I print this tree, I find that the Tree retrieved is the following:

    start
    expr
    or_expr
    and_expr
    comp_expr
    identifier Name
    comparator
    value 'Milk'
    comp_expr
    identifier Price
    comparator
    value 2.55


    The comparator for some reason is not retrieved. And I say retrieved because the Lark package seems to detect it but it is not printed in the tree. This is curious because when I try to "force" the comparator to doing something like this in the grammar comparator: "eq" -> eq what I get is the comparator named as eq but not comparator: eq.

    Continue reading...

Compartilhe esta Página