mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-20 08:32:50 -06:00
Added an FCFG output format (--output-format=fcfg). This now lives in GF.Speech.PGFToCFG, but should probably move somewhere else.
This commit is contained in:
@@ -37,6 +37,7 @@ exportPGF opts fmt pgf =
|
|||||||
FmtProlog -> multi "pl" grammar2prolog
|
FmtProlog -> multi "pl" grammar2prolog
|
||||||
FmtProlog_Abs -> multi "pl" grammar2prolog_abs
|
FmtProlog_Abs -> multi "pl" grammar2prolog_abs
|
||||||
FmtBNF -> single "bnf" bnfPrinter
|
FmtBNF -> single "bnf" bnfPrinter
|
||||||
|
FmtFCFG -> single "fcfg" fcfgPrinter
|
||||||
FmtSRGS_XML -> single "grxml" (srgsXmlPrinter sisr)
|
FmtSRGS_XML -> single "grxml" (srgsXmlPrinter sisr)
|
||||||
FmtSRGS_XML_NonRec -> single "grxml" srgsXmlNonRecursivePrinter
|
FmtSRGS_XML_NonRec -> single "grxml" srgsXmlNonRecursivePrinter
|
||||||
FmtSRGS_ABNF -> single "gram" (srgsAbnfPrinter sisr)
|
FmtSRGS_ABNF -> single "gram" (srgsAbnfPrinter sisr)
|
||||||
@@ -67,3 +68,4 @@ outputConcr pgf = case cncnames pgf of
|
|||||||
|
|
||||||
printPGF :: PGF -> String
|
printPGF :: PGF -> String
|
||||||
printPGF = encodeUTF8 . printTree . fromPGF
|
printPGF = encodeUTF8 . printTree . fromPGF
|
||||||
|
|
||||||
|
|||||||
@@ -87,6 +87,7 @@ data OutputFormat = FmtPGF
|
|||||||
| FmtProlog
|
| FmtProlog
|
||||||
| FmtProlog_Abs
|
| FmtProlog_Abs
|
||||||
| FmtBNF
|
| FmtBNF
|
||||||
|
| FmtFCFG
|
||||||
| FmtSRGS_XML
|
| FmtSRGS_XML
|
||||||
| FmtSRGS_XML_NonRec
|
| FmtSRGS_XML_NonRec
|
||||||
| FmtSRGS_ABNF
|
| FmtSRGS_ABNF
|
||||||
@@ -455,6 +456,7 @@ outputFormats =
|
|||||||
("prolog", FmtProlog),
|
("prolog", FmtProlog),
|
||||||
("prolog_abs", FmtProlog_Abs),
|
("prolog_abs", FmtProlog_Abs),
|
||||||
("bnf", FmtBNF),
|
("bnf", FmtBNF),
|
||||||
|
("fcfg", FmtFCFG),
|
||||||
("srgs_xml", FmtSRGS_XML),
|
("srgs_xml", FmtSRGS_XML),
|
||||||
("srgs_xml_nonrec", FmtSRGS_XML_NonRec),
|
("srgs_xml_nonrec", FmtSRGS_XML_NonRec),
|
||||||
("srgs_abnf", FmtSRGS_ABNF),
|
("srgs_abnf", FmtSRGS_ABNF),
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
--
|
--
|
||||||
-- Approximates PGF grammars with context-free grammars.
|
-- Approximates PGF grammars with context-free grammars.
|
||||||
----------------------------------------------------------------------
|
----------------------------------------------------------------------
|
||||||
module GF.Speech.PGFToCFG (bnfPrinter, pgfToCFG) where
|
module GF.Speech.PGFToCFG (bnfPrinter, fcfgPrinter, pgfToCFG) where
|
||||||
|
|
||||||
import PGF.CId
|
import PGF.CId
|
||||||
import PGF.Data as PGF
|
import PGF.Data as PGF
|
||||||
@@ -13,6 +13,7 @@ import GF.Infra.Ident
|
|||||||
import GF.Speech.CFG
|
import GF.Speech.CFG
|
||||||
|
|
||||||
import Data.Array as Array
|
import Data.Array as Array
|
||||||
|
import Data.List
|
||||||
import Data.Map (Map)
|
import Data.Map (Map)
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
@@ -22,6 +23,22 @@ import qualified Data.Set as Set
|
|||||||
bnfPrinter :: PGF -> CId -> String
|
bnfPrinter :: PGF -> CId -> String
|
||||||
bnfPrinter pgf cnc = prCFG $ pgfToCFG pgf cnc
|
bnfPrinter pgf cnc = prCFG $ pgfToCFG pgf cnc
|
||||||
|
|
||||||
|
-- FIXME: move this somewhere else
|
||||||
|
fcfgPrinter :: PGF -> CId -> String
|
||||||
|
fcfgPrinter pgf cnc = unlines (map showRule rules)
|
||||||
|
where
|
||||||
|
pinfo = fromMaybe (error "fcfgPrinter") (lookParser pgf cnc)
|
||||||
|
|
||||||
|
rules :: [FRule]
|
||||||
|
rules = Array.elems (PGF.allRules pinfo)
|
||||||
|
|
||||||
|
showRule (FRule cid ps cs fc arr) = prCId cid ++ " " ++ show ps ++ ". " ++ showCat fc ++ " ::= " ++ unwords (map showCat cs) ++ " = " ++ showLin arr
|
||||||
|
where
|
||||||
|
showLin arr = "[" ++ concat (intersperse ", " [ unwords (map showFSymbol (Array.elems r)) | r <- Array.elems arr]) ++ "]"
|
||||||
|
showFSymbol (FSymCat i j) = showCat (cs!!j) ++ "_" ++ show j ++ "." ++ show i
|
||||||
|
showFSymbol (FSymTok t) = t
|
||||||
|
showCat c = "C" ++ show c
|
||||||
|
|
||||||
pgfToCFG :: PGF
|
pgfToCFG :: PGF
|
||||||
-> CId -- ^ Concrete syntax name
|
-> CId -- ^ Concrete syntax name
|
||||||
-> CFG
|
-> CFG
|
||||||
|
|||||||
Reference in New Issue
Block a user