2 *******************************************************************************
\r
3 * Copyright (C) 2002-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.util;
\r
9 import java.util.Random;
\r
11 import com.ibm.icu.text.UnicodeSet;
\r
13 public class TestBNF {
\r
15 static final String[] testRules = {
\r
18 "$root = [ab]{3,};",
\r
20 "$root = [ab]{3,5};",
\r
29 "$root = [0-9$us];",
\r
31 "$root = a $foo b? 25% end 30% | $foo 50%;\r\n" +
\r
32 "$foo = c{1,5} 20%;",
\r
34 "$root = [a-z]{1,5}~;",
\r
36 "$root = [a-z]{5}~;",
\r
38 "$root = '\\' (u | U0010 | U000 $hex) $hex{4} ;\r\n" +
\r
39 "$hex = [0-9A-Fa-f];",
\r
42 static String unicodeSetBNF = "" +
\r
43 "$root = $leaf | '[' $s $root2 $s ']' ;\r\n" +
\r
44 "$root2 = $leaf | '[' $s $root3 $s ']' | ($root3 $s ($op $root3 $s){0,3}) ;\r\n" +
\r
45 "$root3 = $leaf | '[' $s $root4 $s ']' | ($root4 $s ($op $root4 $s){0,3}) ;\r\n" +
\r
46 "$root4 = $leaf | ($leaf $s ($op $leaf $s){0,3}) ;\r\n" +
\r
47 "$op = (('&' | '-') $s)? 70%;" +
\r
48 "$leaf = '[' $s $list $s ']' | $prop;\r\n" +
\r
49 "$list = ($char $s ('-' $s $char $s)? 30%){1,5} ;\r\n" +
\r
50 "$prop = '\\' (p | P) '{' $s $propName $s '}' | '[:' '^'? $s $propName $s ':]';\r\n" +
\r
51 "$needsQuote = [\\-\\][:whitespace:][:control:]] ;\r\n" +
\r
52 "$char = [[\\u0000-\\U00010FFFF]-$needsQuote] | $quoted ;\r\n" +
\r
53 "$quoted = '\\' ('u' | 'U0010' | 'U000' $hex) $hex{4} ;\r\n" +
\r
54 "$hex = [0-9A-Fa-f];\r\n" +
\r
55 "$s = ' '? 20%;\r\n" +
\r
56 "$propName = (whitespace | ws) | (uppercase | uc) | (lowercase | lc) | $category;\r\n" +
\r
57 "$category = ((general | gc) $s '=' $s)? $catvalue;\r\n" +
\r
58 "$catvalue = (C | Other | Cc | Control | Cf | Format | Cn | Unassigned | L | Letter);\r\n";
\r
60 public static void main (String[] args) {
\r
62 for (int i = 0; i < testRules.length; ++i) {
\r
63 testBNF(testRules[i], null, 20);
\r
66 testBNF(unicodeSetBNF, null, 20);
\r
70 static void testBNF(String rules, UnicodeSet chars, int count) {
\r
71 BNF bnf = new BNF(new Random(0), new Quoter.RuleQuoter())
\r
72 .addSet("$chars", chars)
\r
76 System.out.println("====================================");
\r
77 System.out.println("BNF");
\r
78 System.out.println(rules);
\r
79 System.out.println(bnf.getInternal());
\r
80 for (int i = 0; i < count; ++i) {
\r
81 System.out.println(i + ": " + bnf.next());
\r
86 public static testManual() {
\r
87 Pick p = Pick.maybe(75,Pick.unquoted("a"));
\r
89 p = Pick.or(new String[]{"", "a", "bb", "ccc"});
\r
91 p = Pick.repeat(3, 5, new int[]{20, 30, 20}, "a");
\r
93 p = Pick.codePoint("[a-ce]");
\r
95 p = Pick.codePoint("[a-ce]");
\r
97 p = Pick.string(2, 8, p);
\r
100 p = Pick.or(new String[]{"", "a", "bb", "ccc"});
\r
101 p = Pick.and(p).and2(p).and2("&");
\r
102 testMatch(p, "abb&");
\r
103 testMatch(p, "bba");
\r
109 static void testMatch(Pick p, String source) {
\r
110 Pick.Position pp = new Pick.Position();
\r
111 boolean value = p.match(source, pp);
\r
112 System.out.println("Match: " + value + ", " + pp);
\r
115 static void testParser() {
\r
117 Pick.Target target = new Pick.Target();
\r
118 for (int i = 0; i < rules.length; ++i) {
\r
119 target.addRule(rules[i]);
\r
121 } catch (ParseException e) {
\r
122 // TODO Auto-generated catch block
\r
123 e.printStackTrace();
\r
128 static class Counts {
\r
131 counts = new int[max+1];
\r
133 void inc(int index) {
\r
137 System.out.println("Printing Counts");
\r
138 for (int i = 0; i < counts.length; ++i) {
\r
139 if (counts[i] == 0) continue;
\r
140 System.out.println(i + ": " + counts[i]);
\r
142 System.out.println();
\r
146 /* static final String[] rules = {
\r
148 "$noun = dog | house | government | wall | street | zebra;",
\r
149 "$adjective = red | glorious | simple | nasty | heavy | clean;",
\r
150 "$article = quickly | oddly | silently | boldly;",
\r
151 "$adjectivePhrase = ($adverb $s)? 50% $adjective* 0% 30% 20% 10%;",
\r
152 "$nounPhrase = $articles $s ($adjectivePhrase $s)? 30% $noun;",
\r
153 "$verb = goes | fishes | walks | sleeps;",
\r
154 "$tverb = carries | lifts | overturns | hits | jumps on;",
\r
155 "$copula = is 30% | seems 10%;",
\r
156 "$sentence1 = $nounPhrase $s $verb $s ($s $adverb)? 30%;",
\r
157 "$sentence2 = $nounPhrase $s $tverb $s $nounPhrase ($s $adverb)? 30%;",
\r
158 "$sentence3 = $nounPhrase $s $copula $s $adjectivePhrase;",
\r
159 "$conj = but | and | or;",
\r
160 "$sentence4 = $sentence1 | $sentence2 | $sentence3 20% | $sentence4 $conj $sentence4 20%;",
\r
161 "$sentence = $sentence4 '.';"};
\r
164 private static void testEnglish() {
\r
165 Pick s = Pick.unquoted(" ");
\r
166 Pick verbs = Pick.or(new String[]{"goes", "fishes", "walks", "sleeps"});
\r
167 Pick transitive = Pick.or(new String[]{"carries", "lifts", "overturns", "hits", "jumps on"});
\r
168 Pick nouns = Pick.or(new String[]{"dog", "house", "government", "wall", "street", "zebra"});
\r
169 Pick adjectives = Pick.or(new String[]{"red", "glorious", "simple", "nasty", "heavy", "clean"});
\r
170 Pick articles = Pick.or(new String[]{"the", "a"});
\r
171 Pick adverbs = Pick.or(new String[]{"quickly", "oddly", "silently", "boldly"});
\r
172 Pick adjectivePhrase = Pick.and(0.5, Pick.and(adverbs).and2(s)).and2(adjectives);
\r
173 Pick nounPhrase = Pick.and(articles).and2(s)
\r
174 .and2(0.3, Pick.and(adjectivePhrase).and2(s))
\r
176 Pick copula = Pick.or(new String[]{"is", "seems"});
\r
177 Pick sentence1 = Pick.and(nounPhrase).and2(s).and2(verbs)
\r
178 .and2(0.3, Pick.and(s).and2(adverbs)).name("s1");
\r
179 Pick sentence2 = Pick.and(nounPhrase).and2(s).and2(transitive).and2(s).and2(nounPhrase)
\r
180 .and2(0.3, Pick.and(s).and2(adverbs)).name("s2");
\r
181 Pick sentence3 = Pick.and(nounPhrase).and2(s).and2(copula).and2(s).and2(adjectivePhrase).name("s3");
\r
182 Pick conj = Pick.or(new String[]{", but", ", and", ", or"});
\r
183 Pick forward = Pick.unquoted("forward");
\r
184 Pick pair = Pick.and(forward).and2(conj).and2(s).and2(forward).name("part");
\r
185 Pick sentenceBase = Pick.or(sentence1).or2(sentence2).or2(sentence3).or2(0.6666, pair).name("sentence");
\r
186 sentenceBase.replace(forward, sentenceBase);
\r
187 Pick sentence = Pick.and(sentenceBase).and2(Pick.unquoted("."));
\r
188 Pick.Target target = Pick.Target.make(sentence);
\r
189 for (int i = 0; i < 50; ++i) {
\r
190 System.out.println(i + ": " + target.next());
\r
193 private static void testOr(Pick p, int count) {
\r
194 Pick.Target target = Pick.Target.make(p);
\r
195 Counts counts = new Counts(count + 10);
\r
196 for (int i = 0; i < 1000; ++i) {
\r
197 String s = target.next();
\r
198 counts.inc(s.length());
\r
202 private static void testCodePoints(Pick p) {
\r
203 Pick.Target target = Pick.Target.make(p);
\r
204 Counts counts = new Counts(128);
\r
205 for (int i = 0; i < 10000; ++i) {
\r
206 String s = target.next();
\r
207 counts.inc(s.charAt(0));
\r
212 public static void printRandoms() {
\r
213 BNF bnf = new BNF(new Random(0), new Quoter.RuleQuoter())
\r
214 .addRules("[a-z]{2,5}").complete();
\r
215 System.out.println("Start");
\r
216 for (int i = 0; i < 100; ++i) {
\r
217 String temp = bnf.next();
\r
218 System.out.println(i + ")\t" + temp);
\r
222 public static void testTokenizer() {
\r
223 Tokenizer t = new Tokenizer();
\r
225 String[] samples = {"a'b'c d #abc\r e", "'a '123 321",
\r
226 "\\\\", "a'b", "a'", "abc def%?ghi", "%", "a", "\\ a", "a''''b"};
\r
227 for (int i = 0; i < samples.length; ++i) {
\r
228 t.setSource(samples[i]);
\r
229 System.out.println();
\r
230 System.out.println("Input: " + t.getSource());
\r
232 while (type != Tokenizer.DONE) {
\r
234 System.out.println(t.toString(type, false));
\r