0

I cannot and do not know how to retrieve the values of an AST that I generated using the Lark parser.

My grammar is as follows, saved in a .lark file :

start: (un_handle ": ")? AMOUNT "|" p_handle ("," p_handle)* (" \"" MESSAGE* "\"")?

AMOUNT: /[0-9]+(\.[0-9][0-9]?)?/

un_handle: HANDLE

p_handle: HANDLE

HANDLE : /[A-Z][A-Z]/

MESSAGE : /[^"]+/

I then run:

testText = '10|GP "Bananas"'
testTree = parser.parse(testText)

and get:

Tree(start, [Token(AMOUNT, '10'), Tree(p_handle, [Token(HANDLE, 'GP')]), Token(MESSAGE, 'Bananas')])

But, what now?

I realize that I have to probably have to build a transformer, but what methods should I define and what should I call them? I just want to extract the values for AMOUNT, un_handle, p_handle (there may be more than one p_handle), and message into Python variables.

Thank you so much in advance! Have been debugging for hours.

Willy
  • 322
  • 1
  • 17
  • You can just call your methods `p_handle`, `un_handle`, `start`, etc. What difficulty are you having with it? – Erez Jul 06 '19 at 11:06

1 Answers1

0

First off, try adding a "line" rule to provide a reference point. Yes, your application does not probably use multiple lines, but it is usually good to include one just in case.

Now, write a subroutine to find each "line" token in the AST, and append it to a list.

Finally, I suggest that you process the resulting list using a subroutine based upon the eval() subroutine in LisPy.