3 ms·
For what do people use parser generators, besides writing new programming languages? I'm very curious to know about the use cases.
by fiatjaf 11y ago
For what do people use parser generators, besides writing new programming languages? I'm very curious to know about the use cases.
- e12e 11y agoThe sibling comment alludes to parsing calendar events (possibly .ical files). In general you need it for any kind of (de)serialization -- and as the recent java deserialization bug[1] (along with similar bugs in python, ruby) illustrates -- even in a mature language, "common practice" might not be "best practice" (everyone should've known that the various uses of blind deserialization was a bad idea -- but that doesn't really matter after the fact -- lots of high-profile libraries were completely broken). So "just use a library" might not be possible. Any time you take native objects/data and persist them, or marshal them, you need a parser -- or a parser library. That might mean reading some CSV, parsing some XML, reading some YAML, JSON or what-not. Then there's configuration that does more than set some variables, like for Nginx, Apache (web server), etc. One might want to have a short-hand for defining templates -- either for general meta-programming, or just to generate a layout-language (eg: html, css). If you're doing markdown (or markdown-like) processing, you might want to have some assurances that that's all that code can do -- generate html. Or you might want do meta-programming on some higher level, like generating interfaces for some java-beans based on a CSV structure. For more on meta-programming in Java, I recommend (the now slightly dated: http://www.amazon.com/Program-Generators-Java-Craig-Cleaveland/dp/0130258784 http://www.amazon.com/Program-Generators-Java-Craig-Cleavela... ). It's a shame he didn't name it as he claims he considered in the foreword: "Program Generators for Fun and Profit!". And finally there's "real" programming languages - full blown DSLs of some kind. Perhaps in an effort to represent some kind of business logic (eg: authorization based on group membership, roles and time of day) in a language that is "safe" -- has no side-effects. [1] http://fishbowl.pastiche.org/2015/11/09/java_serialization_bug/ http://fishbowl.pastiche.org/2015/11/09/java_serialization_b... http://blog.codeclimate.com/blog/2013/01/10/rails-remote-code-execution-vulnerability-explained/ http://blog.codeclimate.com/blog/2013/01/10/rails-remote-cod...
- Blackthorn 11y agoThey should be used anywhere you receive input for a defined protocol, rather than writing your own ad-hoc implementation. Security is much, much better that way.
- YorickPeterse 11y agoI used one to parse XML/HTML, CSS selectors, and XPath expressions. Parser generator in question is https://github.com/YorickPeterse/ruby-ll https://github.com/YorickPeterse/ruby-ll which is an LL(1) parser generator in Ruby. Example grammars: https://github.com/YorickPeterse/oga/blob/master/lib/oga/css/parser.rll https://github.com/YorickPeterse/oga/blob/master/lib/oga/css... https://github.com/YorickPeterse/oga/blob/master/lib/oga/xml/parser.rll https://github.com/YorickPeterse/oga/blob/master/lib/oga/xml... https://github.com/YorickPeterse/oga/blob/master/lib/oga/xpath/parser.rll https://github.com/YorickPeterse/oga/blob/master/lib/oga/xpa...