Lably
Skip to content

Controlled grammar

  • 17 min read
  • 3 chapters

Related on Lably

Through Grammar, Context-free grammar and Tuple

Control by prescribed sequences

Grammars with prescribed sequences are grammars in which the sequence of rule application is constrained in some way. There are four different versions of prescribed sequence grammars: language controlled grammars (often called just controlled grammars), matrix grammars, vector grammars, and programmed grammars.

In the standard context-free grammar formalism, a grammar itself is viewed as a 4-tuple, G=(N,T,S,P), where N is a set of non-terminal/phrasal symbols, T is a disjoint set of terminal/word symbols, S is a specially designated start symbol chosen from N, and P is a set of production rules like X→α, where X is some member of N, and α is some member of (N∪T)∗.

Productions over such a grammar are sequences of rules in P that, when applied in order of the sequence, lead to a terminal string. That is, one can view the set of imaginable derivations in G as the set {p1p2...pn:n≥0}, and the language of G as being the set of terminal strings L(G)={w∈T∗:S⇒p1...⇒pnw}. Control grammars take seriously this definition of the language generated by a grammar, concretizing the set-of-derivations as an aspect of the grammar. Thus, a prescribed sequence controlled grammar is at least approximately a 5-tuple G=(N,T,S,P,R) where everything except R is the same as in a CFG, and R is an infinite set of valid derivation sequences p1p2...pn.

The set R, due to its infinitude, is almost always (though not necessarily) described via some more convenient mechanism, such as a grammar (as in language controlled grammars), or a set of matrices or vectors (as in matrix and vector grammars). The different variations of prescribed sequence grammars thus differ by how the sequence of derivations is defined on top of the context-free base. Because matrix grammars and vector grammars are essentially special cases of language controlled grammars, examples of the former two will not be provided below.

Language controlled grammars

Language controlled grammars are grammars in which the production sequences constitute a well-defined language of arbitrary nature, usually though not necessarily regular, over a set of (again usually though not necessarily) context-free production rules. They also often have a sixth set in the grammar tuple, making it G=(N,T,S,P,R,F), where F is a set of productions that are allowed to apply vacuously. This version of language controlled grammars, ones with what is called "appearance checking", is the one henceforth.

Proof-theoretic description

We let a regularly controlled context-free grammar with appearance checking be a 6-tuple G=(N,T,S,P,R,F) where N, T, S, and P are defined as in CFGs, R is a subset of P* constituting a regular language over P, and F is some subset of P. We then define the immediately derives relation ⇒pi as follows:

Given some strings x and y, both in (N∪T)∗, and some rule p=A→w∈P,

x⇒pacy

holds if either

x=x1Ax2 and y=y1wy2, or
x=y and p∈F

Intuitively, this simply spells out that a rule can apply to a string if the rule's left-hand-side appears in that string, or if the rule is in the set of "vacuously applicable" rules which can "apply" to a string without changing anything. This requirement that the non-vacuously applicable rules must apply is the appearance checking aspect of such a grammar. The language for this kind of grammar is then simply set of terminal strings L(G)={w∈T∗:S⇒p1acw1⇒p2acw2⇒p3ac...⇒pnacw,forsomep1p2...pn∈R}.

Example

Consider a simple (though not the simplest) context-free grammar that generates the language {an:n≥1}:

Let G=({S,A,X},{a},S,{f,g,h,k,l}), where

f:S→AA
g:S→X
h:A→S
k:A→X
l:S→a

In language controlled form, this grammar is simply G′=({S,A,X},{a},S,{f,g,h,k,l},(f|g|h|k|l)∗,{f,g,h,k,l}) (where (f|g|h|k|l)∗ is a regular expression denoting the set of all sequences of production rules). A simple modification to this grammar, changing is control sequence set R into the set (f∗gh∗k)∗l∗, and changing its vacuous rule set F to {g,k}, yields a grammar which generates the non-CF language {a2n:n≥0}. To see how, consider the general case of some string with n instances of S in it, i.e. Sn (the special case S1 trivially derives the string a which is a20, an uninteresting fact).

