3 ms·
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
by timhh 3y ago
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 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).
- secondcoming 3y agoThere 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/doc/introduction.html https://www.boost.org/doc/libs/1_84_0/libs/spirit/classic/do...
- timhh 3y agoAh yeah I should have said "a library for which the integration is easier than writing my own library from scratch"!
- amag 3y agoLast 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.
- nly 3y agoIt wouldn't be efficient for expression parsing. Spirit is a recursive descent parser.
- mgaunard 3y agoThere are dozens of parser generators in C++ that support arbitrary types.