Lark: How to rule/terminal priority works

Created on 25 Oct 2018  Â·  5Comments  Â·  Source: lark-parser/lark

Hello,

I have the following grammar:

    Lark(r"""
    documento: PALABRA+ monto PALABRA+
    monto: SIGNO ENTERO 
    SIGNO: "$" INT    
    ENTERO.5: INT+    
    PALABRA.1: /[^\s]+/
    """, start='documento', parser="earley", lexer="dynamic", ambiguity="resolve")

The priority does not work in the last grammar, the curious is the priority works in the following code

Lark(r"""
documento:PALABRA+ acciondemandante PALABRA+
    acciondemandante: PALABRASDEMANDA informacionentidad+
     PALABRASDEMANDA: "demandante"i                
  informacionentidad: nombres distancia identificacion
             nombres: NOMBRE+ 
              NOMBRE: NOMBRENORMAL+
        NOMBRENORMAL: (UCASE_LETTER LCASE_LETTER+)
           distancia: CONECTOR~0..3
            CONECTOR: LCASE_LETTER+
    identificacion.5: tipo INT+
                tipo: (LCASE_LETTER | UCASE_LETTER) (LCASE_LETTER | UCASE_LETTER)
           PALABRA.1: /.+/

    """, start='documento', parser="earley", lexer="dynamic", ambiguity="resolve")
question

Most helpful comment

Hi,
The current Earley implementation supports rule priority, but not terminal priority.

LALR supports terminal priority but not rule priority.

There are reasons! But it's also something I hope to correct in the future.

All 5 comments

Hi,
The current Earley implementation supports rule priority, but not terminal priority.

LALR supports terminal priority but not rule priority.

There are reasons! But it's also something I hope to correct in the future.

I already tried with rule priority but still does not work

json_parser3 = Lark(r"""
    documento: palabra+ monto palabra+
    monto.5: "$" INT+      
    palabra.1: /[^\s]+/

    %import common.INT             
    %import common.WS
    %ignore WS
    %ignore "." | "," | ";" | ":" | "-" | "(" | ")" | "\"" | "_" | "#" | "”"
    """, start='documento', parser="earley", lexer="dynamic", ambiguity="resolve")

and I tried

`   json_parser3 = Lark(r"""
    documento: PALABRA+ monto PALABRA+
    monto.5: "$" INT+      
    PALABRA.1: /[^\s]+/

    %import common.INT             
    %import common.WS
    %ignore WS
    %ignore "." | "," | ";" | ":" | "-" | "(" | ")" | "\"" | "_" | "#" | "”"
    """, start='documento', parser="earley", lexer="dynamic", ambiguity="resolve")`

I got this
Tree(monto, [Token(INT, '63')]), Token(PALABRA, '000.000.00')
and I wanna this
Tree(monto, [Token(INT, '63')], Token(INT, '000'), Token(INT, '000'), Token(INT, '00')])])

You're right, it should work and it doesn't.

Meanwhile, try this

x = Lark(r"""
    documento: palabra? monto palabra?
    monto: "$" INT+
    palabra.0: /[^\s]+/

    %import common.INT
    %import common.WS
    %ignore WS
    %ignore "." | "," | ";" | ":" | "-" | "(" | ")" | "\"" | "_" | "#" | "”"
    """, start='documento')     

It does not works for me because the text that I send is:
...bla bla Limítese la medida en la suma de $63.000.000.00 Sírvase proceder de conformidad y consignar los dineros retenidos a Órdenes de este Juzgado para el proceso de referencia bla bla..
As you can see the $63.000.000.00 is among the text, so I need to use "palabra+"

I think plain old regular expressions will solve your problem very easily.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

evandrocoan picture evandrocoan  Â·  6Comments

petee-d picture petee-d  Â·  9Comments

ktsibikov picture ktsibikov  Â·  6Comments

grihabor picture grihabor  Â·  7Comments

shewitt-au picture shewitt-au  Â·  3Comments