mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-22 19:22:50 -06:00
Some baby stpes closes to ATK SLF generation.
This commit is contained in:
@@ -5,9 +5,9 @@
|
|||||||
-- Stability : (stable)
|
-- Stability : (stable)
|
||||||
-- Portability : (portable)
|
-- Portability : (portable)
|
||||||
--
|
--
|
||||||
-- > CVS $Date: 2005/06/17 12:46:05 $
|
-- > CVS $Date: 2005/09/02 15:47:46 $
|
||||||
-- > CVS $Author: bringert $
|
-- > CVS $Author: bringert $
|
||||||
-- > CVS $Revision: 1.1 $
|
-- > CVS $Revision: 1.2 $
|
||||||
--
|
--
|
||||||
-- This module converts a CFG to an SLF finite-state network
|
-- This module converts a CFG to an SLF finite-state network
|
||||||
-- for use with the ATK recognizer. The SLF format is described
|
-- for use with the ATK recognizer. The SLF format is described
|
||||||
@@ -32,38 +32,36 @@ import GF.Infra.Option
|
|||||||
|
|
||||||
import Data.Char (toUpper,toLower)
|
import Data.Char (toUpper,toLower)
|
||||||
|
|
||||||
data SLF = SLF [SLFNode] [SLFEdge]
|
data SLF = SLF { slfNodes :: [SLFNode], slfEdges :: [SLFEdge] }
|
||||||
|
|
||||||
data SLFNode = SLFNode Int SLFWord
|
data SLFNode = SLFNode { nId :: Int, nWord :: SLFWord }
|
||||||
|
|
||||||
type SLFWord = Maybe String
|
-- | An SLF word is a word, or the empty string.
|
||||||
|
type SLFWord = String
|
||||||
|
|
||||||
data SLFEdge = SLFEdge Int Int Int
|
data SLFEdge = SLFEdge { eId :: Int, eStart :: Int, eEnd :: Int }
|
||||||
|
|
||||||
|
|
||||||
slfPrinter :: Ident -- ^ Grammar name
|
slfPrinter :: Ident -- ^ Grammar name
|
||||||
-> Options -> CGrammar -> String
|
-> Options -> CGrammar -> String
|
||||||
slfPrinter name opts cfg = prSLF slf ""
|
slfPrinter name opts cfg = prSLF slf ""
|
||||||
where gr = makeNice cfg
|
where slf = srg2slf $ makeSRG name opts $ makeRegular $ makeNice cfg
|
||||||
gr' = makeRegular gr
|
|
||||||
srg = makeSRG name opts gr'
|
|
||||||
slf = srg2slf srg
|
|
||||||
|
|
||||||
srg2slf :: SRG -> SLF
|
srg2slf :: SRG -> SLF
|
||||||
srg2slf = undefined
|
srg2slf = undefined -- should use TransformCFG.compileAutomaton
|
||||||
|
|
||||||
prSLF :: SLF -> ShowS
|
prSLF :: SLF -> ShowS
|
||||||
prSLF (SLF ns es) = header . unlinesS (map prNode ns) . unlinesS (map prEdge es)
|
prSLF (SLF { slfNodes = ns, slfEdges = es}) = header . unlinesS (map prNode ns) . unlinesS (map prEdge es)
|
||||||
where
|
where
|
||||||
header = showString "VERSION=1.0" . nl
|
header = showString "VERSION=1.0" . nl
|
||||||
. prFields [("N",show (length ns)),("L", show (length es))] . nl
|
. prFields [("N",show (length ns)),("L", show (length es))] . nl
|
||||||
prNode (SLFNode i w) = prFields [("I",show i),("W",showWord w)]
|
prNode n = prFields [("I",show (nId n)),("W",showWord (nWord n))]
|
||||||
prEdge (SLFEdge i s e) = prFields [("J",show i),("S",show s),("E",show e)]
|
prEdge e = prFields [("J",show (eId e)),("S",show (eStart e)),("E",show (eEnd e))]
|
||||||
|
|
||||||
|
|
||||||
showWord :: SLFWord -> String
|
showWord :: SLFWord -> String
|
||||||
showWord Nothing = "!NULL"
|
showWord "" = "!NULL"
|
||||||
showWord (Just w) = w -- FIXME: convert words to upper case
|
showWord w = w -- FIXME: convert words to upper case
|
||||||
|
|
||||||
prFields :: [(String,String)] -> ShowS
|
prFields :: [(String,String)] -> ShowS
|
||||||
prFields fs = unwordsS [ showString l . showChar '=' . showString v | (l,v) <- fs ]
|
prFields fs = unwordsS [ showString l . showChar '=' . showString v | (l,v) <- fs ]
|
||||||
|
|||||||
@@ -5,9 +5,9 @@
|
|||||||
-- Stability : (stable)
|
-- Stability : (stable)
|
||||||
-- Portability : (portable)
|
-- Portability : (portable)
|
||||||
--
|
--
|
||||||
-- > CVS $Date: 2005/06/17 12:46:05 $
|
-- > CVS $Date: 2005/09/02 15:47:47 $
|
||||||
-- > CVS $Author: bringert $
|
-- > CVS $Author: bringert $
|
||||||
-- > CVS $Revision: 1.13 $
|
-- > CVS $Revision: 1.14 $
|
||||||
--
|
--
|
||||||
-- This module does some useful transformations on CFGs.
|
-- This module does some useful transformations on CFGs.
|
||||||
--
|
--
|
||||||
@@ -40,7 +40,7 @@ type CFRules = FiniteMap Cat_ [CFRule_]
|
|||||||
-- | Remove left-recursion and categories with no productions
|
-- | Remove left-recursion and categories with no productions
|
||||||
-- from a context-free grammar.
|
-- from a context-free grammar.
|
||||||
makeNice :: CGrammar -> [CFRule_]
|
makeNice :: CGrammar -> [CFRule_]
|
||||||
makeNice = concat . eltsFM . makeNice' . groupProds . cfgToCFRules
|
makeNice = ungroupProds . makeNice' . groupProds . cfgToCFRules
|
||||||
where makeNice' = removeLeftRecursion . removeEmptyCats
|
where makeNice' = removeLeftRecursion . removeEmptyCats
|
||||||
|
|
||||||
cfgToCFRules :: CGrammar -> [CFRule_]
|
cfgToCFRules :: CGrammar -> [CFRule_]
|
||||||
@@ -55,6 +55,9 @@ groupProds :: [CFRule_] -> CFRules
|
|||||||
groupProds = addListToFM_C (++) emptyFM . map (\rs -> (ruleCat rs,[rs]))
|
groupProds = addListToFM_C (++) emptyFM . map (\rs -> (ruleCat rs,[rs]))
|
||||||
where ruleCat (CFRule c _ _) = c
|
where ruleCat (CFRule c _ _) = c
|
||||||
|
|
||||||
|
ungroupProds :: CFRules -> [CFRule_]
|
||||||
|
ungroupProds = concat . eltsFM
|
||||||
|
|
||||||
-- | Remove productions which use categories which have no productions
|
-- | Remove productions which use categories which have no productions
|
||||||
removeEmptyCats :: CFRules -> CFRules
|
removeEmptyCats :: CFRules -> CFRules
|
||||||
removeEmptyCats rss = listToFM $ fix removeEmptyCats' $ fmToList rss
|
removeEmptyCats rss = listToFM $ fix removeEmptyCats' $ fmToList rss
|
||||||
@@ -67,9 +70,6 @@ removeEmptyCats rss = listToFM $ fix removeEmptyCats' $ fmToList rss
|
|||||||
emptyCats = filter (nothingOrNull . flip lookup rs) allCats
|
emptyCats = filter (nothingOrNull . flip lookup rs) allCats
|
||||||
k' = map (\ (c,xs) -> (c, filter (not . anyUsedBy emptyCats) xs)) keep
|
k' = map (\ (c,xs) -> (c, filter (not . anyUsedBy emptyCats) xs)) keep
|
||||||
|
|
||||||
anyUsedBy :: [Cat_] -> CFRule_ -> Bool
|
|
||||||
anyUsedBy ss (CFRule _ r _) = or [c `elem` ss | Cat c <- r]
|
|
||||||
|
|
||||||
removeLeftRecursion :: CFRules -> CFRules
|
removeLeftRecursion :: CFRules -> CFRules
|
||||||
removeLeftRecursion rs = listToFM $ concatMap removeDirectLeftRecursion $ map handleProds $ fmToList rs
|
removeLeftRecursion rs = listToFM $ concatMap removeDirectLeftRecursion $ map handleProds $ fmToList rs
|
||||||
where
|
where
|
||||||
@@ -104,19 +104,50 @@ makeRegular :: [CFRule_] -> [CFRule_]
|
|||||||
makeRegular = undefined
|
makeRegular = undefined
|
||||||
|
|
||||||
{-
|
{-
|
||||||
isRightLinear :: [Cat_] -- ^ The categories to consider
|
-- | Get the sets of mutually recursive non-terminals for a grammar.
|
||||||
-> CFRule_
|
mutRecCats :: Eq c => [CFRule c n t] -> [[c]]
|
||||||
-> Bool
|
mutRecCats =
|
||||||
isRightLinear _ (CFRule _ ss _) | all isTerminal ss = True
|
|
||||||
isRightLinear cs
|
|
||||||
-}
|
-}
|
||||||
|
|
||||||
-- Use the strongly regular grammar to finite automaton
|
{-
|
||||||
-- compilation algorithm from \"Regular Approximation of Context-free
|
-- | Get a map of categories to all categories which can occur in
|
||||||
-- Grammars through Approximation\", Mohri and Nederhof, 2000
|
-- the result of rewriting each category.
|
||||||
|
allCatsTrans :: CFRules -> FinitMap
|
||||||
|
allCatsTrans g c =
|
||||||
|
-}
|
||||||
|
|
||||||
|
-- Convert a strongly regular grammar to a finite automaton.
|
||||||
-- compileAutomaton ::
|
-- compileAutomaton ::
|
||||||
|
|
||||||
|
--
|
||||||
|
-- CFG rule utilities
|
||||||
|
--
|
||||||
|
|
||||||
|
-- | Checks if a context-free rule is right-linear.
|
||||||
|
isRightLinear :: Eq c => [c] -- ^ The categories to consider
|
||||||
|
-> CFRule c n t -- ^ The rule to check for right-linearity
|
||||||
|
-> Bool
|
||||||
|
isRightLinear cs (CFRule _ ss _) = all (not . catElem cs) (safeInit ss)
|
||||||
|
|
||||||
|
-- | Checks if a context-free rule is left-linear.
|
||||||
|
isLeftLinear :: Eq c => [c] -- ^ The categories to consider
|
||||||
|
-> CFRule c n t -- ^ The rule to check for right-linearity
|
||||||
|
-> Bool
|
||||||
|
isLeftLinear cs (CFRule _ ss _) = all (not . catElem cs) (drop 1 ss)
|
||||||
|
|
||||||
|
-- | Checks if a symbol is a non-terminal of one of the given categories.
|
||||||
|
catElem :: Eq c => [c] -> Symbol c t -> Bool
|
||||||
|
catElem cs (Tok _) = False
|
||||||
|
catElem cs (Cat c) = c `elem` cs
|
||||||
|
|
||||||
|
-- | Check if any of the categories used on the right-hand side
|
||||||
|
-- are in the given list of categories.
|
||||||
|
anyUsedBy :: Eq c => [c] -> CFRule c n t -> Bool
|
||||||
|
anyUsedBy cs (CFRule _ ss _) = any (catElem cs) ss
|
||||||
|
|
||||||
|
--
|
||||||
|
-- * Utilities
|
||||||
|
--
|
||||||
|
|
||||||
fix :: Eq a => (a -> a) -> a -> a
|
fix :: Eq a => (a -> a) -> a -> a
|
||||||
fix f x = let x' = f x in if x' == x then x else fix f x'
|
fix f x = let x' = f x in if x' == x then x else fix f x'
|
||||||
@@ -124,3 +155,7 @@ fix f x = let x' = f x in if x' == x then x else fix f x'
|
|||||||
nothingOrNull :: Maybe [a] -> Bool
|
nothingOrNull :: Maybe [a] -> Bool
|
||||||
nothingOrNull Nothing = True
|
nothingOrNull Nothing = True
|
||||||
nothingOrNull (Just xs) = null xs
|
nothingOrNull (Just xs) = null xs
|
||||||
|
|
||||||
|
safeInit :: [a] -> [a]
|
||||||
|
safeInit [] = []
|
||||||
|
safeInit xs = init xs
|
||||||
|
|||||||
Reference in New Issue
Block a user