If we chose some arbitrary production sequence fughvk..., we can consider three possibilities: n=u, n<u, and n>u When n=u we rewrite all n instances of S as AA, by applying rule f to the string u times, and proceed to apply g, which applies vacuously (by virtue of being in F) . When n<u, we rewrite all n instances of S as AA, and then try to perform the n+1 rewrite using rule f, but this fails because there are no more Ss to rewrite, and f is not in F and so cannot apply vacuously, thus when n<u, the derivation fails. Lastly, then n>u, we rewrite u instances of S, leaving at least one instance of S to be rewritten by the subsequent application of g, rewriting S as X. Given that no rule of this grammar ever rewrites X, such a derivation is destined to never produce a terminal string. Thus only derivations with n=u will ever successfully rewrite the string Sn. Similar reasoning holds of the number of As and v. In general, then, we can say that the only valid derivations have the structure Sn⇒f...⇒fA2n⇒gA2n⇒h...⇒hS2n⇒kS2n will produce terminal strings of the grammar. The X rules, combined with the structure of the control, essentially force all Ss to be rewritten as AAs prior to any As being rewritten as Ss, which again is forced to happen prior to all still later iterations over the S-to-AA cycle. Finally, the Ss are rewritten as as. In this way, the number of Ss doubles each for each instantiation of f8gh∗k that appears in a terminal-deriving sequence.

Choosing two random non-terminal deriving sequences, and one terminal-deriving one, we can see this in work:

Let s1=ffghkll, then we get the failed derivation:

S⇒facAA⇒facfailure: f cannot apply, no S to rewrite

Let s2=fghhhkll, then we get the failed derivation:

S⇒facAA⇒gacAA⇒hacSA⇒hacSS⇒hacfailure: h cannot apply, no A to rewrite

Let s3=fghhkll, then we get the successful derivation:

S⇒facAA⇒gacAA⇒hacSA⇒hacSS⇒kacSS⇒lacaS⇒lacaa

Similar derivations with a second cycle of f∗gh∗k produce only SSSS. Showing only the (continued) successful derivation:

...⇒SS⇒facAAS⇒facAAAA⇒gacAAAA
⇒hacSAAA⇒hacSSAA⇒hacSSSA⇒hacSSSS⇒kacSSSS
⇒lacaSSS⇒lacaaSS⇒lacaaaS⇒lacaaaa

Matrix grammars

Matrix grammars (expanded on in their own article) are a special case of regular controlled context-free grammars, in which the production sequence language is of the form (m1|m2|...|mn)∗, where each "matrix" mi is a single sequence. For convenience, such a grammar is not represented with a grammar over P, but rather with just a set of the matrices in place of both the language and the production rules. Thus, a matrix grammar is the 5-tuple G=(N,T,M,S,F), where N, T, S, and F are defined essentially as previously done (with F a subset of M this time), and M is a set of matrices mi=pi,1pi,2...pi,ni where each pi,j is a context-free production rule.

The derives relation in a matrix grammar is thus defined simply as:

Given some strings x and y, both in (N∪T)∗, and some matrix m=p1p2...pn∈M,

x⇒macy

holds if either

x=x1Ax2, y=y1wy2, and A⇒p1acw1⇒p2acw2⇒p3ac...⇒pnacw, or
x=y and m∈F

Informally, a matrix grammar is simply a grammar in which during each rewriting cycle, a particular sequence of rewrite operations must be performed, rather than just a single rewrite operation, i.e. one rule "triggers" a cascade of other rules. Similar phenomena can be performed in the standard context-sensitive idiom, as done in rule-based phonology and earlier Transformational grammar, by what are known as "feeding" rules, which alter a derivation in such a way as to provide the environment for a non-optional rule that immediately follows it.

Vector grammars

