5

I have an Xtext/Antlr grammar that parses a subset of coffeescript. I have some test cases, but I thought of doing another sort of test:

  1. Generate random, syntactically correct snippets from my Antlr grammar
  2. Feed these snippets to the original coffeescript parser (calling coffee -ne "the sentence")
  3. Check if each sentence is parsed by coffeescript

Thus I could ensure that my parser accepts a proper subset, and it's not too permissive in some cases. Now, I am stuck with the first step. How can I generate sentences from my Antlr grammar (which also makes heavy use of syntactic predicates)? So I'm interested in the opposite of parsing a sentence.

I found some related attempts, but the answers are not using Antlr at all, but a custom grammar in python, or in clojure, or in ruby. I'd prefer a working solution rather than a hint about how it could be implemented.

Community
  • 1
  • 1
Adam Schmideg
  • 10,310
  • 9
  • 49
  • 79

1 Answers1

1

No, you can't do this. If you look at the code that ANTLR compiles into, you can see that it's only a recognizer, not a generator.

The links you provided are your best bet -- take your ANTLR grammar, strip out all the rules to make it into a formal grammar, and then try to run it through one of those programs.

Or if your coffeescript subset is very small, you could take the approach of generating strings of random tokens and throwing away all the strings that don't parse.

Nathaniel Waisbrot
  • 21,093
  • 6
  • 69
  • 94