human-parser-generator icon indicating copy to clipboard operation
human-parser-generator copied to clipboard

Improvements

Open adams85 opened this issue 6 years ago • 3 comments

Hi!

I was looking for a parser generator and your project matched my requirements. However, I needed to make some improvements so i decided to fork it (to be precise, the 1.1-alpha version).

I've completely rewritten the ParserBase class as it had some problems.

  • You throw and catch exceptions to propagate them. Throwing exceptions in C# is expensive because of stack trace creation, etc. They are for exceptional cases, failure of a parse rule is not such a case.
  • You use a deep stack of function calls for parsing. Call stack is limited (see https://stackoverflow.com/questions/29832311/call-stack-limitation-in-c-sharp), so I changed this to emulating call stack using the Stack<T> class. So the entire memory of the computer is available for the parser.

I revised the Parsable class (renamed to Scanner) and C# code emitter and made some performance optimalization: perf_old

perf_new

I measured 10,000 iterations of parsing the pascal assignment grammar. There is some boost.

I made the generated code more readable, as well (https://github.com/adams85/human-parser-generator/blob/improvements/netcore/parser.cs), and included generation of the visitor pattern. (For demonstration, I rewrote AST generation using the visitor. It emits now formatted JSON.)

What do you think? Do you have any intention to merge this into the original project? I used C# 7 features heavily, but as far as I know mono supports it already. (But you may consider NET Core 2.0. I think, that's the future of cross-platform development.)

I gladly help you with developing the project if you choose so because it can become a neat tool. As a matter of fact, it is like that already. It needs some code cleanup/organization and there are some glitches in the model/code generation that should be fixed.

adams85 avatar Sep 29 '17 15:09 adams85

17x speedup. Very nice!

masonwheeler avatar Sep 29 '17 15:09 masonwheeler

Hi Adam,

you have been busy :-) Great, it's always a nice surprise when people appreciate some thing you have been working on and take it for a (big) spin.

Regarding the pull request - after a very quick browse: those are two very big commits and they will need some love to get them into the master tree. Do give me some time and I'll dig into it to see how we can make this work.

To be continued! Christophe

christophevg avatar Oct 02 '17 18:10 christophevg

Okay, I hope you'll find it useful! Just let me know if you have some questions.

adams85 avatar Oct 02 '17 19:10 adams85