Vector grammars are closely related to matrix grammars, and in fact can be seen as a special class of matrix grammars, in which if m∈M, then so are all of its permutations p(m). For convenience, however, we will define vector grammars as follows: a vector grammar is a 5-tuple G=(N,T,M,S,F), where N, T, and F are defined previously (F being a subset of M again), and where M is a set of vectors mi={p1,p2,...,pn}, each vector being a set of context free rules.

The derives relation in a vector grammar is then:

Given some strings x and y, both in (N∪T)∗, and some matrix m={p1,p2,...,pn}∈M,

x⇒macy

holds if either

x=x1Ax2, y=y1wy2, and A⇒pi1acw1⇒pi2acw2⇒pi3ac...⇒pinacw, where m={pi1,pi2,...,pin}, or
x=y and m∈F

Notice that the number of production rules used in the derivation sequence, n, is the same as the number of production rules in the vector. Informally, then, a vector grammar is one in which a set of productions is applied, each production applied exactly once, in arbitrary order, to derive one string from another. Thus vector grammars are almost identical to matrix grammars, minus the restriction on the order in which the productions must occur during each cycle of rule application.

Programmed grammars

Programmed grammars are relatively simple extensions to context-free grammars with rule-by-rule control of the derivation. A programmed grammar is a 4-tuple G=(N,T,S,P), where N, T, and S are as in a context-free grammar, and P is a set of tuples (p,σ,ϕ), where p is a context-free production rule, σ is a subset of P (called the success field), and ϕ is a subset of P (called the failure field). If the failure field of every rule in P is empty, the grammar lacks appearance checking, and if at least one failure field is not empty, the grammar has appearance checking. The derivation relation on a programmed grammar is defined as follows:

Given two strings x,y∈(N∪T)∗, and some rule p=(A→w,σ,ϕ)∈P,

x⇒py and x=x′Ax″,y=x′wx″, or
x=y and A does not appear in x.

The language of a programmed grammar G is defined by constraining the derivation rule-wise, as L(G)={w∈(N∪T)∗:S⇒p1w1⇒p2...⇒pnw}, where for each pi=(Ai→vi,σi,ϕi), either wi−1=xi−1Axi−1′,wi=xi−1vixi−1′,andpi+1∈σi or wi−1=wi,pi+1∈ϕi.

Intuitively, when applying a rule p in a programmed grammar, the rule can either succeed at rewriting a symbol in the string, in which case the subsequent rule must be in ps success field, or the rule can fail to rewrite a symbol (thus applying vacuously), in which case the subsequent rule must be in ps failure field. The choice of which rule to apply to the start string is arbitrary, unlike in a language controlled grammar, but once a choice is made the rules that can be applied after it constrain the sequence of rules from that point on.

Example

As with so many controlled grammars, programmed grammars can generate the language {a2n:n≥0}:

Let G=({S,A},{a},S,{r1,r2,r3}), where

r1=(S→AA,{r1},{r2})
r2=(A→S,{r2},{r1,r3})
r3=(S→a,{r3},∅)

The derivation for the string aaaa is as follows:

S⇒r1AA⇒r1AA⇒r2SA⇒r2SS⇒r2SS
⇒r1AAS⇒r1AAAA⇒r1AAAA
⇒r2SAAA⇒r2SSAA⇒r2SSSA⇒r2SSSS⇒r2SSSS
⇒r3aSSS⇒r3aaSS⇒r3aaaS⇒r3aaaa⇒r3aaaa

As can be seen from the derivation and the rules, each time r1 and r2 succeed, they feed back to themselves, which forces each rule to continue to rewrite the string over and over until it can do so no more. Upon failing, the derivation can switch to a different rule. In the case of r1, that means rewriting all Ss as AAs, then switching to r2. In the case of r2, it means rewriting all As as Ss, then switching either to r1, which will lead to doubling the number of Ss produced, or to r3 which converts the Ss to as then halts the derivation. Each cycle through r1 then r2 therefore either doubles the initial number of Ss, or converts the Ss to as. The trivial case of generating a, in case it is difficult to see, simply involves vacuously applying r1, thus jumping straight to r2 which also vacuously applies, then jumping to r3 which produces a.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in From Lably lessons