News for hackers. Stuff that matters.
Re:Pratt Parsers: Expression Parsing Made Easy (2011) by jopsen on Saturday February 17, 2018 @05:19PM (#16403082)
I recently needed a custom expression language and using a Pratt-Parser to write the interpreter was a joy.

https://github.com/taskcluster/json-e/blob/a0ab770157a5fdb5d...

I wrote up abstractions so you just declare token patterns, precedence ordering of tokens, and infix/prefix rules as mapping from token to function(token, ctx). Where ctx.attempt(...token)), ctx.require(...token) and ctx.parse(precedence) is all you need to implement almost anything.

I didn't get as far as to make it recursive and support multiple grammars. But I'm sure this concept can be made very elegant, without too many tricks.

There are no comments on this comment yet.