Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Strings from Grammar in ScalaCheck

In Scala, I have a grammar implemented using the Parser Combinators library. Now, what I want to do is generate random strings given a grammar from the parser combinators library.

It seems to me, that what the ScalaCheck library does it somehow the opposite of Parser Combinators in that it combines generators instead of parsers.

Is there already a way to generate strings using the Parser Combinators or ScalaCheck, or is there a straightforward way of transforming a Parser Combinator into a generator?

like image 703
Felix Avatar asked Aug 17 '15 12:08

Felix


1 Answers

There's no straightforward way to convert your grammar to generators. You have to write them manually. And it won't be that hard, because you already have a grammar. You can easily test your parser, but testing your type checker can be quite problematic (but still possible). Before you begin, make sure that your AST nodes can be compared to each other.

  • Scalacheck allows you to generate recursive properties, so you can easily generate AST nodes.
  • When you have your AST nodes generated you could use Scalacheck and some additional knowledge about white-spaces and their complaisance between the nodes, translated into strings.
  • Then you can feed the generated strings to the parser, you're going to test of code and compare them with pre-generated AST
like image 56
ppopoff Avatar answered Oct 18 '22 05:10

ppopoff