Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trees are always simpler to work with than state-machines. #4

Open
kootenpv opened this issue Mar 20, 2017 · 4 comments
Open

Trees are always simpler to work with than state-machines. #4

kootenpv opened this issue Mar 20, 2017 · 4 comments
Labels

Comments

@kootenpv
Copy link

Hey, it looks very interesting!

You make this statement 2 times: "Trees are always simpler to work with than state-machines."

Could you back this up with some resource or examples? I'm very curious.

@erezsh
Copy link
Member

erezsh commented Mar 20, 2017

Hi, thanks!
The simple solution is that the parse-trees that lark produces are reduce-able to a parser's state-machine. If you use the Transformer class on a tree, it will call your methods at the exact same order you would get in a traditional parser, and your return-values will be treated the same. So at the very least, a parse-tree is equivalent to a state-machine. But:

  1. Trees allow you to see the "state-machine" visually: At once, instead of in steps. You can also see the tree fold step-by-step as you process it.
  2. Trees allow your computation to be aware of previous and future states, so they are more powerful computationally.
  3. You can process a tree in steps. That means you can separate your processing into several logical steps if you like. A state-machine requires that each method performs a complete computation.

I am not aware of any benefits for state-machines, besides performance. But Lark solves that too: Once you write a complete transformer, you can hand it off to Lark and it will execute it as a state-machine, without building a tree (for example, when parsing json, it uses about half the memory)

@charles-esterbrook
Copy link

This could go in the docs somewhere if it's not already there.

@charles-esterbrook
Copy link

Cool, then I think this ticket can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants