Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I had to write an expression parser recently and found the number of names for essentially the same algorithm very confusing. There's Pratt parsing, precedence climbing, and shunting yard. But really they're all the same algorithm.

Pratt parsing and precedence climbing are the same except one merges left/right associativity precedence into a single precedence table (which makes way more sense). Shunting yard is the same as the others except it's iterative instead of recursive, and it seems like common implementations omit some syntax checking (but there's no reason you have to omit it).

Here's what I ended up with:

https://github.com/Timmmm/expr

I had to write that because somewhat surprisingly I couldn't find an expression parser library that supports 64-bit integers and bools. Almost all of them only do floats. Also I needed it in C++ - that library was a prototype that I later translated into C++ (easier to write in Rust and then translate).



There is Boost.Spirit. We use it but I find its cleverness just results in incomprehensible code.

https://www.boost.org/doc/libs/1_84_0/libs/spirit/classic/do...


Ah yeah I should have said "a library for which the integration is easier than writing my own library from scratch"!


Last I used it, even pretty small grammars took forever to build. I had to be very diligent not to let the grammar cpp-file depend on anything else in my codebase.


It wouldn't be efficient for expression parsing. Spirit is a recursive descent parser.


There are dozens of parser generators in C++ that support arbitrary types.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: