0

For example, I had a .g4 of calculating expressions. ANTLR4 can parse any valid input of calculation expression as a parser tree. I'm considering the following scenario.

Without any input, is it possible to generate the new valid calculation expressions based on ANTLR4? It should be a grammar-based generator.

Does anyone have a suggestion about how to build a grammar-based generator with ANTLR4? Thanks in advance!

Kevin Lu
  • 53
  • 2
  • [Grammarinator](https://github.com/renatahodovan/grammarinator). Unfortunately, the generated code may not look realistic because the default model just randomizes every decision. Example use: `grammarinator-process VerilogLexer.g4 VerilogParser.g4 -o .; grammarinator-generate VerilogGenerator.VerilogGenerator --sys-path . -d 15 -n 100 -r source_text --serializer grammarinator.runtime.simple_space_serializer --no-mutate --no-recombine`. The algorithm they employ converts the grammar to a tree walker that generates a parse tree, then serializes it to a string. – kaby76 Mar 24 '22 at 18:50
  • The algorithm is conceptually simple: perform a left-to-right traversal of the RHS of a rule starting with the start rule, recursing on a parser or lexer rule reference, constructing a tree on output. This can be done using a visitor for the Antlr4 grammar itself. Again, one needs a "choice model" to pick choices for "alts"/"|" and kleene-operators. However, I disagree with pure random choices Grammarinator uses as it generates mostly unrealistic tests. It would be better to use a corpus of examples that a tool would query to create a "choice model" with realistic outputs. – kaby76 Mar 24 '22 at 19:05
  • @BartKiers This question is certainly too broad and needs fine tuning, but closing it by referencing a 9 years old other question (which certainly is outdated meanwhile), is the wrong decision IMO. – Mike Lischke Mar 25 '22 at 08:02
  • @MikeLischke IMO, the situation w.r.t. ANTLR hasn't changed. Both version 3 and version 4 do not support this kind of thing. And (again IMO) "needs fine tuning" is a rather big understatement. But feel free to vote to reopen this question, of course. – Bart Kiers Mar 25 '22 at 08:36
  • Oh wait, I thought you also had 1-click re-open privileges for one of the assigned tags... I'll re-open then. – Bart Kiers Mar 25 '22 at 08:38
  • Bart, cool, thanks. @KevinLu if you can improve your question, show us what you did yourself and where you struggle, we might be able to help. I'm actually working on a sentence generator (more or less), so I can give you first hand input, if you provide a solid question. – Mike Lischke Mar 25 '22 at 10:26
  • @mike: the nine-year old question is perfectly relevant today. An answer may be outdated; if so, a new answer can be provided, by you or anyone else. A question is a duplicate of another question if both would have the same answer; linking them by declaring one a duplicate of the other is a way of improving search quality, not a vote against either of the questioners. I didn't mark this as a duplicate in the first place because I wasn't sure that the questions were sufficiently similar, and I wanted a second opinion. – rici Mar 25 '22 at 15:58
  • 1
    Fwiw, the potential dupes I noted in a comment yesterday are https://stackoverflow.com/questions/3429368/is-there-a-utility-which-given-an-antlr-grammar-will-produce-matching-strings and https://stackoverflow.com/questions/8478320/generate-syntactically-correct-sentences-from-an-antlr-grammar – rici Mar 25 '22 at 16:15
  • The questions are really of a broad topic that has been researched since at least [Purdom, P., 1972. A sentence generator for testing parsers. BIT Numerical Mathematics, 12(3), pp.366-375.](https://link.springer.com/article/10.1007/BF01932308). For what it's worth, reading the Grammarinator source code and the generated output code may help. The implementation in Jinja is particularly interesting. It converts the Antlr parse tree into code that closely resembles a recursive descent parser. – kaby76 Mar 25 '22 at 18:50

0 Answers0