kaby76 / Trash

Toolkit for grammars
MIT License
71 stars 5 forks source link

Convert ANTLR4 to LALR-1 (yacc,yecc) parser #133

Open Aleyasen opened 2 years ago

Aleyasen commented 2 years ago

I am wondering does Trash support converting ANTLR4 parser files to a LALR-1 (e.g. yacc) files? And if no, is it possible to have that conversion at all? I know the fundamental for these two are quite different (top-down vs. bottom-up), I am NOT looking for a perfect solution, but a solution can take care of more of the cases. Thanks.

kaby76 commented 2 years ago

Hi, I don't have any Antlr to other converters implemented yet. I am planning on adding that, but I need to work on first ironing out the refactoring tools. So, for the moment, you'll need to do the conversion via hand.

But, for this problem, Antlr4 grammars are generally more powerful than LALR(1). What you could do it to check if the parser doesn't require more than one symbol lookahead for inputs in order to decide what rule to take when during the parse. I think you can do this with the Intellij Antlr4 plugin and try parsing an example then see the "max k" that it used in the parse. I don't recall if I have the same functionality in Trash at the moment, but that is something I was planning on adding.

Assuming that the Antlr parser doesn't take more than k=1, then you can probably just do a simple edit of the source to take it into Yacc.

If it didn't work for k=1, then the only other thing I would suggest is to try the GLR mode for Bison. That should take unlimited k lookahead, at least I think so.