summaryrefslogtreecommitdiff
path: root/src/GF/Parsing/ParseCF.hs
diff options
context:
space:
mode:
authorpeb <unknown>2005-03-21 13:17:44 +0000
committerpeb <unknown>2005-03-21 13:17:44 +0000
commit96a08c9df49345657c769ac481b6df47cbea3a8d (patch)
tree2c9d6dc0603fb1fe70934af8df7b6e1336c83fa4 /src/GF/Parsing/ParseCF.hs
parentaef9430eb0576964a3fb669c741f1c689724bb5a (diff)
"Committed_by_peb"
Diffstat (limited to 'src/GF/Parsing/ParseCF.hs')
-rw-r--r--src/GF/Parsing/ParseCF.hs82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/GF/Parsing/ParseCF.hs b/src/GF/Parsing/ParseCF.hs
new file mode 100644
index 000000000..20f45e3f2
--- /dev/null
+++ b/src/GF/Parsing/ParseCF.hs
@@ -0,0 +1,82 @@
+----------------------------------------------------------------------
+-- |
+-- Module : ParseCF
+-- Maintainer : PL
+-- Stability : (stable)
+-- Portability : (portable)
+--
+-- > CVS $Date: 2005/03/21 14:17:42 $
+-- > CVS $Author: peb $
+-- > CVS $Revision: 1.1 $
+--
+-- Chart parsing of grammars in CF format
+-----------------------------------------------------------------------------
+
+module GF.Parsing.ParseCF (parse, alternatives) where
+
+import Tracing
+import GF.Printing.PrintParser
+import GF.Printing.PrintSimplifiedTerm
+
+import GF.Data.SortedList (nubsort)
+import GF.Data.Assoc
+import qualified CF
+import qualified CFIdent as CFI
+import GF.Parsing.Parser
+import GF.Conversion.CFGrammar
+import qualified GF.Parsing.ParseCFG as P
+
+type Token = CFI.CFTok
+type Name = CFI.CFFun
+type Category = CFI.CFCat
+
+alternatives :: [(String, [String])]
+alternatives = [ ("gb", ["G","GB","_gen","_genBU"]),
+ ("gt", ["GT","_genTD"]),
+ ("ibn", ["","I","B","IB","IBN","_inc","BU","_incBU"]),
+ ("ibb", ["BB","IBB","BU_BUF","_incBU_BUF"]),
+ ("ibt", ["BT","IBT","BU_TDF","_incBU_TDF"]),
+ ("iba", ["BA","IBA","BU_BTF","BU_TBF","_incBU_BTF","_incBU_TBF"]),
+ ("itn", ["T","IT","ITN","TD","_incTD"]),
+ ("itb", ["TB","ITB","TD_BUF","_incTD_BUF"])
+ ]
+
+parse :: String -> CF.CF -> Category -> CF.CFParser
+parse = buildParser . P.parse
+
+buildParser :: CFParser Name Category Token -> CF.CF -> Category -> CF.CFParser
+buildParser parser cf start tokens = trace "ParseCF" $
+ (parseResults, parseInformation)
+ where parseInformation = prtSep "\n" trees
+ parseResults = {-take maxTake-} [ (tree2cfTree t, []) | t <- trees ]
+ theInput = input tokens
+ edges = tracePrt "#edges" (prt.length) $
+ parser pInf [start] theInput
+ chart = tracePrt "#chart" (prt . map (length.snd) . aAssocs) $
+ edges2chart theInput $ map (fmap addCategory) edges
+ forests = tracePrt "#forests" (prt.length) $
+ chart2forests chart (const False) $
+ uncurry Edge (inputBounds theInput) start
+ trees = tracePrt "#trees" (prt.length) $
+ concatMap forest2trees forests
+ pInf = pInfo $ cf2grammar cf (nubsort tokens)
+
+
+addCategory (Rule cat rhs name) = Rule cat rhs (name, cat)
+
+tree2cfTree (TNode (name, cat) trees) = CF.CFTree (name, (cat, map tree2cfTree trees))
+
+cf2grammar :: CF.CF -> [Token] -> Grammar Name Category Token
+cf2grammar cf tokens = [ Rule 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
+
+-- maxTake :: Int
+-- maxTake = 500
+-- maxTake = maxBound
+
+