]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mQueryTParser.mly
4945edfc0531bcf3ee16acaea184579688a0f0bb
[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.fun_arity p (List.length pl) (List.length xl); 
35       M.Fun p pl xl
36
37    let make_gen p xl = 
38       L.gen_arity p (List.length xl); 
39       M.Gen p xl
40
41    let analyze x =
42       let rec join l1 l2 = match l1, l2 with
43          | [], _                           -> l2
44          | _, []                           -> l1
45          | s1 :: tl1, s2 :: _ when s1 < s2 -> s1 :: join tl1 l2
46          | s1 :: _, s2 :: tl2 when s2 < s1 -> s2 :: join l1 tl2
47          | s1 :: tl1, s2 :: tl2            -> s1 :: join tl1 tl2 
48       in
49       let rec iter f = function
50          | []  -> []
51          | head :: tail -> join (f head) (iter f tail)
52       in
53       let rec an_set = function
54          | M.Const _
55          | M.SVar _
56          | M.AVar _
57          | M.Ex _                       -> []
58          | M.Dot rv _                   -> [rv]
59          | M.Let _ x y
60          | M.Select _ x y
61          | M.For _ _ x y                -> iter an_set [x; y]
62          | M.While _ x y                -> iter an_set [x; y]
63          | M.Fun _ _ l                  -> iter an_set l
64          | M.Gen _ l                    -> iter an_set l
65          | M.Add _ g x                  -> join (an_grp g) (an_set x)
66          | M.Property _ _ _ _ c d _ _ x -> 
67             join (an_set x) (iter an_con [c; List.concat d])
68       and fc (_, _, v) = an_set v 
69       and an_con c = iter fc c
70       and fg (_, v) = an_set v
71       and an_grp = function
72          | M.Attr g -> iter (iter fg) g
73          | M.From _ -> [] 
74       in
75       an_set x
76       
77    let f (x, y, z) = x
78    let s (x, y, z) = y
79    let t (x, y, z) = z
80 %}
81    %token    <string> SVAR AVAR STR
82    %token    LB RB SL LC RC CM SC LP RP FS DQ EOF 
83    %token    ADD ALIGN AND AS ATTR BE BUT COUNT DIFF DISTR ELSE EMPTY EQ EX  
84    %token    FALSE FOR FROM GEN IF IN INF INTER INV ISF IST KEEP LE LET LOG LT
85    %token    MAIN MATCH MEET NOT OF OR PAT PEEK PROJ PROP READ RENDER SELECT 
86    %token    SEQ SOURCE STAT SUB SUP SUPER THEN TRUE UNION WHERE WHILE XOR
87
88    %nonassoc SOURCE
89    %right    IN SEQ
90    %nonassoc SUP INF ELSE LOG STAT KEEP RENDER PEEK READ
91    %left     DIFF   
92    %left     UNION
93    %left     INTER
94    %nonassoc WHERE EX
95    %left     XOR OR
96    %left     AND
97    %nonassoc NOT 
98    %nonassoc SUB MEET EQ LT LE
99    %nonassoc OF PROJ COUNT ALIGN
100    
101    %start    qstr query result
102    %type     <string>        qstr      
103    %type     <MathQL.query>  query
104    %type     <MathQL.result> result 
105 %%
106    qstr:
107       | DQ       { ""      }
108       | STR qstr { $1 ^ $2 }
109    ;
110    svar:
111       | SVAR { $1 }
112    ;
113    avar:
114       | AVAR { $1 }
115    ;
116    strs:
117       | STR CM strs { $1 :: $3 }
118       | STR         { [$1]     } 
119    ;
120    subpath:
121       | STR SL subpath { $1 :: $3 }
122       | STR            { [$1]     } 
123    ;
124    path:
125       | SL subpath { $2 }
126 /*    | subpath    { $1 }
127 */    | SL         { [] }
128    ;   
129    ppaths:
130       | path CM ppaths { $1 :: $3 }
131       | path           { [$1]     }
132    ;
133    paths:
134       | ppaths { $1 }
135       |        { [] }
136    ;
137    inv:
138       | INV { true  }
139       |     { false }
140    ;
141    ref:
142       | SUB   { M.RefineSub   }
143       | SUPER { M.RefineSuper }
144       |       { M.RefineExact }
145    ;
146    qualif:
147       | inv ref path { $1, $2, $3 } 
148    ;
149    cons:
150       | path IN set_exp    { (false, $1, $3) }
151       | path MATCH set_exp { (true, $1, $3)  }
152    ;
153    conss:
154       | cons CM conss { $1 :: $3 }
155       | cons          { [$1]     }
156    ;
157    istrue:
158       | IST conss { $2 }
159       |           { [] }
160    ;
161    isfalse:
162       |                   { []       }
163       | ISF conss isfalse { $2 :: $3 }
164    ;
165    mainc: 
166       | MAIN path { $2 }
167       |           { [] }
168    ;
169    exp:
170       | path AS path { $1, Some $3 }
171       | path         { $1, None    }
172    ;
173    exps:
174       | exp CM exps { $1 :: $3 }
175       | exp         { [$1]     }
176    ;   
177    attrc:
178       | ATTR exps { $2 }
179       |           { [] }
180    ;
181    pattern:
182       | PAT { true  }
183       |     { false }
184    ;
185    ass:
186       | set_exp AS path { ($3, $1) }
187    ;
188    asss:
189       | ass CM asss { $1 :: $3 }
190       | ass         { [$1]     }
191    ;
192    assg:
193       | asss SC assg { $1 :: $3 }
194       | asss         { [$1]     }
195    ;      
196    distr:
197       | DISTR { true  }
198       |       { false }
199    ;
200    allbut:
201       | BUT { "allbut" }
202       |     { "these"  }
203    ;   
204    gen_op:
205       | SUP set_exp { M.GenFJoin, $2 }
206       | INF set_exp { M.GenFMeet, $2 }
207    ;   
208    source:
209       | SOURCE { "source" }
210       |        { "result" }
211    ;
212    xml:
213       | { "text" }
214    ;
215    grp_exp:
216       | assg { M.Attr $1 }
217       | avar { M.From $1 }
218    ;
219    set_exp:
220       | STAT set_exp                  { make_fun ["stat"] [] [$2] }
221       | RENDER set_exp                { make_fun ["render"] [] [$2] }
222       | READ set_exp                  { make_fun ["read"] [] [$2] }
223       | FALSE                         { make_fun ["false"] [] [] }
224       | TRUE                          { make_fun ["true"] [] [] }
225       | LC sets RC                    { make_fun ["union"] [] $2 }
226       | NOT set_exp                   { make_fun ["not"] [] [$2] }
227       | PROJ path OF set_exp          { make_fun ["proj"] [$2] [$4] }
228       | COUNT set_exp                 { make_fun ["count"] [] [$2] }
229       | ALIGN set_exp IN set_exp      { make_fun ["align"] [] [$2; $4] }
230       | EMPTY                         { make_fun ["empty"] [] [] }
231       | LOG xml source set_exp        { make_fun ["log"; $2; $3] [] [$4] }
232       | KEEP allbut ppaths IN set_exp { make_fun ["keep"; $2] $3 [$5] } 
233       | KEEP allbut set_exp           { make_fun ["keep"; $2] [] [$3] } 
234       | path LC paths RC LC sets RC   { make_fun $1 $3 $6 }
235       | set_exp DIFF set_exp          { make_fun ["diff"] [] [$1; $3] }
236       | set_exp UNION set_exp         { make_fun ["union"] [] [$1; $3] }
237       | set_exp INTER set_exp         { make_fun ["intersect"] [] [$1; $3] }
238       | set_exp XOR set_exp           { make_fun ["xor"] [] [$1; $3] }
239       | set_exp OR set_exp            { make_fun ["or"] [] [$1; $3] }
240       | set_exp AND set_exp           { make_fun ["and"] [] [$1; $3] }
241       | set_exp SUB set_exp           { make_fun ["sub"] [] [$1; $3] }
242       | set_exp MEET set_exp          { make_fun ["meet"] [] [$1; $3] }
243       | set_exp EQ set_exp            { make_fun ["eq"] [] [$1; $3] }
244       | set_exp LE set_exp            { make_fun ["le"] [] [$1; $3] }
245       | set_exp LT set_exp            { make_fun ["lt"] [] [$1; $3] }
246       | PEEK set_exp                  { make_fun ["peek"] [] [$2] }
247       | IF set_exp THEN set_exp ELSE set_exp 
248          { make_fun ["if"] [] [$2; $4; $6] }
249       | STR                            { M.Const [$1, []] }
250       | LB resources RB                { M.Const $2 }
251       | avar FS path                   { M.Dot $1 $3 }
252       | LP set_exp RP                  { $2 }
253       | EX set_exp                     { M.Ex (analyze $2) $2 }
254       | svar                           { M.SVar $1 }
255       | avar                           { M.AVar $1 }
256       | LET svar BE set_exp IN set_exp { M.Let (Some $2) $4 $6 }
257       | set_exp SEQ set_exp            { M.Let None $1 $3 }
258       | FOR avar IN set_exp gen_op     { M.For (fst $5) $2 $4 (snd $5) }
259       | WHILE set_exp gen_op           { M.While (fst $3) $2 (snd $3) }
260       | ADD distr grp_exp IN set_exp   { M.Add $2 $3 $5 }
261       | PROP qualif mainc istrue isfalse attrc OF pattern set_exp     
262          { M.Property (f $2) (s $2) (t $2) $3 $4 $5 $6 $8 $9 }
263       | SELECT avar FROM set_exp WHERE set_exp { M.Select $2 $4 $6 }
264       | GEN path LC sets RC { make_gen $2 $4 }
265       | GEN path IN set_exp { make_gen $2 [$4] }
266    ;   
267    psets:
268       | set_exp CM psets { $1 :: $3 }
269       | set_exp          { [$1]     }
270    ;
271    sets:
272       | psets { $1 }
273       |       { [] }
274    ;
275    query:
276       | set_exp       { $1                }
277       | set_exp error { $1                }
278       | EOF           { raise End_of_file }
279    ;
280    attr:
281       | path BE strs { $1, $3 }
282       | path         { $1, [] }
283    ;
284    attrs:
285       | attr SC attrs { $1 :: $3 }
286       | attr          { [$1]     }
287    ;
288    group:
289       LC attrs RC { $2 }
290    ;
291    groups:
292       | group CM groups { $1 :: $3 }
293       | group           { [$1]     }
294    ;
295    resource:
296       | STR ATTR groups { ($1, $3) }
297       | STR             { ($1, []) }
298    ;
299    resources:
300       | resource SC resources { $1 :: $3 }
301       | resource              { [$1]     }
302       |                       { []       }
303    ;   
304    result:
305       | resources       { $1                }
306       | resources error { $1                }
307       | EOF             { raise End_of_file }