gsl printer now figures out startcat from grammar flags

This commit is contained in:
bringert
2004-09-14 15:05:37 +00:00
parent 7f79a8eaa4
commit 79f1afe65b
2 changed files with 27 additions and 13 deletions

View File

@@ -28,7 +28,7 @@
-- FIXME: figure out name prefix from grammar name -- FIXME: figure out name prefix from grammar name
module PrGSL (prGSL) where module PrGSL (gslPrinter) where
import Ident import Ident
import CFGrammar import CFGrammar
@@ -36,26 +36,36 @@ import Parser (Symbol(..))
import GrammarTypes import GrammarTypes
import PrintParser import PrintParser
import TransformCFG import TransformCFG
import Option
import Data.List import Data.List
import Data.Maybe (fromMaybe)
import Data.FiniteMap import Data.FiniteMap
type GSLGrammar = [GSLRule] data GSLGrammar = GSLGrammar String [GSLRule]
data GSLRule = GSLRule String [GSLAlt] data GSLRule = GSLRule String [GSLAlt]
type GSLAlt = [Symbol String Token] type GSLAlt = [Symbol String Token]
type CatNames = FiniteMap String String type CatNames = FiniteMap String String
prGSL :: CFGrammar -> String gslPrinter :: Options -> CFGrammar -> String
prGSL cfg = prGSLGrammar names gsl "" gslPrinter opts = prGSL start
where mstart = getOptVal opts gStartCat
start = fromMaybe "S" mstart ++ "{}.s"
prGSL :: String -- ^ startcat
-> CFGrammar -> String
prGSL start cfg = prGSLGrammar names gsl ""
where where
cfg' = makeNice cfg cfg' = makeNice cfg
gsl = cfgToGSL cfg' gsl = cfgToGSL start cfg'
names = mkCatNames "GSL_" gsl names = mkCatNames "GSL_" gsl
cfgToGSL :: [CFRule_] -> GSLGrammar cfgToGSL :: String -- ^ startcat
cfgToGSL = map cfgRulesToGSLRule . sortAndGroupBy ruleCat -> [CFRule_] -> GSLGrammar
cfgToGSL start =
GSLGrammar start . map cfgRulesToGSLRule . sortAndGroupBy ruleCat
where where
ruleCat (Rule c _ _) = c ruleCat (Rule c _ _) = c
ruleRhs (Rule _ r _) = r ruleRhs (Rule _ r _) = r
@@ -63,14 +73,18 @@ cfgToGSL = map cfgRulesToGSLRule . sortAndGroupBy ruleCat
mkCatNames :: String -- name prefix mkCatNames :: String -- name prefix
-> GSLGrammar -> CatNames -> GSLGrammar -> CatNames
mkCatNames pref gsl = listToFM (zip lhsCats names) mkCatNames pref (GSLGrammar start rules) =
listToFM (zipWith dotIfStart lhsCats names)
where names = [pref ++ show x | x <- [0..]] where names = [pref ++ show x | x <- [0..]]
lhsCats = [ c | GSLRule c _ <- gsl ] lhsCats = [ c | GSLRule c _ <- rules]
dotIfStart c n | c == start = (c, "." ++ n)
| otherwise = (c, n)
prGSLGrammar :: CatNames -> GSLGrammar -> ShowS prGSLGrammar :: CatNames -> GSLGrammar -> ShowS
prGSLGrammar names g = header . unlinesS (map prGSLrule g) prGSLGrammar names (GSLGrammar start g) = header . unlinesS (map prGSLrule g)
where where
header = showString ";GSL2.0" . nl header = showString ";GSL2.0" . nl
. showString ("; startcat = " ++ start ) . nl
prGSLrule (GSLRule cat rhs) = prGSLrule (GSLRule cat rhs) =
showString "; " . prtS cat . nl showString "; " . prtS cat . nl
. prGSLCat cat . sp . wrap "[" (unwordsS (map prGSLAlt rhs)) "]" . nl . prGSLCat cat . sp . wrap "[" (unwordsS (map prGSLAlt rhs)) "]" . nl

View File

@@ -27,7 +27,7 @@ import PrGrammar
import PrOld import PrOld
import MkGFC import MkGFC
import CFtoSRG import CFtoSRG
import PrGSL (prGSL) import PrGSL (gslPrinter)
import Zipper import Zipper
@@ -191,7 +191,7 @@ customGrammarPrinter =
,(strCI "cf", prCF . stateCF) ,(strCI "cf", prCF . stateCF)
,(strCI "old", printGrammarOld . stateGrammarST) ,(strCI "old", printGrammarOld . stateGrammarST)
,(strCI "srg", prSRG . stateCF) ,(strCI "srg", prSRG . stateCF)
,(strCI "gsl", prGSL . Cnv.cfg . statePInfo) ,(strCI "gsl", \s -> gslPrinter (stateOptions s) $ Cnv.cfg $ statePInfo s)
,(strCI "lbnf", prLBNF . stateCF) ,(strCI "lbnf", prLBNF . stateCF)
,(strCI "haskell", grammar2haskell . stateGrammarST) ,(strCI "haskell", grammar2haskell . stateGrammarST)
,(strCI "morpho", prMorpho . stateMorpho) ,(strCI "morpho", prMorpho . stateMorpho)