Reputation: 15
Is there a way to generate (all possible) sentences from a given grammar?
Let's say I have the grammar:
grammar Drink;
//Parser Rules
drinkSentence : ARTICLE? DRINKING_VESSEL OF drink ;
drink : TEXT;
// Lexer Rules
ARTICLE : 'the' | 'an' | 'a' ;
OF : 'of' ;
DRINKING_VESSEL : 'cup' | 'pint' | 'shot' | 'mug' | 'glass' ;
TEXT : ('a'..'z')+ ;
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ -> skip ;
Would it be possible to generate all possible drinkSentences from this? I understand that e.g. the TEXT has a huge amount of possible combinations but for example's sake let's say that can only contain about 4 letters.
Upvotes: 0
Views: 142
Reputation: 53337
It's certainly doable, but you have to write a tool which does the job. To my knowledge there's no existing tool for this. A possible approach would be to walk the generated ATN and construct possible input from that.
However, I wonder for what you need such a generator. Is it that you want to create test input for your parser? If so you should think about this idea again. Your parser will always successfully parse correct input according to the grammar it was created for (assuming that there is no ANTLR4 bug here). How can you then test your always correctly working parser with your always correct input?
Upvotes: 0
Reputation: 170158
No, this is not possible with any built-in ANTLR functionality.
Upvotes: 1