mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 03:32:51 -06:00
Removed cycles when building SRGs, to avoid getting left-recursive grammars.
This commit is contained in:
@@ -74,7 +74,7 @@ makeSimpleSRG :: Ident -- ^ Grammar name
|
|||||||
-> CGrammar -- ^ A context-free grammar
|
-> CGrammar -- ^ A context-free grammar
|
||||||
-> SRG
|
-> SRG
|
||||||
makeSimpleSRG
|
makeSimpleSRG
|
||||||
= makeSRG_ (removeLeftRecursion . removeIdenticalRules . removeEmptyCats)
|
= makeSRG_ (removeLeftRecursion . removeIdenticalRules . removeEmptyCats . removeCycles)
|
||||||
|
|
||||||
-- | Create a SRG preserving the names, profiles and probabilities of the
|
-- | Create a SRG preserving the names, profiles and probabilities of the
|
||||||
-- input grammar. The returned grammar may be left-recursive.
|
-- input grammar. The returned grammar may be left-recursive.
|
||||||
|
|||||||
@@ -100,8 +100,7 @@ removeLeftRecursion rs = removeDirectLeftRecursions $ map handleProds rs
|
|||||||
[CFRule ai (beta ++ alpha) n | CFRule _ beta _ <- lookup' aj rs]
|
[CFRule ai (beta ++ alpha) n | CFRule _ beta _ <- lookup' aj rs]
|
||||||
handleProd r = [r]
|
handleProd r = [r]
|
||||||
|
|
||||||
removeDirectLeftRecursions :: [(Cat_,[CFRule_])] -- ^ All productions for a category
|
removeDirectLeftRecursions :: CFRules -> CFRules
|
||||||
-> CFRules
|
|
||||||
removeDirectLeftRecursions = concat . flip evalState 0 . mapM removeDirectLeftRecursion
|
removeDirectLeftRecursions = concat . flip evalState 0 . mapM removeDirectLeftRecursion
|
||||||
|
|
||||||
removeDirectLeftRecursion :: (Cat_,[CFRule_]) -- ^ All productions for a category
|
removeDirectLeftRecursion :: (Cat_,[CFRule_]) -- ^ All productions for a category
|
||||||
@@ -124,6 +123,9 @@ isDirectLeftRecursive :: CFRule_ -> Bool
|
|||||||
isDirectLeftRecursive (CFRule c (Cat c':_) _) = c == c'
|
isDirectLeftRecursive (CFRule c (Cat c':_) _) = c == c'
|
||||||
isDirectLeftRecursive _ = False
|
isDirectLeftRecursive _ = False
|
||||||
|
|
||||||
|
removeCycles :: CFRules -> CFRules
|
||||||
|
removeCycles = groupProds . removeCycles_ . ungroupProds
|
||||||
|
where removeCycles_ rs = [r | r@(CFRule c rhs n) <- rs, rhs /= [Cat c]]
|
||||||
|
|
||||||
--
|
--
|
||||||
-- * CFG rule utilities
|
-- * CFG rule utilities
|
||||||
|
|||||||
Reference in New Issue
Block a user