mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 03:32:51 -06:00
Fixed left recursion removal to not create cyclic rules.
This commit is contained in:
@@ -87,6 +87,8 @@ removeIdenticalRules g = [(c,sortNubBy cmpRules rs) | (c,rs) <- g]
|
|||||||
cmpRules (CFRule c1 ss1 _) (CFRule c2 ss2 _) =
|
cmpRules (CFRule c1 ss1 _) (CFRule c2 ss2 _) =
|
||||||
mconcat [c1 `compare` c2, ss1 `compare` ss2]
|
mconcat [c1 `compare` c2, ss1 `compare` ss2]
|
||||||
|
|
||||||
|
-- Paull's algorithm, see
|
||||||
|
-- http://research.microsoft.com/users/bobmoore/naacl2k-proc-rev.pdf
|
||||||
removeLeftRecursion :: CFRules -> CFRules
|
removeLeftRecursion :: CFRules -> CFRules
|
||||||
removeLeftRecursion rs = removeDirectLeftRecursions $ map handleProds rs
|
removeLeftRecursion rs = removeDirectLeftRecursions $ map handleProds rs
|
||||||
where
|
where
|
||||||
@@ -113,7 +115,10 @@ removeDirectLeftRecursion (a,rs)
|
|||||||
let as = maybeEndWithA' nr
|
let as = maybeEndWithA' nr
|
||||||
is = [CFRule a' (tail r) n | CFRule _ r n <- dr]
|
is = [CFRule a' (tail r) n | CFRule _ r n <- dr]
|
||||||
a's = maybeEndWithA' is
|
a's = maybeEndWithA' is
|
||||||
maybeEndWithA' xs = xs ++ [CFRule c (r++[Cat a']) n | CFRule c r n <- xs]
|
-- the not null constraint here avoids creating new
|
||||||
|
-- left recursive (cyclic) rules.
|
||||||
|
maybeEndWithA' xs = xs ++ [CFRule c (r++[Cat a']) n | CFRule c r n <- xs,
|
||||||
|
not (null r)]
|
||||||
return [(a, as), (a', a's)]
|
return [(a, as), (a', a's)]
|
||||||
where
|
where
|
||||||
(dr,nr) = partition isDirectLeftRecursive rs
|
(dr,nr) = partition isDirectLeftRecursive rs
|
||||||
|
|||||||
Reference in New Issue
Block a user