]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mQueryTParser.mly
a529326b19a211ac8947d8ea0cb54164c91a378d
[helm.git] / helm / ocaml / mathql_interpreter / mQueryTParser.mly
1 /* Copyright (C) 2000, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  */
25
26 /*  AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
27  */ 
28
29 %{
30    module M = MathQL
31    module L = MQILib
32
33    let make_fun p pl xl = 
34       L.check_arity p (List.length pl) (List.length xl); 
35       M.Fun p pl xl
36
37    let analyze x =
38       let rec join l1 l2 = match l1, l2 with
39          | [], _                           -> l2
40          | _, []                           -> l1
41          | s1 :: tl1, s2 :: _ when s1 < s2 -> s1 :: join tl1 l2
42          | s1 :: _, s2 :: tl2 when s2 < s1 -> s2 :: join l1 tl2
43          | s1 :: tl1, s2 :: tl2            -> s1 :: join tl1 tl2 
44       in
45       let rec iter f = function
46          | []  -> []
47          | head :: tail -> join (f head) (iter f tail)
48       in
49       let rec an_set = function
50          | M.Const _
51          | M.SVar _
52          | M.AVar _
53          | M.Ex _                       -> []
54          | M.Dot rv _                   -> [rv]
55          | M.Let _ x y
56          | M.Select _ x y
57          | M.For _ _ x y                -> iter an_set [x; y]
58          | M.Fun _ _ l                  -> iter an_set l
59          | M.Add _ g x                  -> join (an_grp g) (an_set x)
60          | M.Property _ _ _ _ c d _ _ x -> 
61             join (an_set x) (iter an_con [c; List.concat d])
62       and fc (_, _, v) = an_set v 
63       and an_con c = iter fc c
64       and fg (_, v) = an_set v
65       and an_grp = function
66          | M.Attr g -> iter (iter fg) g
67          | M.From _ -> [] 
68       in
69       an_set x
70       
71    let f (x, y, z) = x
72    let s (x, y, z) = y
73    let t (x, y, z) = z
74 %}
75    %token    <string> SVAR AVAR STR
76    %token    LB RB SL LC RC CM SC LP RP FS DQ EOF 
77    %token    ADD ALIGN AND AS ATTR BE BUT COUNT DIFF DISTR ELSE EMPTY EQ EX  
78    %token    FALSE FOR FROM IF IN INF INTER INV ISF IST KEEP LE LET LOG LT   
79    %token    MAIN MATCH MEET NOT OF OR PAT PEEK PROJ PROP READ RENDER SELECT 
80    %token    SEQ SOURCE STAT SUB SUP SUPER THEN TRUE UNION WHERE XOR
81
82    %nonassoc SOURCE
83    %right    IN SEQ
84    %nonassoc SUP INF ELSE LOG STAT KEEP RENDER PEEK READ
85    %left     DIFF   
86    %left     UNION
87    %left     INTER
88    %nonassoc WHERE EX
89    %left     XOR OR
90    %left     AND
91    %nonassoc NOT 
92    %nonassoc SUB MEET EQ LT LE
93    %nonassoc OF PROJ COUNT ALIGN
94    
95    %start    qstr query result
96    %type     <string>        qstr      
97    %type     <MathQL.query>  query
98    %type     <MathQL.result> result 
99 %%
100    qstr:
101       | DQ       { ""      }
102       | STR qstr { $1 ^ $2 }
103    ;
104    svar:
105       | SVAR { $1 }
106    ;
107    avar:
108       | AVAR { $1 }
109    ;
110    strs:
111       | STR CM strs { $1 :: $3 }
112       | STR         { [$1]     } 
113    ;
114    subpath:
115       | STR SL subpath { $1 :: $3 }
116       | STR            { [$1]     } 
117    ;
118    path:
119       | SL subpath { $2 }
120       | SL         { [] }
121    ;   
122    ppaths:
123       | path CM ppaths { $1 :: $3 }
124       | path           { [$1]     }
125    ;
126    paths:
127       | ppaths { $1 }
128       |        { [] }
129    ;
130    inv:
131       | INV { true  }
132       |     { false }
133    ;
134    ref:
135       | SUB   { M.RefineSub   }
136       | SUPER { M.RefineSuper }
137       |       { M.RefineExact }
138    ;
139    qualif:
140       | inv ref path { $1, $2, $3 } 
141    ;
142    cons:
143       | path IN set_exp    { (false, $1, $3) }
144       | path MATCH set_exp { (true, $1, $3)  }
145    ;
146    conss:
147       | cons CM conss { $1 :: $3 }
148       | cons          { [$1]     }
149    ;
150    istrue:
151       | IST conss { $2 }
152       |           { [] }
153    ;
154    isfalse:
155       |                   { []       }
156       | ISF conss isfalse { $2 :: $3 }
157    ;
158    mainc: 
159       | MAIN path { $2 }
160       |           { [] }
161    ;
162    exp:
163       | path AS path { $1, Some $3 }
164       | path         { $1, None    }
165    ;
166    exps:
167       | exp CM exps { $1 :: $3 }
168       | exp         { [$1]     }
169    ;   
170    attrc:
171       | ATTR exps { $2 }
172       |           { [] }
173    ;
174    pattern:
175       | PAT { true  }
176       |     { false }
177    ;
178    ass:
179       | set_exp AS path { ($3, $1) }
180    ;
181    asss:
182       | ass CM asss { $1 :: $3 }
183       | ass         { [$1]     }
184    ;
185    assg:
186       | asss SC assg { $1 :: $3 }
187       | asss         { [$1]     }
188    ;      
189    distr:
190       | DISTR { true  }
191       |       { false }
192    ;
193    allbut:
194       | BUT { "allbut" }
195       |     { "these"  }
196    ;   
197    gen_op:
198       | SUP set_exp { M.GenFJoin, $2 }
199       | INF set_exp { M.GenFMeet, $2 }
200    ;   
201    source:
202       | SOURCE { "source" }
203       |        { "result" }
204    ;
205    xml:
206       | { "text" }
207    ;
208    grp_exp:
209       | assg { M.Attr $1 }
210       | avar { M.From $1 }
211    ;
212    set_exp:
213       | FALSE
214          { make_fun ["false"] [] [] }
215       | TRUE
216          { make_fun ["true"] [] [] }
217       | STR 
218          { M.Const [$1, []] }
219       | LB resources RB 
220          { M.Const $2 }
221       | avar FS path
222          { M.Dot $1 $3 }
223       | LC sets RC
224          { make_fun ["union"] [] $2 }
225       | LP set_exp RP
226          { $2 }
227       | STAT set_exp
228          { make_fun ["stat"] [] [$2] }
229       | RENDER set_exp
230          { make_fun ["render"] [] [$2] }
231       | READ set_exp
232          { make_fun ["read"] [] [$2] }
233       | EX set_exp
234          { M.Ex (analyze $2) $2 }
235       | NOT set_exp
236          { make_fun ["not"] [] [$2] }
237       | PROJ path OF set_exp
238          { make_fun ["proj"] [$2] [$4] }
239       | COUNT set_exp
240          { make_fun ["count"] [] [$2] }
241       | ALIGN set_exp IN set_exp    
242          { make_fun ["align"] [] [$2; $4] }
243       | EMPTY
244          { make_fun ["empty"] [] [] }
245       | svar
246          { M.SVar $1 }
247       | avar
248          { M.AVar $1 }
249       | LET svar BE set_exp IN set_exp
250          { M.Let $2 $4 $6 }
251       | FOR avar IN set_exp gen_op             
252          { M.For (fst $5) $2 $4 (snd $5) }
253       | ADD distr grp_exp IN set_exp
254          { M.Add $2 $3 $5 }
255       | IF set_exp THEN set_exp ELSE set_exp
256          { make_fun ["if"] [] [$2; $4; $6] }
257       | PROP qualif mainc istrue isfalse attrc OF pattern set_exp     
258          { M.Property (f $2) (s $2) (t $2) $3 $4 $5 $6 $8 $9 }
259       | LOG xml source set_exp
260          { make_fun ["log"; $2; $3] [] [$4] }
261       | KEEP allbut ppaths IN set_exp
262          { make_fun ["keep"; $2] $3 [$5] } 
263       | KEEP allbut set_exp
264          { make_fun ["keep"; $2] [] [$3] } 
265       | SELECT avar FROM set_exp WHERE set_exp
266          { M.Select $2 $4 $6 }
267       | path LC paths RC LC sets RC
268          { make_fun $1 $3 $6 }
269       | set_exp SEQ set_exp
270          { make_fun ["seq"] [] [$1; $3] }
271       | set_exp DIFF set_exp
272          { make_fun ["diff"] [] [$1; $3] }
273       | set_exp UNION set_exp
274          { make_fun ["union"] [] [$1; $3] }
275       | set_exp INTER set_exp
276          { make_fun ["intersect"] [] [$1; $3] }
277       | set_exp XOR set_exp
278          { make_fun ["xor"] [] [$1; $3] }
279       | set_exp OR set_exp
280          { make_fun ["or"] [] [$1; $3] }
281       | set_exp AND set_exp
282          { make_fun ["and"] [] [$1; $3] }
283       | set_exp SUB set_exp
284          { make_fun ["sub"] [] [$1; $3] }
285       | set_exp MEET set_exp
286          { make_fun ["meet"] [] [$1; $3] }
287       | set_exp EQ set_exp
288          { make_fun ["eq"] [] [$1; $3] }
289       | set_exp LE set_exp
290          { make_fun ["le"] [] [$1; $3] }
291       | set_exp LT set_exp
292          { make_fun ["lt"] [] [$1; $3] }
293       | PEEK set_exp
294          { make_fun ["peek"] [] [$2] }
295    ;   
296    psets:
297       | set_exp CM psets { $1 :: $3 }
298       | set_exp          { [$1]     }
299    ;
300    sets:
301       | psets { $1 }
302       |       { [] }
303    ;
304    query:
305       | set_exp       { $1                }
306       | set_exp error { $1                }
307       | EOF           { raise End_of_file }
308    ;
309    attr:
310       | path BE strs { $1, $3 }
311       | path         { $1, [] }
312    ;
313    attrs:
314       | attr SC attrs { $1 :: $3 }
315       | attr          { [$1]     }
316    ;
317    group:
318       LC attrs RC { $2 }
319    ;
320    groups:
321       | group CM groups { $1 :: $3 }
322       | group           { [$1]     }
323    ;
324    resource:
325       | STR ATTR groups { ($1, $3) }
326       | STR             { ($1, []) }
327    ;
328    resources:
329       | resource SC resources { $1 :: $3 }
330       | resource              { [$1]     }
331       |                       { []       }
332    ;   
333    result:
334       | resources       { $1                }
335       | resources error { $1                }
336       | EOF             { raise End_of_file }