Is it possible to do bi-directional parsing with Lark? #1396
Replies: 2 comments 2 replies
-
Lark has a reconstructor, but you have to still manually build a pretty printer (inserting the correct ignored tokens, to for example match indentations between lines in a C-like language) manually on top of that. At least based on my reading of the abstract and looking at the examples, I don't think the first linked paper actually does what you want. I am not actually sure what they mean with "bidirectional". If you want to contribute facilities to do something like this, it might be possible to add them to lark. However, note that there is a pretty fundamental difference in syntactic form between a templating engine and a BNF-based parser. This might make more sense as a different library that uses lark in the background. |
Beta Was this translation helpful? Give feedback.
-
Lark's reconstructor pretty much does what was described in that reddit post. Because the Lark grammar supports a few simple rules for turning CSTs directly into ASTs (e.g. dropping literals, inlining rules), we can also do the reverse operation. There is some ambiguity involved, but it can be solved relatively efficiently with our Earley parser. However, we don't yet have a good formulation for what derivations to prioritize, so sometime the resulting code can have some unnatural artifacts. It works and is even used by other packages, but mostly for simple languages like JSON. (as aside: it would be interesting to see if LLMs can smooth those artifacts out). P.S. thanks for making it a discussion and not an issue ;) |
Beta Was this translation helpful? Give feedback.
-
I was reading about bi-directional parsing, where you define your parser/grammar and pretty-printer/templating-language at the same time.
The basic idea is expressed by this example:
Is it possible to do something like that with Lark?
Beta Was this translation helpful? Give feedback.
All reactions