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;
27
import
ca
.
uqac
.
lif
.
synthia
.
grammar
.
GrammarSentence
;
28
import
ca
.
uqac
.
lif
.
synthia
.
random
.
RandomInteger
;
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
* <S> := The <adjective-group> <noun-verb> the <adjective-group> <noun> . ;
37
* <adjective-group> := <observation> <size> <age> <color> ;
38
* <noun-verb> := <nv-singular> | <nv-plural> ;
39
* <nv-singular> := <noun> <verb> ;
40
* <nv-plural> := <nouns> <verbs> ;
41
* <noun> := fox | <dog> | <cat> | bird ;
42
* <verb> := watches | plays with | runs after ;
43
* <nouns> := cats | dogs | birds | farmers ;
44
* <verbs> := play with | run after | watch ;
45
* <observation> := lovely | funny | ugly | quick | ε ;
46
* <size> := big | small | ε ;
47
* <age> := young | old | ε ;
48
* <color> := brown | black | white | grey | ε ;
49
* <dog> := poodle | pitbull | dog ;
50
* <cat> := 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
* …
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
Source
Examples
src
examples
grammar
GrammarDemo.java
Generated by
1.8.17