summaryrefslogtreecommitdiff
path: root/src/PGF/Parsing/FCFG/Active.hs
blob: e88926f6e3a9009bb9f63b8dff9e784da93e51d0 (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
----------------------------------------------------------------------
-- |
-- Maintainer  : Krasimir Angelov
-- Stability   : (stable)
-- Portability : (portable)
--
-- MCFG parsing, the active algorithm
-----------------------------------------------------------------------------

module PGF.Parsing.FCFG.Active (parse) where

import GF.Data.Assoc
import GF.Data.SortedList
import GF.Data.Utilities
import qualified GF.Data.MultiMap as MM

import PGF.CId
import PGF.Data
import PGF.Tree
import PGF.Parsing.FCFG.Utilities
import PGF.BuildParser

import Control.Monad (guard)

import qualified Data.List as List
import qualified Data.Map  as Map
import qualified Data.IntMap  as IntMap
import qualified Data.Set  as Set
import Data.Array.IArray
import Debug.Trace

----------------------------------------------------------------------
-- * parsing

type FToken = String

makeFinalEdge cat 0 0 = (cat, [EmptyRange])
makeFinalEdge cat i j = (cat, [makeRange i j])

-- | the list of categories = possible starting categories
parse :: String -> ParserInfo -> Type -> [FToken] -> [Expr]
parse strategy pinfo (DTyp _ start _) toks = map (tree2expr) . nubsort $ filteredForests >>= forest2trees
  where
    inTokens = input toks
    starts = Map.findWithDefault [] start (startCats pinfo)
    schart = xchart2syntaxchart chart pinfo
    (i,j) = inputBounds inTokens
    finalEdges = [makeFinalEdge cat i j | cat <- starts]
    forests = chart2forests schart (const False) finalEdges
    filteredForests = forests >>= applyProfileToForest
        
    pinfoex = buildParserInfo pinfo
    
    chart = process strategy pinfo pinfoex inTokens axioms emptyXChart
    axioms | isBU  strategy = literals pinfoex inTokens ++ initialBU pinfo pinfoex inTokens
           | isTD  strategy = literals pinfoex inTokens ++ initialTD pinfo starts inTokens

isBU  s = s=="b"
isTD  s = s=="t"

-- used in prediction
emptyChildren :: FunId -> [FCat] -> SyntaxNode FunId RangeRec
emptyChildren ruleid args = SNode ruleid (replicate (length args) [])


process :: String -> ParserInfo -> ParserInfoEx -> Input FToken -> [Item] -> XChart FCat -> XChart FCat
process strategy pinfo pinfoex toks []           chart = chart
process strategy pinfo pinfoex toks (item:items) chart = process strategy pinfo pinfoex toks items $! univRule item chart
  where
    univRule item@(Active found rng lbl ppos node@(SNode ruleid recs) args cat) chart
      | inRange (bounds lin) ppos =
           case lin ! ppos of
             FSymCat d r -> let c = args !! d
                            in case recs !! d of
                                [] -> case insertXChart chart item c of
	                                Nothing    -> chart
	                                Just chart -> let items = do item@(Final found' _ _ _) <- lookupXChartFinal chart c
	                			                     rng  <- concatRange rng (found' !! r)
	     			                                     return (Active found rng lbl (ppos+1) (SNode ruleid (updateNth (const found') d recs)) args cat)
	     			                                  ++
	     			                                  do guard (isTD strategy)
	     			                                     (ruleid,args) <- topdownRules pinfo c
	     			                                     return (Active [] EmptyRange 0 0 (emptyChildren ruleid args) args c)
	     			                      in process strategy pinfo pinfoex toks items chart
	     			found' -> let items = do rng  <- concatRange rng (found' !! r)
	     			                         return (Active found rng lbl (ppos+1) node args cat)
	     			          in process strategy pinfo pinfoex toks items chart
	     FSymKS [tok]
	                 -> let items = do t_rng <- inputToken toks ? tok
	                                   rng' <- concatRange rng t_rng
	                                   return (Active found rng' lbl (ppos+1) node args cat)
                            in process strategy pinfo pinfoex toks items chart
      | otherwise =
           if inRange (bounds lins) (lbl+1)
             then univRule (Active          (rng:found)  EmptyRange (lbl+1) 0 node args cat) chart
             else univRule (Final  (reverse (rng:found))                      node args cat) chart
      where
        (FFun _ _ lins) = functions pinfo ! ruleid
        lin             = sequences pinfo ! (lins ! lbl)
    univRule item@(Final found' node args cat) chart =
      case insertXChart chart item cat of
        Nothing    -> chart
        Just chart -> let items = do (Active found rng l ppos node@(SNode ruleid _) args c) <- lookupXChartAct chart cat
                                     let FFun _ _ lins = functions pinfo ! ruleid
                                         FSymCat d r = (sequences pinfo ! (lins ! l)) ! ppos
                                     rng  <- concatRange rng (found' !! r)
                                     return (Active found rng l (ppos+1) (updateChildren node d found') args c)
                                  ++
    			          do guard (isBU strategy)
			             (ruleid,args,c) <- leftcornerCats pinfoex ? cat
			             let FFun _ _ lins = functions pinfo ! ruleid
			                 FSymCat d r = (sequences pinfo ! (lins ! 0)) ! 0
                                     return (Active [] (found' !! r) 0 1 (updateChildren (emptyChildren ruleid args) d found') args c)

                          updateChildren :: SyntaxNode FunId RangeRec -> Int -> RangeRec -> SyntaxNode FunId RangeRec
                          updateChildren (SNode ruleid recs) i rec = SNode ruleid $! updateNth (const rec) i recs
                      in process strategy pinfo pinfoex toks items chart

----------------------------------------------------------------------
-- * XChart

data Item
  = Active RangeRec
           Range
           {-# UNPACK #-} !FIndex
           {-# UNPACK #-} !FPointPos
           (SyntaxNode FunId RangeRec)
           [FCat]
           FCat
  | Final RangeRec (SyntaxNode FunId RangeRec) [FCat] FCat
  deriving (Eq, Ord, Show)

data XChart c = XChart !(MM.MultiMap c Item) !(MM.MultiMap c Item)

emptyXChart :: Ord c => XChart c
emptyXChart = XChart MM.empty MM.empty

insertXChart (XChart actives finals) item@(Active _ _ _ _ _ _ _) c = 
  case MM.insert' c item actives of
    Nothing      -> Nothing
    Just actives -> Just (XChart actives finals)

insertXChart (XChart actives finals) item@(Final _ _ _ _) c =
  case MM.insert' c item finals of
    Nothing     -> Nothing
    Just finals -> Just (XChart actives finals)

lookupXChartAct   (XChart actives finals) c = actives MM.! c
lookupXChartFinal (XChart actives finals) c = finals  MM.! c

xchart2syntaxchart :: XChart FCat -> ParserInfo -> SyntaxChart (CId,[Profile]) (FCat,RangeRec)
xchart2syntaxchart (XChart actives finals) pinfo =
  accumAssoc groupSyntaxNodes $
    [ case node of
        SNode ruleid rrecs -> let FFun fun prof _ = functions pinfo ! ruleid
		              in ((cat,found), SNode (fun,prof) (zip rhs rrecs))
        SString s          ->    ((cat,found), SString s)
        SInt    n          ->    ((cat,found), SInt    n)
        SFloat  f          ->    ((cat,found), SFloat  f)
    | (Final found node rhs cat) <- MM.elems finals
    ]

literals :: ParserInfoEx -> Input FToken -> [Item]
literals pinfoex toks =
  [let (c,node) = lexer t in (Final [rng] node [] c) | (t,rngs) <- aAssocs (inputToken toks), rng <- rngs, not (t `elem` grammarToks pinfoex)]
  where
    lexer t =
      case reads t of
        [(n,"")] -> (fcatInt, SInt (n::Integer))
        _        -> case reads t of
                      [(f,"")] -> (fcatFloat, SFloat  (f::Double))
                      _        -> (fcatString,SString t)


----------------------------------------------------------------------
-- Earley --

-- called with all starting categories
initialTD :: ParserInfo -> [FCat] -> Input FToken -> [Item]
initialTD pinfo starts toks = 
    do cat <- starts
       (ruleid,args) <- topdownRules pinfo cat
       return (Active [] (Range 0 0) 0 0 (emptyChildren ruleid args) args cat)

topdownRules pinfo cat = f cat []
  where
    f cat rules = maybe rules (Set.fold g rules) (IntMap.lookup cat (productions pinfo))

    g (FApply ruleid args) rules = (ruleid,args) : rules
    g (FCoerce cat)        rules = f cat rules


----------------------------------------------------------------------
-- Kilbury --

initialBU :: ParserInfo -> ParserInfoEx -> Input FToken -> [Item]
initialBU pinfo pinfoex toks =
    do (tok,rngs) <- aAssocs (inputToken toks)
       (ruleid,args,cat) <- leftcornerTokens pinfoex ? tok
       rng <- rngs
       return (Active [] rng 0 1 (emptyChildren ruleid args) args cat)
    ++
    do (ruleid,args,cat) <- epsilonRules pinfoex
       let FFun _ _ _ = functions pinfo ! ruleid
       return (Active [] EmptyRange 0 0 (emptyChildren ruleid args) args cat)