RegEqns
structure
signature REGEQNS
structure RegEqns
: REGEQNS
The structure RegEqns extends RegExp to systems of recursion equations with regular expressions as right hand sides. Only those functions manipulating such systems are mentioned that do not change the least solution in all continuous Kleenean algebras.
include REGEXP_WEAK
type 'b eqns = (int * 'b) list
type 'a env = int -> 'a
val maxVar : 'a re eqns -> int
val nthVar : 'a re eqns * int -> 'a re
val freeVars : 'a re eqns -> int list
val recVars : 'a re eqns -> int list
val eliminateStars : ''a re eqns -> ''a re eqns
val convertToCFG : ''a re eqns -> ''a re eqns
val greibachNF : ''a re eqns -> ''a re eqns
val leftToRightRecursion : ''a re eqns -> ''a re eqns
val isolateLeftVarFactors : ''a re eqns
-> ''a RegMat.t
* ''a RegMat.t
val solveRightlinearEqns : ''a re eqns -> ''a re eqns
val fix : ('a env -> 'b -> 'a)
-> 'a env
-> ('a env -> ''c) -> 'b eqns -> 'a env
val solve : ('a env -> 'b -> 'a)
-> 'a env
-> ('a env -> ''c) -> 'b eqns -> 'a eqns
val iter : ('a env -> 'b -> 'a)
-> 'a env -> int -> 'b eqns -> 'a env
val iterate : ('a env -> 'b -> 'a)
-> 'a env -> int -> 'b eqns -> 'a eqns
type 'b eqns = (int * 'b) list
type 'a env = int -> 'a
maxVar eqs
nthVar (eqs, i)
freeVars eqs
recVars eqs
eliminateStars eqs
convertToCFG eqs
greibachNF eqs
Applies leftToRightRecursion and then substitutes (tY + t) for leading variables x in R of Y = RY + R.
leftToRightRecursion eqs
We write eqs as a matrix equation x = xR + t, whose least solution is the same as that for the system {x = tR* = t + tR+}, which is right-recursive when written with additional unknowns Y as {x = tY + t, Y = RY + R}, where R,t depend on x. Assuming t has no 1, all remaining leading recursion variables are from x in R. Note that the new recursion equations Y = RY + R are right-recursive in Y.
isolateLeftVarFactors eqs
solveRightlinearEqns eqs
fix eval env test eqs
test is intended to be an efficient way to determine wether all right hand sides of eqs evaluate to the same values under eval both with env_k and env_k+1.
solve eval env test eqs
iter eval env i eqs
iterate eval env i eqs
RegExp, RegMat, ppRegEqns
To print recursion equation systems with names for variables and atoms provided by the user, see ppRegEqns.
Example:
Solve a right-linear system by regular expressions without recursion variables:
- val rlE = [(0,Plus[Times[Atom "a",Var 0],Atom "b"]), (1,Plus[Times[Atom "c",Var 0],Var 0,Var 1])]; - solveRightlinearEqns rlE; val it = [(0,Times [Star (Atom "a"),Atom "b"]), (1,Times [Plus [Times [],Atom "c"],Star (Atom "a"),Atom "b"])] : string re eqns
Example:
Transform regular recursion equations to a context free grammar:
- val EBNF = [(0,Plus[Star(Times[Var 1,Atom "a",Var 0]),Atom "b"]), (1,Times[Plus[Atom "c",Var 0],Var 3])]; - convertToCFG EBNF; val it = [(0,Plus [Var 4,Atom "b"]), (1,Plus [Times [Atom "c",Var 3],Times [Var 0,Var 3]]), (4,Plus [Times [Var 1,Atom "a",Var 0,Var 4],Times []])] : string re eqnsSince there was a non-recursion variable Var 3, the new recursion variable needed to eliminate the Star is Var 4.
Example:
Replace left-recursion by right-recursion, using n^2 additional equations, if the input system has n equations:
- val E = [(0,Plus[Times[Var 1,Atom "a",Var 0],Atom "b"]), (1,Plus[Atom "c",Var 0])]; - val eLR = leftToRightRecursion E; val eLR = [(0,Plus [Atom "b",Times [Atom "b",Var 2],Times [Atom "c",Var 4]]), (1,Plus [Atom "c",Times [Atom "b",Var 3],Times [Atom "c",Var 5]]), (2,Var 4), (3,Plus [Var 5,Times []]), (4,Plus [Times [Atom "a",Var 0,Var 2],Times [Atom "a",Var 0]]), (5,Times [Atom "a",Var 0,Var 3])] : string re eqnsWhile the old recursion variables no longer occur as left factors, some of the new recursion variables do. Using greibachNF replaces these (and other occurrences) by the right-hand sides of their recursion equations.