Synthia
Generic and flexible data structure generator
GrammarDemo.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2020 Laboratoire d'informatique formelle
4  Université du Québec à Chicoutimi, Canada
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /**
21  * @ingroup Examples
22  */
23 package examples.grammar;
24 
25 import ca.uqac.lif.bullwinkle.BnfParser;
26 import ca.uqac.lif.bullwinkle.BnfParser.InvalidGrammarException;
29 
30 /**
31  * Generates character strings corresponding to sentences formed according
32  * to a BNF grammar.
33  * The simple grammar in this example, found in the file <tt>grammar.bnf</tt>,
34  * is the following:
35  * <pre>
36  * &lt;S&gt; := The &lt;adjective-group&gt; &lt;noun-verb&gt; the &lt;adjective-group&gt; &lt;noun&gt; . ;
37  * &lt;adjective-group&gt; := &lt;observation&gt; &lt;size&gt; &lt;age&gt; &lt;color&gt; ;
38  * &lt;noun-verb&gt; := &lt;nv-singular&gt; | &lt;nv-plural&gt; ;
39  * &lt;nv-singular&gt; := &lt;noun&gt; &lt;verb&gt; ;
40  * &lt;nv-plural&gt; := &lt;nouns&gt; &lt;verbs&gt; ;
41  * &lt;noun&gt; := fox | &lt;dog&gt; | &lt;cat&gt; | bird ;
42  * &lt;verb&gt; := watches | plays with | runs after ;
43  * &lt;nouns&gt; := cats | dogs | birds | farmers ;
44  * &lt;verbs&gt; := play with | run after | watch ;
45  * &lt;observation&gt; := lovely | funny | ugly | quick | ε ;
46  * &lt;size&gt; := big | small | ε ;
47  * &lt;age&gt; := young | old | ε ;
48  * &lt;color&gt; := brown | black | white | grey | ε ;
49  * &lt;dog&gt; := poodle | pitbull | dog ;
50  * &lt;cat&gt; := siamese | Cheshire cat | burmese cat | cat ;
51  * </pre>
52  * <p>
53  * A sample run of the program could be:
54  * <pre>
55  * The ugly small old black cats run after the funny brown pitbull .
56  * The small young grey fox runs after the ugly old Cheshire cat .
57  * The old grey bird watches the funny brown poodle .
58  * The funny cats watch the funny old fox .
59  * &hellip;
60  * </pre>
61  * @author Sylvain Hallé
62  */
63 public class GrammarDemo
64 {
65  public static void main(String[] args) throws InvalidGrammarException
66  {
67  /* Instantiate a Bullwinkle parser with the grammar from the file. */
68  BnfParser parser = new BnfParser(GrammarDemo.class.getResourceAsStream("grammar.bnf"));
69  parser.setStartRule("<S>");
70 
71  /* Create a GrammarSentence picker by passing this parser. */
72  GrammarSentence gp = new GrammarSentence(parser, new RandomInteger());
73 
74  /* Show a few sentences derived from the grammar. */
75  for (int i = 0; i < 10; i++)
76  {
77  System.out.println(gp.pick());
78  }
79  }
80 }
examples.grammar.GrammarDemo
Generates character strings corresponding to sentences formed according to a BNF grammar.
Definition: GrammarDemo.java:63
ca.uqac
ca.uqac.lif.synthia.random
Pickers that produce pseudo-random objects such as numbers.
Definition: AffineTransform.java:19
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif
ca.uqac.lif.synthia.random.RandomInteger
Picks an integer uniformly in an interval.
Definition: RandomInteger.java:31
ca
ca.uqac.lif.synthia.grammar
Pickers generating sequences based on a context-free grammar.
Definition: GrammarSentence.java:19
ca.uqac.lif.synthia.grammar.GrammarSentence.pick
String pick()
Picks an object.
Definition: GrammarSentence.java:77
ca.uqac.lif.synthia.grammar.GrammarSentence
Picker that generates sentences from a format grammar.
Definition: GrammarSentence.java:39
examples.grammar.GrammarDemo.main
static void main(String[] args)
Definition: GrammarDemo.java:65