Grammars
structure
signature GRAMMARS
structure Grammars
: GRAMMARS
This structure provides functions that determine properties of regular recursion equation systems, and their least solution in the language interpretation. (The structure Grammars will probably be modified to subsume the structure RegEqns.)
It also provides functions to transform such an equation system (or: extendend Backus-Naur-Form grammar) to grammars having the same least solution, except that the empty word is removed, etc.
type 'a t = 'a list Set.t
type 'a env = int -> 'a
type 'a eqns = (int * 'a) list
datatype re = datatype RegExp.re
val Unit : 'a re
val Zero : 'a re
val maxVar : 'a re eqns -> int
val recVars : 'a re eqns -> int list
val freeVars : 'a re eqns -> int list
val unit : unit -> ''a t
val zero : unit -> ''a t
val atom : ''a -> ''a t
val initial : int -> ''a t -> ''a t
val geUnit : bool env -> 'a re eqns -> 'a re -> bool
val nonEmpty : bool env -> 'a re eqns -> 'a re -> bool
val subtractUnits : bool env -> 'a re eqns -> 'a re eqns
val nfEmpty : bool env -> ''a re eqns -> ''a re eqns
val first : int
-> ''a t env
-> ''a re eqns -> ''a re -> ''a t
val leftcontexts : ''a re eqns -> ''a re -> ''a re eqns
val follows : int
-> ''a t env
-> ''a re eqns
-> ''a re -> ''a re -> ''a t
Unit
Zero
maxVar eqs
recVars eqs
freeVars eqs
unit ()
zero ()
atom a
initial k t
geUnit env eqs r
nonEmpty env eqs r
subtractUnits env eqs
nfEmpty env eqs
first k env eqs r
leftcontexts eqs r
follows k env eqs s r
RegEqns
,RegExp
Since we allow free non-recursive variables, most functions need an environment giving properties of the languages interpreting these variables. Normally, a grammar does not have such free variables, and the choice of the environment is then irrelevant.
For the following examples, consider an equation system with two non-recursion variables; for readability, we use the printing of equations.
- structure Grm = ppRegEqns (StandardNames); open Grm; open Grammars; - val Eqns = [(0,Plus[Times[Atom "a",Var 1,Var 4],Var 2]), (1,Plus[Times[Atom "a",Var 1,Atom "b"],Times[]])]; printEqns Eqns; Equation system: x0 = (a;x1;x4 + x2) x1 = (a;x1;b + 1) - fun falseEnv i = false and trueEnv i = true
Example:
Assuming all free variables denote the empty language, the least solution has the following non-empty components:
- printEqns (nfEmpty trueEnv Eqns); Equation system: x0 = 0 x1 = (a;x1;b + 1)
Example:
Assuming the non-recursion variables denote languages not containing the empty word, removing the empty word from the languages defined by Eqns gives the languages defined by the grammar
- printEqns (subtractUnits falseEnv Eqns); Equation system: x0 = (a;(1 + x1);x4 + x2) x1 = a;(1 + x1);b
Example:
- structure NamesG = struct type alph = string fun atomName (a : alph) = a : string fun varName 0 = "S" | varName 1 = "E" | varName i = "x"^ Int.toString i end; - structure Grm = ppRegEqns(NamesG); open Grm; - val eqs = [(0,Plus[Times[Var 1,Atom "=",Var 1],Times[Atom "[",Var 0,Atom "]"]]), (1,Plus[Times[Atom "[",Var 1,Atom "*",Var 1,Atom "]"],Atom "a",Atom "b"])] - printEqns eqs; Equation system: S = (E;=;E + [;S;]) E = ([;E;*;E;] + a + b)The sets of reduced left contexts of E in sentence forms of the component languages are defined by the right-linear grammar:- printEqns (leftcontexts eqs (Var 1)); Equation system: x2 = ([;x2 + x3 + E;=;x3) x3 = (1 + [;x3 + [;E;*;x3) - printEqns (solveRightlinearEqns (leftcontexts eqs (Var 1))) Equation system: x2 = ([)*;(E;= + 1);([;E;* + [)* x3 = ([;E;* + [)*