<p>Does anyone have any experience with a tool that generates test strings from a BNF grammar that could then be fed into a unit test?</p>

[{'answer_id': 52302, 'author': 'hazzen', 'author_id': 5066, 'author_profile': 'https://Stackoverflow.com/users/5066', 'pm_score': 1, 'selected': False, 'text': "<p>I don't have an answer to the tool question, but I will say it is fairly easy in any text processing language (perl/python/etc) to randomly generate sentences from a BNF grammar, and slightly more verbose in a bigger language (Java/C/etc), but it shouldn't be too hard to roll your own.</p>\n\n<p>The problem with this, of course, is that it can only generate strings in the grammar, and unless your grammar is very simple, the test space is infinitely large.</p>\n"}, {'answer_id': 52324, 'author': 'joel.neely', 'author_id': 3525, 'author_profile': 'https://Stackoverflow.com/users/3525', 'pm_score': 1, 'selected': False, 'text': "<p>I've done exactly as hazzen commented (using an embedded DSL in a scripting language). It was a mildly interesting exercise, but except for the most basic tests of e.g. parsing, it wasn't terribly useful. Most of my most interesting tests have to do with more sophisticated relationships than one can easily express in BNF (or any other context-free grammar).</p>\n"}, {'answer_id': 85564, 'author': 'EfForEffort', 'author_id': 14113, 'author_profile': 'https://Stackoverflow.com/users/14113', 'pm_score': 0, 'selected': False, 'text': '<p>If, say, you\'re developing a compiler, then you likely have an abstract syntax tree datatype.  If so, then you could write a function to generate an random AST -- with that, you can print it to a string and feed that to your unit test.  It\'s guaranteed to be a valid program this way, since you <em>started</em> with your AST.</p>\n\n<p>If I were writing a compiler in Haskell or ML, this is what I would do, using <a href="http://www.cs.chalmers.se/~rjmh/QuickCheck/" rel="nofollow noreferrer">QuickCheck</a>.</p>\n'}, {'answer_id': 32621392, 'author': 'codelion', 'author_id': 598029, 'author_profile': 'https://Stackoverflow.com/users/598029', 'pm_score': 0, 'selected': False, 'text': '<p><a href="https://github.com/codelion/gramtest" rel="nofollow">Gramtest</a> is one such tool that can generate strings from arbitrary user defined BNF grammars. You can read more details about the algorithm behind Gramtest <a href="https://blog.srcclr.com/how-does-grammar-based-test-case-generation-work/" rel="nofollow">here</a> and some practical tips on the tool are available <a href="https://blog.srcclr.com/practical-tips-for-implementing-grammar-based-test-case-generation/" rel="nofollow">here</a>.</p>\n'}]
['https://Stackoverflow.com/questions/52167', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3631/']