summaryrefslogtreecommitdiff
path: root/src-3.0/GF/Parsing/CF.hs
diff options
context:
space:
mode:
authoraarne <aarne@cs.chalmers.se>2008-05-21 09:26:44 +0000
committeraarne <aarne@cs.chalmers.se>2008-05-21 09:26:44 +0000
commit055c0d0d5a5bb0dc75904fe53df7f2e4f5732a8f (patch)
tree0e63fb68c69c8f6ad0f78893c63420f0a3600e1c /src-3.0/GF/Parsing/CF.hs
parent915a1de71783ab8446b1af9e72c7ba7dfbc12d3f (diff)
GF/src is now for 2.9, and the new sources are in src-3.0 - keep it this way until the release of GF 3
Diffstat (limited to 'src-3.0/GF/Parsing/CF.hs')
-rw-r--r--src-3.0/GF/Parsing/CF.hs66
1 files changed, 66 insertions, 0 deletions
diff --git a/src-3.0/GF/Parsing/CF.hs b/src-3.0/GF/Parsing/CF.hs
new file mode 100644
index 000000000..1a65f6caf
--- /dev/null
+++ b/src-3.0/GF/Parsing/CF.hs
@@ -0,0 +1,66 @@
+----------------------------------------------------------------------
+-- |
+-- Maintainer : PL
+-- Stability : (stable)
+-- Portability : (portable)
+--
+-- > CVS $Date: 2005/04/21 16:23:04 $
+-- > CVS $Author: bringert $
+-- > CVS $Revision: 1.4 $
+--
+-- Chart parsing of grammars in CF format
+-----------------------------------------------------------------------------
+
+module GF.Parsing.CF (parse) where
+
+import GF.Data.Operations (errVal)
+
+import GF.System.Tracing
+import GF.Infra.Print
+
+import GF.Data.SortedList (nubsort)
+import GF.Data.Assoc
+import qualified GF.CF.CF as CF
+import qualified GF.CF.CFIdent as CFI
+import GF.Formalism.Utilities
+import GF.Formalism.CFG
+import qualified GF.Parsing.CFG as P
+
+type Token = CFI.CFTok
+type Name = CFI.CFFun
+type Category = CFI.CFCat
+
+parse :: String -> CF.CF -> Category -> CF.CFParser
+parse = buildParser . errVal (errVal undefined (P.parseCF "")) . P.parseCF
+
+buildParser :: P.CFParser Category Name Token -> CF.CF -> Category -> CF.CFParser
+buildParser parser cf start tokens = (parseResults, parseInformation)
+ where parseInformation = prtSep "\n" trees
+ parseResults = [ (tree2cfTree t, []) | t <- trees ]
+ theInput = input tokens
+ edges = tracePrt "Parsing.CF - nr. edges" (prt.length) $
+ parser pInf [start] theInput
+ chart = tracePrt "Parsing.CF - sz. chart" (prt . map (length.snd) . aAssocs) $
+ grammar2chart $ map addCategory edges
+ forests = tracePrt "Parsing.CF - nr. forests" (prt.length) $
+ chart2forests chart (const False)
+ [ uncurry Edge (inputBounds theInput) start ]
+ trees = tracePrt "Parsing.CF - nr. trees" (prt.length) $
+ concatMap forest2trees forests
+ pInf = P.buildCFPInfo $ cf2grammar cf (nubsort tokens)
+
+
+addCategory (CFRule cat rhs name) = CFRule cat rhs (name, cat)
+
+tree2cfTree (TNode (name, Edge _ _ cat) trees) = CF.CFTree (name, (cat, map tree2cfTree trees))
+
+cf2grammar :: CF.CF -> [Token] -> CFGrammar Category Name Token
+cf2grammar cf tokens = [ CFRule cat rhs name |
+ (name, (cat, rhs0)) <- cfRules,
+ rhs <- mapM item2symbol rhs0 ]
+ where cfRules = concatMap (CF.predefRules (CF.predefOfCF cf)) tokens ++
+ CF.rulesOfCF cf
+ item2symbol (CF.CFNonterm cat) = [Cat cat]
+ item2symbol item = map Tok $ filter (CF.matchCFTerm item) tokens
+
+