checkpoint
[sbp.git] / tests / regression.tc
1 //testcase {
2 //    input  "x";
3 //    output "a1:{x}";
4 //
5 //    s ::= a        => a1
6 //    a ::= s        => s1
7 //    a ::= ^"x"
8 //}
9 //
10 //testcase {
11 //    input  "x";
12 //    output "x";
13 //    output "s2:{s2:{s0 s0} x}";
14 //    output "s2:{s0 x}";
15 //
16 //
17 //    s ::= s s      => s2
18 //    s ::= ^"x"
19 //    s ::= ()       => s0
20 //}
21
22 testcase {
23     input "ab c";
24     output "1:{{a b} {c}}";
25
26     s   ::= ids
27     ids ::= id (" " ids &~ id [~]*) => "1"
28           | id (    ids &~ id [~]*) => "2"
29           | id
30     id  ::= [a-z]++
31 }
32
33 testcase {
34     input "ab c";
35
36     output "2:{{a} 1:{{b} {c}}}";
37     output "1:{{a b} {c}}";
38
39     s   ::= ids
40     ids ::= id " " ids => "1"
41           | id     ids => "2"
42           | id
43     id  ::= [a-z]+
44 }
45
46 testcase {
47     input "aaabbbccc";
48     output "";
49
50     s   ::= ab & dc
51     ab  ::= a b
52     dc  ::= d c
53     a   ::= "a" a     | ()
54     b   ::= "b" b "c" | ()
55     c   ::= "c" c     | ()
56     d   ::= "a" d "b" | ()
57 }
58
59 testcase {
60     input "aaabbbbccc";
61
62     s   ::= ab & dc
63     ab !::= a b
64     dc !::= d c
65     a   ::= "a" a     | ()
66     b   ::= "b" b "c" | ()
67     c   ::= "c" c     | ()
68     d   ::= "a" d "b" | ()
69 }
70
71 testcase {
72     input "12111211";
73     output "ac:{{2 1 2 1}}";
74     //output "a:{{2 1 2 1}}";
75     //output "c:{{c:{1 1} c:{1 1}}}";
76
77     s ::= ab => "ab"
78         | ac => "ac"
79         | bc => "bc"
80         //| a  =>  "a"
81         //| b  =>  "b"
82         //| c  =>  "c"
83     ab ::= a & b
84     ac ::= a & c
85     bc ::= b & c
86     a ::= ("1" x)*
87     b ::= (x "2" => "b")*
88     c ::= (x "2" x "1" => "c")*
89     x ::= [123]
90 }
91
92 testcase {
93     input  "xbambambam";
94     output "bam:{a bam:{a bam:{a x}}}";
95
96     s ::= a s ^"bam"
97     s ::= ^"x"
98     a ::= ()       => "a"
99 }
100
101 testcase {
102     input  "baaaa";
103     output "s2:{b0 a:{a:{epsilon}}}";
104     output "b:{a:{a:{epsilon}} epsilon}";
105     s ::= b t        => "s2"
106         | ^"b" t b
107     t ::= ^"a" t "a"
108         | ()         => "epsilon"
109     b ::= "b"        => "b0"
110         | ()         => "epsilon"
111 }
112
113 testcase {
114     input  "qaq";
115     output "q:{a:{s1:{epsilon}}}";
116
117     s ::= ^"q" x "q"
118     x ::= ^"a" a
119     x ::= ()        => "epsilon"
120     a ::= x         => "s1"
121 }
122
123 testcase {
124     input "baa";
125     output "s1:{a2:{a2:{a0 b0} b0}}";
126
127     s ::= "b" a     => "s1"
128     a ::= "a" a b   => "a2"
129         | ()        => "a0"
130     b ::= ()        => "b0"
131 }
132
133 testcase {
134     input  "aaa";
135
136     output "s3:{s3:{epsilon a0 epsilon epsilon} epsilon epsilon epsilon}";
137     output "s3:{s3:{epsilon epsilon epsilon epsilon} a0 epsilon epsilon}";
138     output "s3:{s3:{epsilon epsilon a0 epsilon} epsilon epsilon epsilon}";
139     output "s3:{s3:{epsilon epsilon epsilon a0} epsilon epsilon epsilon}";
140     output "s3:{epsilon epsilon a0 a0}";
141     output "s3:{s3:{s3:{epsilon epsilon epsilon epsilon} epsilon epsilon epsilon} epsilon epsilon epsilon}";
142     output "s3:{s3:{epsilon epsilon epsilon epsilon} epsilon epsilon a0}";
143     output "s3:{s3:{epsilon epsilon epsilon epsilon} epsilon a0 epsilon}";
144     output "s3:{epsilon a0 epsilon a0}";
145     output "s3:{epsilon a0 a0 epsilon}";
146
147     s ::= "a" s a a a => "s3"
148         | ()          => "epsilon"
149     a ::= "a"         => "a0"
150         | ()          => "epsilon"
151 }
152
153 testcase {
154     input "aa";
155     output "poo:{poo:{poox poox} poox}";
156     output "poo:{poox poo:{poox poox}}";
157     s ::= s s "a"  => "poo"
158         | ()       => "poox"
159 }
160
161 testcase {
162     input "baa";
163     output "s:{aa:{aa:{a b} b}}";
164     s ::= "b" a      => "s"
165     a ::= "a" a b    => "aa"
166     a ::= ()         => "a"
167     b ::= ()         => "b"
168 }
169
170 testcase {
171     input "aaab";
172     output "sx:{b aa:{aa:{b b} b}}";
173     s ::= b d "a" "b"  => "sx"
174     s ::= "a" d "a" d  => "sy"
175     d ::= "a" a b      => "aa"
176     a ::= "a" b b      => "aa"
177     a ::= ()           => "a"
178     b ::= ()           => "b"
179 }
180
181 testcase {
182     input "a+(b*c)";
183     output "+:{{a} *:{{b} {c}}}";
184
185     s   ::= r
186     r   ::= id
187           | r ^"*" r
188           | r ^"+" r
189           | "(" r ")"
190     id  ::= [a-z]++
191 }
192
193 testcase {
194     input "a+b-d*c";
195     output "plus:{stringify:{{a}} minus:{stringify:{{b}} times:{stringify:{{d}} stringify:{{c}}}}}";
196     output "times:{plus:{stringify:{{a}} minus:{stringify:{{b}} stringify:{{d}}}} stringify:{{c}}}";
197     output "plus:{stringify:{{a}} times:{minus:{stringify:{{b}} stringify:{{d}}} stringify:{{c}}}}";
198     output "times:{minus:{plus:{stringify:{{a}} stringify:{{b}}} stringify:{{d}}} stringify:{{c}}}";
199     output "minus:{plus:{stringify:{{a}} stringify:{{b}}} times:{stringify:{{d}} stringify:{{c}}}}";
200     w  ::= " "
201     l  ::= id
202     s  ::= l "=" q  => "assign"
203          | q
204     q  ::= id
205          | l "=" q       => "assign"
206          | q "-" q       => "minus"
207          | q "+" q       => "plus"
208          | q "*" q       => "times"
209          | "(" q ")"
210     id   ::= idl++       => "stringify"
211     idl  ::= [a-d]
212 }
213
214 testcase {
215     input "a*b*c";
216     output "times:{stringify:{{a}} times:{stringify:{{b}} stringify:{{c}}}}";
217     w  ::= " "
218     l  ::= id
219     s  ::= l "=" r  => "assign"
220          | r
221     r  ::= l
222          | l "=" r       => "assign"
223          | r "+" r       => "plus"
224          | (r) "*" r       => "times"
225          | "(" r ")"
226          | r r           => "times"
227     id   ::= idl++       => "stringify"
228     idl  ::= [a-d]
229 }
230
231 testcase {
232     input "a+b*c";
233     output "plus:{stringify:{{a}} times:{stringify:{{b}} stringify:{{c}}}}";
234     w  ::= " "
235     l  ::= id
236     s  ::= l "=" r  => "assign"
237          | r
238     r  ::= l
239          | l "=" r       => "assign"
240          | r "+" r       => "plus"
241          > r "*" r       => "times"
242          | "(" r ")"
243          | r r           => "times"
244     id   ::= idl++       => "stringify"
245     idl  ::= [a-d]
246 }
247
248 testcase {
249
250     input "
251
252
253  while x>0
254    while y>0
255     foo()
256      bar()
257
258  while x>0
259    while y>0
260     foo()
261    bar()
262
263
264 ";
265     output "smt:{while:{>:{{x} {0}} while:{>:{{y} {0}} sbb:{{f o o} {b a r}}}}}";
266     output "smt:{while:{>:{{x} {0}} sbb:{while:{>:{{y} {0}} {f o o}} {b a r}}}}";
267
268 indent  !::= ww
269 outdent !::= " "  outdent " "
270            | " "  ([~]*)  "\n"
271
272 any      !::= [~]*
273 s         ::= !any "\n\n" !ww statement !ww "\n\n" !any => smt
274 ww        ::= sp*
275 ws       !::= sp**
276 sp        ::= " "
277
278 block     ::= "\n" !indent  blockBody
279            &~ "\n" outdent [~\ ] [~]*
280
281 blockBody ::= statement
282             > statement blockBody /ws => "sbb"
283
284 statement ::= call
285             | ^"while" expr block /ws
286
287 expr      ::= ident
288             | call
289             | expr ^">" expr   /ws
290             | num
291
292 call      ::= expr "()"        /ws
293
294 num       ::= [0-9]++
295
296 ident     ::= [a-z]++ &~ keyword
297 keyword   ::= "if" | "then" | "else" | "while"
298
299 w         ::= " " | "\n" | "\r"
300 ws        ::= w*
301
302
303 }