1. 14

I wrote one popular expression evaluation library: https://github.com/antonmedv/expr

Here is the first implementation of the parser: https://github.com/antonmedv/expr/blob/v1.1.4/parser.go

After I was thinking about using ANTLR: https://github.com/antonmedv/expr/blob/v1.4.5/parser/parser.go

But, bugs in ANTLR go bindings, lack of control of error massages, made we return back to the handwritten parser: https://github.com/antonmedv/expr/blob/master/parser/parser.go

Also, notice what removing all if err != nil in the latest parser evolution makes code readable.


  2. 4

    For simple expression parsing, Pratt + operator precedence parsing is a sweet spot in terms of code economy and fewer moving parts. So this makes total sense. I’m not sure when I’d elect to use ANTLR, honestly, despite preferring LL algorithms to LALR generally.

    1. 3

      My current hypothesis is that parser generators really pay off only if you want to support multiple languages within one tool. Otherwise, a hand written parser is less work: it’s (constant factor) more code, but dramatically less complexity.

      One caveat is that a hand-written parser does not give you a proof that your language is in a well-behaved grammar class.

      1. 1

        They are very nice in that case, having consumed an ANTLR grammar in both Python and C++. However, this is quite niche IMO.

        1. 1

          I think we are actually taking about two different use cases for parser generators. If I understand correctly, yours is “I want to share the same grammar for language X to generate parsers in two languages”, mine is “I need to parse languages X, Y and Z”. I haven’t thought about the first one, but it makes sense.

      2. 2

        I personally do not like ANTLR, I find that the leniency by which is accepts grammars with ambiguities leads to code that is extremely poor in performance, eliminating one of the reasons to choose a parser generator in the first place. That, and I’ve had problems with bugs in the runtime before

        1. 1

          One of those back-of-mind ideas I have is writing an LSP for a language without one, nor does it have a formal grammar; would it make sense to use ANTLR, or try to roll your own parser?

          1. 1

            Writing your own parser is fun, but can be challenging. Try to start writing your own parser, It’s better to understand how antlr working.

          2. 1

            I needed to build a parser for SQL (actually N1QL, a superset) last year. I went through ANTLR and Lemon and a couple of other parser generators and settled on a PEG / packrat parser tool called “peg”. I really liked the simplicity of the grammar specifications; you effectively get infinite lookahead so there’s a lot less trouble with ambiguity. (Also, lexing is unified with parsing.)

            1. 1

              Can the expr editor perform expression evaluation in the browser?

              1. 1

                Currently no, bit now with rewriting parser it’s possible to do, as code can be compiled to js.