Jump to content
  • Advertisement
Sign in to follow this  
godsenddeath

When to use a parser?

This topic is 2906 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by godsenddeath
rather than regex/simple comparisons


That is a parser too.

Quote:
It tends to add quite a bit of overhead
Same rule applies as with everything: when overhead is smaller than the benefit.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Same 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!