1 /* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 /******************************************************************************/
30 /* Ferruccio Guidi <fguidi@cs.unibo.it> */
34 /******************************************************************************/
38 let module M = MathQL in
39 let rec join l1 l2 = match l1, l2 with
42 | s1 :: tl1, s2 :: _ when s1 < s2 -> s1 :: join tl1 l2
43 | s1 :: _, s2 :: tl2 when s2 < s1 -> s2 :: join l1 tl2
44 | s1 :: tl1, s2 :: tl2 -> s1 :: join tl1 tl2
46 let rec an_val = function
49 | M.Record (rv, _) -> [rv]
50 | M.Fun (_, x) -> an_val x
51 | M.Attribute (_, _, x) -> an_val x
52 | M.RefOf x -> an_set x
53 and an_boole = function
57 | M.Not x -> an_boole x
58 | M.And (x, y) -> join (an_boole x) (an_boole y)
59 | M.Or (x, y) -> join (an_boole x) (an_boole y)
60 | M.Sub (x, y) -> join (an_val x) (an_val y)
61 | M.Meet (x, y) -> join (an_val x) (an_val y)
62 | M.Eq (x, y) -> join (an_val x) (an_val y)
66 | M.Relation (_, _, x, _) -> an_set x
67 | M.Pattern x -> an_val x
69 | M.Union (x, y) -> join (an_set x) (an_set y)
70 | M.Intersect (x, y) -> join (an_set x) (an_set y)
71 | M.Diff (x, y) -> join (an_set x) (an_set y)
72 | M.LetSVar (_, x, y) -> join (an_set x) (an_set y)
73 | M.LetVVar (_, x, y) -> join (an_val x) (an_set y)
74 | M.Select (_, x, y) -> join (an_set x) (an_boole y)
78 %token <string> ID STR
79 %token SL IS LC RC CM SC LP RP AT PC DL FS DQ EOF
80 %token AND ATTR ATTRIB BE DIFF EQ EX FALSE FUN IN INTER LET MEET NOT OR
81 %token PAT REF REFOF REL SELECT SUB SUPER TRUE UNION WHERE
82 %left DIFF WHERE REFOF
86 %nonassoc NOT EX IN ATTR
88 %start qstr query result
90 %type <MathQL.query> query
91 %type <MathQL.result> result
95 | STR qstr { $1 ^ $2 }
107 | STR CM qstr_list { $1 :: $3 }
111 | vvar CM vvar_list { $1 :: $3 }
115 | STR SL qstr_path { $1 :: $3 }
119 | SUB { MathQL.SubOp }
120 | SUPER { MathQL.SuperOp }
124 | STR { MathQL.Const [$1] }
125 | FUN STR val_exp { MathQL.Fun ($2, $3) }
126 | ATTRIB ref_op qstr_path val_exp { MathQL.Attribute ($2, $3, $4) }
127 | rvar FS vvar { MathQL.Record ($1, $3) }
128 | vvar { MathQL.VVar $1 }
129 | LC qstr_list RC { MathQL.Const $2 }
130 | LC RC { MathQL.Const [] }
131 | REFOF set_exp { MathQL.RefOf $2 }
132 | LP val_exp RP { $2 }
135 | TRUE { MathQL.True }
136 | FALSE { MathQL.False }
137 | LP boole_exp RP { $2 }
138 | NOT boole_exp { MathQL.Not $2 }
139 | EX boole_exp { MathQL.Ex (analyze $2) $2 }
140 | val_exp SUB val_exp { MathQL.Sub ($1, $3) }
141 | val_exp MEET val_exp { MathQL.Meet ($1, $3) }
142 | val_exp EQ val_exp { MathQL.Eq ($1, $3) }
143 | boole_exp AND boole_exp { MathQL.And ($1, $3) }
144 | boole_exp OR boole_exp { MathQL.Or ($1, $3) }
147 | REF val_exp { MathQL.Ref $2 }
148 | PAT val_exp { MathQL.Pattern $2 }
149 | LP set_exp RP { $2 }
150 | SELECT rvar IN set_exp WHERE boole_exp { MathQL.Select ($2, $4, $6) }
151 | REL ref_op qstr_path set_exp ATTR vvar_list { MathQL.Relation ($2, $3, $4, $6) }
152 | REL ref_op qstr_path set_exp { MathQL.Relation ($2, $3, $4, []) }
153 | svar { MathQL.SVar $1 }
154 | rvar { MathQL.RVar $1 }
155 | set_exp UNION set_exp { MathQL.Union ($1, $3) }
156 | set_exp INTER set_exp { MathQL.Intersect ($1, $3) }
157 | set_exp DIFF set_exp { MathQL.Diff ($1, $3) }
158 | LET svar BE set_exp IN set_exp { MathQL.LetSVar ($2, $4, $6) }
159 | LET vvar BE val_exp IN set_exp { MathQL.LetVVar ($2, $4, $6) }
165 | STR IS qstr_list { ($1, $3) }
169 | attribute SC attr_list { $1 :: $3 }
173 LC attr_list RC { $2 }
176 | group CM group_list { $1 :: $3 }
180 | STR ATTR group_list { ($1, $3) }
184 | resource SC resource_list { $1 :: $3 }
189 | resource_list EOF { $1 }