   When to use a parser?
  I've often wondered, at what complexity does it start making sense to use a parser rather than regex/simple comparisons?For instance, the OBJ and MD5 files.  Is writing a full parser with lex/syntactic analysis overkill?  It tends to add quite a bit of overhead, but is more robust and can deal with somewhat malformed files.So when is a Parser required/overkill?   
Quote:Original post by godsenddeathrather than regex/simple comparisonsThat is a parser too.Quote:It tends to add quite a bit of overheadSame rule applies as with everything: when overhead is smaller than the benefit.   ;  Do you mean human readable files by the "parser" stuff?If so: if you want it to be human readable, make it human readable. Otherwise don't...   ;  It depends on the formal complexity of grammar you are parsing. It makes sense to use a third-party parser if the grammar has recursive structure. Generally, if there is something in the grammar that is as complex as an expression in a programming language you want to be using a real parser.   ;  Error recovery is a very complex subject - dealing with malformed data is far more difficult than you might think. Unless you are specifically interested in learning parser theory, I'd recommend shying away from it, as it will eat up a huge amount of time and effort for essentially no benefits in trivial file formats like OBJ.Something like OBJ or MD5 is such a simple format that you can roll your own parser in a couple of hours tops if you're comfortable with string manipulation in your language of choice. Even something like a limited subset of XML (or JSON or s-expressions or whatnot) can be parsed very easily with a hand-written recursive descent parser. There are only a very limited number of ways in which data can be malformed in OBJ or MD5, and you can trap them pretty easily by hand with some simple string comparison/pattern matching checks. I'd be shocked if you even needed to break out regular expressions, although there may well be cases where doing so could simplify things a tiny bit.Getting into specifying full grammars and using parser-generator toolchains is a major investment. If you're working with a file format that is genuinely that complex, there should already be libraries for parsing it for you. If not, pick a different format; life's too short [wink]   ;Quote:Original post by AntheusSame rule applies as with everything: when overhead is smaller than the benefit.This. Though I would caution you to remember that maintaining a parser (especially a generated one) is going to be a bit challenging for the run of the mill programmer who isn't familiar with the code. Just remember that when figuring the amount of overhead involved.