2aba4b1e62e6865b2722b7cd527feb2f56829b22
[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     s  ::= S
201     w  ::= " "
202     L  ::= id
203     S  ::= L "=" Q  => "assign"
204          | Q
205     Q  ::= id
206          | L "=" Q       => "assign"
207          | Q "-" Q       => "minus"
208          | Q "+" Q       => "plus"
209          | Q "*" Q       => "times"
210          | "(" Q ")"
211     id   ::= idl++       => "stringify"
212     idl  ::= [a-d]
213 }
214
215 testcase {
216     input "a*b*c";
217     output "times:{stringify:{{a}} times:{stringify:{{b}} stringify:{{c}}}}";
218     s  ::= S
219     w  ::= " "
220     L  ::= id
221     S  ::= L "=" R  => "assign"
222          | R
223     R  ::= L
224          | L "=" R       => "assign"
225          | R "+" R       => "plus"
226          | (R) "*" R       => "times"
227          | "(" R ")"
228          | R R           => "times"
229     id   ::= idl++       => "stringify"
230     idl  ::= [a-d]
231 }
232
233 testcase {
234     input "a+b*c";
235     output "plus:{stringify:{{a}} times:{stringify:{{b}} stringify:{{c}}}}";
236     s  ::= S
237     w  ::= " "
238     L  ::= id
239     S  ::= L "=" R  => "assign"
240          | R
241     R  ::= L
242          | L "=" R       => "assign"
243          | R "+" R       => "plus"
244          > R "*" R       => "times"
245          | "(" R ")"
246          | R R           => "times"
247     id   ::= idl++       => "stringify"
248     idl  ::= [a-d]
249 }
250
251 testcase {
252
253     input "
254
255
256  while x>0
257    while y>0
258     foo()
259      bar()
260
261  while x>0
262    while y>0
263     foo()
264    bar()
265
266
267 ";
268     output "smt:{while:{>:{{x} {0}} while:{>:{{y} {0}} sbb:{{f o o} {b a r}}}}}";
269     output "smt:{while:{>:{{x} {0}} sbb:{while:{>:{{y} {0}} {f o o}} {b a r}}}}";
270
271 indent  !::= ww
272 outdent !::= " "  outdent " "
273            | " "  [~]*    "\n"
274
275 any      !::= [~]*
276 s         ::= !any "\n\n" !ww Statement !ww "\n\n" !any => smt
277 ww        ::= sp*
278 sp        ::= " "
279
280 block     ::= "\n" !indent  BlockBody
281            &~ "\n" outdent [~\ ] [~]*
282
283 BlockBody ::= Statement
284             > Statement BlockBody => "sbb"
285
286 Statement ::= Call
287             | ^"while" Expr block
288
289 Expr      ::= ident
290             | Call
291             | Expr ^">" Expr
292             | num
293
294 Call      ::= Expr "()"
295
296 num       ::= [0-9]++
297
298 ident     ::= [a-z]++ &~ keyword
299 keyword   ::= "if" | "then" | "else" | "while"
300
301 w         ::= " " | "\n" | "\r"
302 ws        ::= w*
303
304
305 }