summaryrefslogtreecommitdiff
path: root/src/GF/OldParsing/MCFGrammar.hs
blob: ff9d7de1b503d327c2c9d36690a531b0ad14765f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
----------------------------------------------------------------------
-- |
-- Module      : MCFGrammar
-- Maintainer  : PL
-- Stability   : (stable)
-- Portability : (portable)
--
-- > CVS $Date: 2005/04/21 16:22:48 $ 
-- > CVS $Author: bringert $
-- > CVS $Revision: 1.2 $
--
-- Definitions of multiple context-free grammars,
-- parser information and chart conversion
-----------------------------------------------------------------------------

module GF.OldParsing.MCFGrammar
    (-- * Type definitions
     Grammar,
     Rule(..),
     Lin(..),
     -- * Parser information
     MCFParser,
     MEdge,
     edges2chart,
     PInfo,
     pInfo,
     -- * Ranges
     Range(..),
     makeRange,
     concatRange,
     unifyRange,
     unionRange,
     failRange,
     -- * Utilities
     select,
     updateIndex
    ) where

-- gf modules:
import GF.Data.SortedList
import GF.Data.Assoc
-- parser modules:
import GF.OldParsing.Utilities
import GF.Printing.PrintParser



select :: [a] -> [(a, [a])]
select [] = []
select (x:xs) = (x,xs) : [ (y,x:ys) | (y,ys) <- select xs ]

updateIndex :: Functor f => Int -> [a] -> (a -> f a) -> f [a]
updateIndex 0 (a:as) f = fmap (:as) $ f a
updateIndex n (a:as) f = fmap (a:)  $ updateIndex (n-1) as f
updateIndex _ _ _ = error "ParserUtils.updateIndex: Index out of range"


------------------------------------------------------------
-- grammar types

type Grammar n c l t = [Rule n c l t]
data Rule    n c l t = Rule c [c] [Lin c l t] n
		       deriving (Eq, Ord, Show)
data Lin       c l t = Lin l [Symbol (c, l, Int) t]
		       deriving (Eq, Ord, Show)

-- variants is simply several linearizations with the same label


------------------------------------------------------------
-- parser information

type PInfo n c l t = Grammar n c l t

pInfo :: Grammar n c l t -> PInfo n c l t
pInfo = id

type MCFParser n c l t = PInfo n c l t -> [c] -> Input t -> ParseChart n (MEdge c l)

type MEdge c l = (c, [(l, Range)])

edges2chart :: (Ord n, Ord c, Ord l) =>
	       [(n, MEdge c l, [MEdge c l])] -> ParseChart n (MEdge c l)
edges2chart edges = fmap groupPairs $ accumAssoc id $
		    [ (medge, (name, medges)) | (name, medge, medges) <- edges ]


------------------------------------------------------------
-- ranges as sets of int-pairs

newtype Range = Rng (SList (Int, Int)) deriving (Eq, Ord, Show)

makeRange :: SList (Int, Int) -> Range
makeRange rho = Rng rho

concatRange :: Range -> Range -> Range
concatRange (Rng rho) (Rng rho') = Rng $ nubsort [ (i,k) | (i,j) <- rho, (j',k) <- rho', j==j' ]

