mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-22 09:32:53 -06:00
printing abs as BNF
This commit is contained in:
32
src/GF/Canon/AbsToBNF.hs
Normal file
32
src/GF/Canon/AbsToBNF.hs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
module GF.Canon.AbsToBNF where
|
||||||
|
|
||||||
|
import GF.Grammar.SGrammar
|
||||||
|
import GF.Data.Operations
|
||||||
|
import GF.Infra.Option
|
||||||
|
import GF.Canon.GFC (CanonGrammar)
|
||||||
|
|
||||||
|
abstract2bnf :: CanonGrammar -> String
|
||||||
|
abstract2bnf = sgrammar2bnf . gr2sgr noOptions emptyProbs
|
||||||
|
|
||||||
|
sgrammar2bnf :: SGrammar -> String
|
||||||
|
sgrammar2bnf = unlines . map (prBNFRule . mkBNF) . allRules
|
||||||
|
|
||||||
|
prBNFRule :: BNFRule -> String
|
||||||
|
prBNFRule = id
|
||||||
|
|
||||||
|
type BNFRule = String
|
||||||
|
|
||||||
|
mkBNF :: SRule -> BNFRule
|
||||||
|
mkBNF (pfun,(args,cat)) =
|
||||||
|
fun ++ "." +++ gfId cat +++ "::=" +++ rhs +++ ";"
|
||||||
|
where
|
||||||
|
fun = gfId (snd pfun)
|
||||||
|
rhs = case args of
|
||||||
|
[] -> prQuotedString (snd pfun)
|
||||||
|
_ -> unwords (map gfId args)
|
||||||
|
|
||||||
|
gfId i = case i of
|
||||||
|
"Int" -> "Integer"
|
||||||
|
"String" -> i
|
||||||
|
"Float" -> "Double"
|
||||||
|
_ -> "G" ++ i ++ "_"
|
||||||
@@ -96,6 +96,7 @@ import qualified GF.Conversion.Prolog as CnvProlog
|
|||||||
import qualified GF.Conversion.TypeGraph as CnvTypeGraph
|
import qualified GF.Conversion.TypeGraph as CnvTypeGraph
|
||||||
import GF.Canon.Unparametrize
|
import GF.Canon.Unparametrize
|
||||||
import GF.Canon.Subexpressions
|
import GF.Canon.Subexpressions
|
||||||
|
import GF.Canon.AbsToBNF
|
||||||
|
|
||||||
import GF.Canon.GFC
|
import GF.Canon.GFC
|
||||||
import qualified GF.Canon.MkGFC as MC
|
import qualified GF.Canon.MkGFC as MC
|
||||||
@@ -267,6 +268,7 @@ customGrammarPrinter =
|
|||||||
,(strCI "plbnf", \_ -> prLBNF True)
|
,(strCI "plbnf", \_ -> prLBNF True)
|
||||||
,(strCI "lbnf", \_ -> prLBNF False)
|
,(strCI "lbnf", \_ -> prLBNF False)
|
||||||
,(strCI "bnf", \_ -> prBNF False)
|
,(strCI "bnf", \_ -> prBNF False)
|
||||||
|
,(strCI "absbnf", \_ -> abstract2bnf . stateGrammarST)
|
||||||
,(strCI "haskell", \_ -> grammar2haskell . stateGrammarST)
|
,(strCI "haskell", \_ -> grammar2haskell . stateGrammarST)
|
||||||
,(strCI "transfer", \_ -> grammar2transfer . stateGrammarST)
|
,(strCI "transfer", \_ -> grammar2transfer . stateGrammarST)
|
||||||
,(strCI "morpho", \_ -> prMorpho . stateMorpho)
|
,(strCI "morpho", \_ -> prMorpho . stateMorpho)
|
||||||
|
|||||||
Reference in New Issue
Block a user