summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpeb <peb@cs.chalmers.se>2006-06-07 20:25:39 +0000
committerpeb <peb@cs.chalmers.se>2006-06-07 20:25:39 +0000
commit14f9452fd8e490f05b5eb721b77b41d825a23fd5 (patch)
tree11c0953303c3a035d35d704bea310bed5d61fdb4 /src
parent06e8ae025e6953ec8b43aca04bfcd89b8a578c1a (diff)
added the file GF.Conversion.MCFGtoFCFG
Diffstat (limited to 'src')
-rw-r--r--src/GF/Conversion/MCFGtoFCFG.hs51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/GF/Conversion/MCFGtoFCFG.hs b/src/GF/Conversion/MCFGtoFCFG.hs
new file mode 100644
index 000000000..70aa4644d
--- /dev/null
+++ b/src/GF/Conversion/MCFGtoFCFG.hs
@@ -0,0 +1,51 @@
+----------------------------------------------------------------------
+-- |
+-- Maintainer : PL
+-- Stability : (stable)
+-- Portability : (portable)
+--
+-- > CVS $Date: 2005/05/09 09:28:43 $
+-- > CVS $Author: peb $
+-- > CVS $Revision: 1.6 $
+--
+-- Converting MCFG grammars to equivalent optimized FCFG
+-----------------------------------------------------------------------------
+
+
+module GF.Conversion.MCFGtoFCFG
+ (convertGrammar) where
+
+import Control.Monad
+import List (elemIndex)
+import Array
+
+import GF.Formalism.Utilities
+import GF.Formalism.GCFG
+import GF.Formalism.MCFG
+import GF.Formalism.FCFG
+import GF.Conversion.Types
+import GF.Data.SortedList (nubsort)
+
+import GF.Infra.Print
+
+----------------------------------------------------------------------
+-- * converting MCFG to optimized FCFG
+
+convertGrammar :: MGrammar -> FGrammar
+convertGrammar gram = [ FRule (Abs (fcat cat) (map fcat cats) name) (fcnc cnc) |
+ Rule (Abs cat cats name) cnc <- gram ]
+ where mcats = nubsort [ mc | Rule (Abs mcat mcats _) _ <- gram, mc <- mcat:mcats ]
+
+ fcat mcat@(MCat (ECat scat ecns) mlbls)
+ = case elemIndex mcat mcats of
+ Just catid -> FCat catid scat mlbls ecns
+ Nothing -> error ("MCFGtoFCFG.fcat " ++ prt mcat)
+
+ fcnc (Cnc _ arglbls lins) = listArray (0, length lins-1) (map flin lins)
+ where flin (Lin _ syms) = listArray (0, length syms-1) (map fsym syms)
+ fsym (Tok tok) = FSymTok tok
+ fsym (Cat (cat,lbl,arg)) = FSymCat (fcat cat) (flbl arg lbl) arg
+ flbl arg lbl = case elemIndex lbl (arglbls !! arg) of
+ Just lblid -> lblid
+ Nothing -> error ("MCFGtoFCFG.flbl " ++ prt arg ++ " " ++ prt lbl)
+