unifyRange :: Range -> Range -> Range
unifyRange  (Rng rho) (Rng rho') = Rng $ rho <**> rho'

unionRange :: Range -> Range -> Range
unionRange  (Rng rho) (Rng rho') = Rng $ rho <++> rho'

failRange :: Range
failRange = Rng []


------------------------------------------------------------
-- pretty-printing

instance (Print n, Print c, Print l, Print t) => Print (Rule n c l t) where
    prt (Rule cat args record name) 
	= prt name ++ ". " ++ prt cat ++ " -> " ++ prtSep " " args ++ "\n" ++ prt record
    prtList = concatMap prt

instance (Print c, Print l, Print t) => Print (Lin c l t) where
    prt (Lin lbl lin) = prt lbl ++ " = " ++ prtSep " " (map (symbol prArg (show.prt)) lin)
	where prArg (cat, lbl, arg) = prt cat ++ "@" ++ prt arg ++ "." ++ prt lbl
    prtList = prtBeforeAfter "\t" "\n" 

instance Print Range where
    prt (Rng rho) = "(" ++ prtSep "|" [ show i ++ "-" ++ show j | (i,j) <- rho ] ++ ")"

{-
------------------------------------------------------------
-- items & forests

data Item    n c l   = Item n (MEdge c l) [[MEdge c l]]
		       deriving (Eq, Ord, Show)
type MEdge     c l   = (c, [Edge l])

items2forests :: (Ord n, Ord c, Ord l) => Edge ((c, l) -> Bool) -> [Item n c l] -> [ParseForest n]

----------

items2forests (Edge i0 k0 startCat) items 
    = concatMap edge2forests $ filter checkEdge $ aElems chart
    where edge2forests (cat, []) = [FMeta]
	  edge2forests edge = filter checkForest $ map item2forest (chart ? edge)

	  item2forest (Item name _ children) = FNode name [ forests | edges <- children,
							    forests <- mapM edge2forests edges ]

	  checkEdge (cat, [Edge i k lbl]) = i == i0 && k == k0 && startCat (cat, lbl)
	  checkEdge _ = False

          checkForest (FNode _ children) = not (null children)

          chart = accumAssoc id [ (edge, item) | item@(Item _ edge _) <- items ]
-}


------------------------------------------------------------
-- grammar checking
{-
--checkGrammar :: (Ord c, Ord l, Print n, Print c, Print l, Print t) => Grammar n c l t -> [String]

checkGrammar rules
    = do rule@(Rule cat rhs record name) <- rules
	 if null record 
            then [ "empty linearization record in rule: " ++ prt rule ]
	    else [ "category does not exist: " ++ prt rcat ++ "\n" ++
		   "  - in rule: " ++ prt rule |
		   rcat <- rhs, rcat `notElem` lhsCats ] ++
 	         do Lin _ lin <- record  
		    Cat (arg, albl) <- lin
		    if arg<0 || arg>=length rhs 
		       then [ "argument index out of range: " ++ show arg ++ "/" ++ prt albl ++ "\n" ++
			      "  - in rule: " ++ prt rule ]
		       else [ "label does not exist: " ++ prt albl ++ "\n" ++
			      "  - from rule: " ++ prt rule ++
			      "  - in rule: " ++ prt arule |
			      arule@(Rule _ acat _ arecord) <- rules,
			      acat == rhs !! arg,
			      albl `notElem` [ lbl | Lin lbl _ <- arecord ] ]
    where lhsCats = nubsort [ cat | Rule _ cat _ _ <- rules ]
-}





{-----
------------------------------------------------------------
-- simplifications 

splitMRule :: (Ord n, Ord c, Ord l, Ord t) => Grammar n c l t -> Rule n c l t -> [Rule n c l t]
splitMRule rules (Rule name cat args record) = nubsort [ (Rule name cat args splitrec) |
							 (cat', lbls) <- rhsCats, cat == cat',
							 let splitrec = [ lin | lin@(Lin lbl _) <- record, lbl `elem` lbls ] ]
    where rhsCats = limit rhsC lhsCats
	  lhsCats = nubsort [ (cat, [lbl]) | Rule _ cat _ record <- rules, Lin lbl _ <- record ]
	  rhsC (cat, lbls) = nubsort [ (rcat, rlbls) |
				       Rule _ cat' rhs lins <- rules, cat == cat',
				       (arg, rcat) <- zip [0..] rhs,
				       let rlbls = nubsort [ rlbl | Lin lbl lin <- lins, lbl `elem` lbls,
							     Cat (arg', rlbl) <- lin, arg == arg' ],
				       not $ null rlbls
				     ]


----}