In this section we give a formal definition of the grammars with productions in context-free form and the languages generated by them. We also introduce derivations trees for the grammars.
Definition 11 A two-dimensional grammar with productions in context-free form is a tuple(VN, VT, S0,P), where
• VN is a finite set of non-terminals
• VT is a finite set of terminals
• S0∈VN is the initial non-terminal
• P is a finite set of productions of the formN →W, where N ∈VN and
W ∈(VN ∪VT)∗∗\ {Λ}. In addition,P can containS0→Λ. In this case,
Definition 12 Let G = (VN, VT, S0,P) be a two-dimensional grammar with
productions in context-free form. We define a picture language L(G, N) over VT for everyN ∈VN. The definition is given by the following recurrent rules:
A) If N →W is a production inP andW ∈VT∗∗, thenW is inL(G, N).
B) Let N → [Aij]m,n be a production in P, different to S0 → Λ, and Pij
(i= 1, . . . , n;j= 1, . . . , m) be pictures such that
– if Aij is a terminal, thenPij =Aij
– if Aij is a non-terminal, then Pij ∈L(G, Aij)
Then, ifL[Pij]m,n is defined,L[Pij]m,n is inL(G, N).
The setL(G, N)contains exactly all pictures that can be obtained by applying a finite sequence of rules A) and B). The language L(G) generated by the grammar Gis defined to be the languageL(G, S0).
We abbreviate a two-dimensional grammar with productions in context-free form byCF P G(or byCF P grammar). L(CF P G) is the class of all languages generated by these grammars. Languages in L(CF P G) are called CF P lan- guages. IfP ∈L(G, N), we sayN generates P in G, or shortly,N generatesP
if it is evident from the context, which grammarGwe refer to.
To illustrate the presented definition, we give a simple example of a CF P G
generating the set of all non-empty square pictures over a one-symbol alphabet. Example 7 Let G= (VN, VT, S0,P) be a CF P grammar, where VT = {a},
VN ={V, H, S0}and P contains the following productions:
1) H →a 2) H → a H 3) V →a 4) V → a
V
5) S0→a 6) S0→ Va SH 0
Productions 1), 2) are one-dimensional, thus it should be clear that L(G, H) contains exactly all non-empty rows of a’s – applying rule A) on production 1), we have a∈L(G, H). Furthermore, ifak ∈L(G, H), then rule B) applied on production 2) givesak+1∈L(G, H). Similarly,L(G, V) contains non-empty
columns ofa’s.
Applying ruleA) on production 5), we get thatais generated byG. Since
a∈L(G, S0)∩L(G, H)∩L(G, V), ruleB) applied on production 6) gives that
the square 2×2 is also in L(G, S0). The row, resp. column of length 2 is
generated by H, resp. V, thus rule B) can be applied again to produce the square 3×3, etc. By induction on the size, we can show that each non-empty square picture over{a} can be generated and that there is no way to generate any non-square picture.
Definition 13 Let G= (VN, VT, S0,P) be aCF P G. A derivation tree for G
is every treeT satisfying:
(S0,3×3) ³ ³ ³ ³ ³ ³ ³ ´ ´ ´ ´ ¦ ¦¦ PPPPPPP (a,1×1) (H,1×2) (V,2×1) (S0,2×2) ´ ´ ´ ´ ¦ ¦¦ EEE Q Q QQ LLLHHPPPHHH`````PPPP``````` (a,1×1) (H,1×1) (a,1×1) (V,1×1) (a,1×1) (H,1×1) (V,1×1)(S0,1×1) (a,1×1) (a,1×1) (a,1×1) (a,1×1) (a,1×1) Figure 7.9: Example of a derivation treeT, wherep(T) is the square 3×3 over
{a}. Nodes are represented by assigned labels, edges appear in the lexicograph- ical order which is induced by their labels.
• Each node vof T is labelled by a pair(a, k×l). Ifv is a leaf thena∈VT
andk=l= 1 elsea∈VN and k, l are positive integers.
• Edges are labelled by pairs(i, j), wherei, j∈N+. LetI(v)denote the set
of labels of all edges connecting v with its children. Then, it holds that I(v) ={1, . . . , m} × {1, . . . , n} andm·nis the number of descendants of v.
• Let v be a node of T labelled by (N, k×l), where I(v) = {1, . . . , m} × {1, . . . , n}. Let the edge labelled by(i, j)connectvand its childvij labelled
by (Aij, ki×lj). Then, Pm i=1ki =k, Pn j=1lj =l andN →[Aij]m,n is a production in P.
IfS0→Λ∈ P then the treeTΛ with two nodes – the root labelled by(S0,0×0)
and the leaf labelled by(Λ,0×0)– is a derivation tree forGtoo (the only edge inTΛ is labelled by (1,1)).
LetT be a derivation tree for aCF P grammarG= (VN, VT, S,P) such that
T 6=TΛ, V be the set of its nodes. We assign a picture to each node of T by
defining a functionp:V →VT∗∗as follows: ifv∈V is a leaf labelled by (a,1×1) then p(v) = a else p(v) = L[Pij]m,n, where I(v) = {1, . . . , m} × {1, . . . , n},
Pij =p(vij), vij is the child ofv connected by the edge labelled by (i, j). We also definep(T) to be p(r), whereris the root ofT, moreover,p(TΛ) = Λ.
We can make a simple observation: if v ∈ V is labelled by (N, k×l) then rows(p(v)) =kand cols(p(v)) =l.
Figure 7.9 shows an example of a derivation tree which corresponds to gen- erating the square 3×3 in Example 7.
Lemma 8 Let G= (VN, VT, S,P)be a CF P grammar and N ∈VN.
1. LetT be a derivation tree for G having its root labelled by(N, k×l). Then p(T)∈L(G, N).
2. LetP be a picture in L(G, N). There is a derivation tree forGwith root labelled by (N, k×l)such that rows(P) =k,cols(P) =l andp(T) =P.
Proof. The lemma follows directly from the previous definitions. ut