2 //#if defined(FOUNDATION10) || defined(J2SE13)
5 *******************************************************************************
6 * Copyright (C) 2002-2009, International Business Machines Corporation and *
7 * others. All Rights Reserved. *
8 *******************************************************************************
10 package com.ibm.icu.dev.test.util;
12 import java.util.Random;
14 //TODO integrate this into the test framework
16 import com.ibm.icu.text.UnicodeSet;
18 public class TestBNF {
20 static final String[] testRules = {
36 "$root = a $foo b? 25% end 30% | $foo 50%;\r\n" +
39 "$root = [a-z]{1,5}~;",
43 "$root = '\\' (u | U0010 | U000 $hex) $hex{4} ;\r\n" +
44 "$hex = [0-9A-Fa-f];",
47 static String unicodeSetBNF = "" +
48 "$root = $leaf | '[' $s $root2 $s ']' ;\r\n" +
49 "$root2 = $leaf | '[' $s $root3 $s ']' | ($root3 $s ($op $root3 $s){0,3}) ;\r\n" +
50 "$root3 = $leaf | '[' $s $root4 $s ']' | ($root4 $s ($op $root4 $s){0,3}) ;\r\n" +
51 "$root4 = $leaf | ($leaf $s ($op $leaf $s){0,3}) ;\r\n" +
52 "$op = (('&' | '-') $s)? 70%;" +
53 "$leaf = '[' $s $list $s ']' | $prop;\r\n" +
54 "$list = ($char $s ('-' $s $char $s)? 30%){1,5} ;\r\n" +
55 "$prop = '\\' (p | P) '{' $s $propName $s '}' | '[:' '^'? $s $propName $s ':]';\r\n" +
56 "$needsQuote = [\\-\\][:whitespace:][:control:]] ;\r\n" +
57 "$char = [[\\u0000-\\U00010FFFF]-$needsQuote] | $quoted ;\r\n" +
58 "$quoted = '\\' ('u' | 'U0010' | 'U000' $hex) $hex{4} ;\r\n" +
59 "$hex = [0-9A-Fa-f];\r\n" +
60 "$s = ' '? 20%;\r\n" +
61 "$propName = (whitespace | ws) | (uppercase | uc) | (lowercase | lc) | $category;\r\n" +
62 "$category = ((general | gc) $s '=' $s)? $catvalue;\r\n" +
63 "$catvalue = (C | Other | Cc | Control | Cf | Format | Cn | Unassigned | L | Letter);\r\n";
65 public static void main (String[] args) {
67 for (int i = 0; i < testRules.length; ++i) {
68 testBNF(testRules[i], null, 20);
71 testBNF(unicodeSetBNF, null, 20);
75 static void testBNF(String rules, UnicodeSet chars, int count) {
76 BNF bnf = new BNF(new Random(0), new Quoter.RuleQuoter())
77 .addSet("$chars", chars)
81 System.out.println("====================================");
82 System.out.println("BNF");
83 System.out.println(rules);
84 System.out.println(bnf.getInternal());
85 for (int i = 0; i < count; ++i) {
86 System.out.println(i + ": " + bnf.next());
91 public static testManual() {
92 Pick p = Pick.maybe(75,Pick.unquoted("a"));
94 p = Pick.or(new String[]{"", "a", "bb", "ccc"});
96 p = Pick.repeat(3, 5, new int[]{20, 30, 20}, "a");
98 p = Pick.codePoint("[a-ce]");
100 p = Pick.codePoint("[a-ce]");
102 p = Pick.string(2, 8, p);
105 p = Pick.or(new String[]{"", "a", "bb", "ccc"});
106 p = Pick.and(p).and2(p).and2("&");
107 testMatch(p, "abb&");
114 static void testMatch(Pick p, String source) {
115 Pick.Position pp = new Pick.Position();
116 boolean value = p.match(source, pp);
117 System.out.println("Match: " + value + ", " + pp);
120 static void testParser() {
122 Pick.Target target = new Pick.Target();
123 for (int i = 0; i < rules.length; ++i) {
124 target.addRule(rules[i]);
126 } catch (ParseException e) {
127 // TODO Auto-generated catch block
133 static class Counts {
136 counts = new int[max+1];
138 void inc(int index) {
142 System.out.println("Printing Counts");
143 for (int i = 0; i < counts.length; ++i) {
144 if (counts[i] == 0) continue;
145 System.out.println(i + ": " + counts[i]);
147 System.out.println();
151 /* static final String[] rules = {
153 "$noun = dog | house | government | wall | street | zebra;",
154 "$adjective = red | glorious | simple | nasty | heavy | clean;",
155 "$article = quickly | oddly | silently | boldly;",
156 "$adjectivePhrase = ($adverb $s)? 50% $adjective* 0% 30% 20% 10%;",
157 "$nounPhrase = $articles $s ($adjectivePhrase $s)? 30% $noun;",
158 "$verb = goes | fishes | walks | sleeps;",
159 "$tverb = carries | lifts | overturns | hits | jumps on;",
160 "$copula = is 30% | seems 10%;",
161 "$sentence1 = $nounPhrase $s $verb $s ($s $adverb)? 30%;",
162 "$sentence2 = $nounPhrase $s $tverb $s $nounPhrase ($s $adverb)? 30%;",
163 "$sentence3 = $nounPhrase $s $copula $s $adjectivePhrase;",
164 "$conj = but | and | or;",
165 "$sentence4 = $sentence1 | $sentence2 | $sentence3 20% | $sentence4 $conj $sentence4 20%;",
166 "$sentence = $sentence4 '.';"};
169 private static void testEnglish() {
170 Pick s = Pick.unquoted(" ");
171 Pick verbs = Pick.or(new String[]{"goes", "fishes", "walks", "sleeps"});
172 Pick transitive = Pick.or(new String[]{"carries", "lifts", "overturns", "hits", "jumps on"});
173 Pick nouns = Pick.or(new String[]{"dog", "house", "government", "wall", "street", "zebra"});
174 Pick adjectives = Pick.or(new String[]{"red", "glorious", "simple", "nasty", "heavy", "clean"});
175 Pick articles = Pick.or(new String[]{"the", "a"});
176 Pick adverbs = Pick.or(new String[]{"quickly", "oddly", "silently", "boldly"});
177 Pick adjectivePhrase = Pick.and(0.5, Pick.and(adverbs).and2(s)).and2(adjectives);
178 Pick nounPhrase = Pick.and(articles).and2(s)
179 .and2(0.3, Pick.and(adjectivePhrase).and2(s))
181 Pick copula = Pick.or(new String[]{"is", "seems"});
182 Pick sentence1 = Pick.and(nounPhrase).and2(s).and2(verbs)
183 .and2(0.3, Pick.and(s).and2(adverbs)).name("s1");
184 Pick sentence2 = Pick.and(nounPhrase).and2(s).and2(transitive).and2(s).and2(nounPhrase)
185 .and2(0.3, Pick.and(s).and2(adverbs)).name("s2");
186 Pick sentence3 = Pick.and(nounPhrase).and2(s).and2(copula).and2(s).and2(adjectivePhrase).name("s3");
187 Pick conj = Pick.or(new String[]{", but", ", and", ", or"});
188 Pick forward = Pick.unquoted("forward");
189 Pick pair = Pick.and(forward).and2(conj).and2(s).and2(forward).name("part");
190 Pick sentenceBase = Pick.or(sentence1).or2(sentence2).or2(sentence3).or2(0.6666, pair).name("sentence");
191 sentenceBase.replace(forward, sentenceBase);
192 Pick sentence = Pick.and(sentenceBase).and2(Pick.unquoted("."));
193 Pick.Target target = Pick.Target.make(sentence);
194 for (int i = 0; i < 50; ++i) {
195 System.out.println(i + ": " + target.next());
198 private static void testOr(Pick p, int count) {
199 Pick.Target target = Pick.Target.make(p);
200 Counts counts = new Counts(count + 10);
201 for (int i = 0; i < 1000; ++i) {
202 String s = target.next();
203 counts.inc(s.length());
207 private static void testCodePoints(Pick p) {
208 Pick.Target target = Pick.Target.make(p);
209 Counts counts = new Counts(128);
210 for (int i = 0; i < 10000; ++i) {
211 String s = target.next();
212 counts.inc(s.charAt(0));
217 public static void printRandoms() {
218 BNF bnf = new BNF(new Random(0), new Quoter.RuleQuoter())
219 .addRules("[a-z]{2,5}").complete();
220 System.out.println("Start");
221 for (int i = 0; i < 100; ++i) {
222 String temp = bnf.next();
223 System.out.println(i + ")\t" + temp);
227 public static void testTokenizer() {
228 Tokenizer t = new Tokenizer();
230 String[] samples = {"a'b'c d #abc\r e", "'a '123 321",
231 "\\\\", "a'b", "a'", "abc def%?ghi", "%", "a", "\\ a", "a''''b"};
232 for (int i = 0; i < samples.length; ++i) {
233 t.setSource(samples[i]);
234 System.out.println();
235 System.out.println("Input: " + t.getSource());
237 while (type != Tokenizer.DONE) {
239 System.out.println(t.toString(type, false));