Indexed grammar
Indexed grammars are a generalization of context-free grammars in that nonterminals are equipped with lists of flags, or index symbols.
The language produced by an indexed grammar is called an indexed language.
Definition
Modern definition by Hopcroft and Ullman
In contemporary publications following Hopcroft and Ullman,an indexed grammar is formally defined a 5-tuple G = ⟨N,T,F,P,S⟩ where
- N is a set of variables or nonterminal symbols,
- T is a set of terminal symbols,
- F is a set of so-called index symbols, or indices,
- S ∈ N is the start symbol, and
- P is a finite set of productions.
Terminal symbols may not be followed by index stacks.
For an index stack σ ∈ F* and a string α ∈ * of nonterminal and terminal symbols, α denotes the result of attaching to every nonterminal in α; for example if α equals with a,d ∈ T terminal, and nonterminal symbols, then α denotes
Using this notation, each production in P has to be of the form
- A → α,
- A → B, or
- A → α,
Derivations are similar to those in a context-free grammar except for the index stack attached to each nonterminal symbol.
When a production like e.g. A → BC is applied, the index stack of A is copied to both B and C.
Moreover, a rule can push an index symbol onto the stack, or pop its "topmost" index symbol.
Formally, the relation ⇒ is defined on the set * of "sentential forms" as follows:
- If A → α is a production of type 1, then β A γ ⇒ β α γ, using the [|above] definition. That is, the rule's left hand side's index stack φ is copied to each nonterminal of the right hand side.
- If A → B is a production of type 2, then β A γ ⇒ β B γ. That is, the right hand side's index stack is obtained from the left hand side's stack φ by pushing f onto it.
- If A → α is a production of type 3, then β A γ ⇒ β α γ, using again the definition of α. That is, the first index f is popped from the left hand side's stack, which is then distributed to each nonterminal of the right hand side.
The language L = is the set of all strings of terminal symbols derivable from the start symbol.
Original definition by Aho
Historically, the concept of indexed grammars was first introduced by Alfred Aho using a different formalism.Aho defined an indexed grammar to be a 5-tuple where
- N is a finite alphabet of variables or nonterminal symbols
- T is a finite alphabet of terminal symbols
- F ⊆ 2N × * is the finite set of so-called flags
- P ⊆ N × * is the finite set of productions
- S ∈ N is the start symbol
- A production p = from P matches a nonterminal A ∈ N followed by its string of flags ζ ∈ F*. In context, γ Aζ δ, via p, derives to γ X1θ1…Xkθk δ, where θi = ηiζ if Xi was a nonterminal and the empty word otherwise. The old flags of A are therefore copied to each new nonterminal produced by p. Each such production can be simulated by appropriate productions of type 1 and 2 in the Hopcroft/Ullman formalism.
- An index production p = ∈ f matches Afζ and copies the remaining index string ζ to each new nonterminal: γ Afζ δ derives to γ X1θ1…Xkθk δ, where θi is the empty word when Xi is a terminal and ζ when it is a nonterminal. Each such production corresponds to a production of type 3 in the Hopcroft/Ullman formalism.
Examples
In practice, stacks of indices can count and remember what rules were applied and in which order. For example, indexed grammars can describe the context-sensitive language of word triples :A derivation of is then
As another example, the grammar G = ⟨,,, P, S ⟩ produces the language, where the production set P consists of
An example derivation is
Both example languages are not context-free by the pumping lemma.
Properties
and Ullman tend to consider indexed languages as a "natural" class, since they are generated by several formalisms other than indexed grammars, viz.- Aho's one-way nested stack automata
- Fischer's macro grammars
- Greibach's automata with stacks of stacks
- Maibaum's algebraic characterization
Conversely, Gilman gives a "shrinking lemma" for indexed languages.
Linear indexed grammars
has defined a second class, the linear indexed grammars, by requiring that at most one nonterminal in each production be specified as receiving the stack,whereas in an ordinary indexed grammar, all nonterminals receive copies of the stack.
Formally, a linear indexed grammar is defined similar to an ordinary indexed grammar, but the production's form requirements are modified to:
- A → α B β,
- A → α B β,
- A → α B β,
which belongs to the mildly context-sensitive classes.
The language is generable by an indexed grammar, but not by a linear indexed grammar, while both and are generable by a linear indexed grammar.
If both the original and the modified production rules are admitted, the language class remains the indexed languages.
Example
Letting σ denote an arbitrary collection of stack symbols, we can define a grammar for the language L = asTo derive the string abc we have the steps:
Similarly:
Computational power
The linearly indexed languages are a subset of the indexed languages, and thus all LIGs can be recoded as IGs, making the LIGs strictly less powerful than the IGs. A conversion from a LIG to an IG is relatively simple. LIG rules in general look approximately like, modulo the push/pop part of a rewrite rule. The symbols and represent strings of terminal and/or non-terminal symbols, and any non-terminal symbol in either must have an empty stack, by the definition of a LIG. This is, of course, counter to how IGs are defined: in an IG, the non-terminals whose stacks are not being pushed to or popped from must have exactly the same stack as the rewritten non-terminal. Thus, somehow, we need to have non-terminals in and which, despite having non-empty stacks, behave as if they had empty stacks.Consider the rule as an example case. In converting this to an IG, the replacement for must be some that behaves exactly like regardless of what is. To achieve this, we can simply have a pair of rules that takes any where is not empty, and pops symbols from the stack. Then, when the stack is empty, it can be rewritten as.
We can apply this in general to derive an IG from an LIG. So for example if the LIG for the language is as follows:
The sentential rule here is not an IG rule, but using the above conversion algorithm, we can define new rules for, changing the grammar to:
Each rule now fits the definition of an IG, in which all the non-terminals in the right hand side of a rewrite rule receive a copy of the rewritten symbol's stack. The indexed grammars are therefore able to describe all the languages that linearly indexed grammars can describe.
Relation to other formalism
Vijay-Shanker and Weir demonstrates that Linear Indexed Grammars, Combinatory Categorial Grammars, Tree-adjoining Grammars, and Head Grammars all define the same class of string languages.Their formal definition of linear indexed grammars differs from the above.
LIGs are strictly less expressive than the languages generated by another family of weakly equivalent formalism, which include: LCFRS, MCTAG, MCFG and minimalist grammars. The latter family can be parsed in polynomial time.
Distributed index grammars
Another form of indexed grammars, introduced by Staudacher, is the class of Distributed Index grammars. What distinguishes DIGs from Aho's Indexed Grammars is the propagation of indexes. Unlike Aho's IGs, which distribute the whole symbol stack to all non-terminals during a rewrite operation, DIGs divide the stack into substacks and distributes the substacks to selected non-terminals.The general rule schema for a binarily distributing rule of DIG is the form
Where α, β, and γ are arbitrary terminal strings. For a ternarily distributing string:
And so forth for higher numbers of non-terminals in the right hand side of the rewrite rule. In general, if there are m non-terminals in the right hand side of a rewrite rule, the stack is partitioned m ways and distributed amongst the new non-terminals. Notice that there is a special case where a partition is empty, which effectively makes the rule a LIG rule. The Distributed Index languages are therefore a superset of the Linearly Indexed languages.