summaryrefslogtreecommitdiff
path: root/src/GF/Speech/PrFA.hs
diff options
context:
space:
mode:
authorbringert <unknown>2005-11-10 13:19:33 +0000
committerbringert <unknown>2005-11-10 13:19:33 +0000
commit5ae8cdc3d08fb1aae74e6da5c647c6c76cb46d15 (patch)
treeff70a3acda1775abc6bae8c52c98741c324d0ee3 /src/GF/Speech/PrFA.hs
parente322a9e5e367f87955f7f0b53546a7402c8e2f0e (diff)
Moved FA and regular grammar printers to a separate module.
Diffstat (limited to 'src/GF/Speech/PrFA.hs')
-rw-r--r--src/GF/Speech/PrFA.hs50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/GF/Speech/PrFA.hs b/src/GF/Speech/PrFA.hs
new file mode 100644
index 000000000..28745066f
--- /dev/null
+++ b/src/GF/Speech/PrFA.hs
@@ -0,0 +1,50 @@
+----------------------------------------------------------------------
+-- |
+-- Module : PrSLF
+-- Maintainer : BB
+-- Stability : (stable)
+-- Portability : (portable)
+--
+-- > CVS $Date: 2005/11/10 14:19:33 $
+-- > CVS $Author: bringert $
+-- > CVS $Revision: 1.1 $
+--
+-- This module prints finite automata and regular grammars
+-- for a context-free grammar.
+--
+-- FIXME: remove \/ warn \/ fail if there are int \/ string literal
+-- categories in the grammar
+-----------------------------------------------------------------------------
+
+module GF.Speech.PrFA (faGraphvizPrinter,regularPrinter) where
+
+import GF.Data.Utilities
+import GF.Conversion.Types
+import GF.Formalism.CFG
+import GF.Formalism.Utilities (Symbol(..),symbol)
+import GF.Infra.Ident
+import GF.Infra.Option
+import GF.Infra.Print
+import GF.Speech.CFGToFiniteState
+import GF.Speech.FiniteState
+import GF.Speech.SRG
+import GF.Speech.TransformCFG
+
+import Data.Char (toUpper,toLower)
+import Data.List
+import Data.Maybe (fromMaybe)
+
+faGraphvizPrinter :: Ident -- ^ Grammar name
+ -> Options -> CGrammar -> String
+faGraphvizPrinter name opts cfg =
+ prFAGraphviz (mapStates (const "") $ mapTransitions (fromMaybe "") $ cfgToFA name opts cfg)
+
+
+-- | Convert the grammar to a regular grammar and print it in BNF
+regularPrinter :: CGrammar -> String
+regularPrinter = prCFRules . makeSimpleRegular
+ where
+ prCFRules :: CFRules -> String
+ prCFRules g = unlines [ c ++ " ::= " ++ join " | " (map (showRhs . ruleRhs) rs) | (c,rs) <- g]
+ join g = concat . intersperse g
+ showRhs = unwords . map (symbol id show)