Programmed
grammars in BP2 use
**flags**
taking integer values. Suppose we need to generate random variations of length
6 containing 'a' and 'b', yet with exactly four occurrences of 'a' and two of
'b'. We may write (see "-gr.tryflags"):

RND

GRAM#1[1]
S
--> X
X X X X X /flag1 = 4/ /flag2 = 2/

GRAM#1[2]
/flag1 -1/ X
--> a

GRAM#1[3]
/flag2 -1/ X
--> b

The
first rule generates "X X X X X X" and six flags. More precisely, "flag1" is
set to integer value 4 whereas "flag2" is set to 2.

These
are used as conditions for the application of rules [2] and [3]: a rule may be
applied only if the values of all its condition flags
are
**strictly
positive
**.
In addition, each time rule [2] (or [3]) is applied, "flag1" (or "flag2") is
decremented by one unit.

Productions
look like:

a
a a a b b, b a a a b a, b a a a b a, a a b a b a, a b a b a a, b a b a a a, a b
a a a b...

Another
example: generate a string of length 10 on alphabet {a,b,c} in which 'b' and
'c' have equal numbers of occurrences (±1). The following grammar

("-gr.tryflags2") will do it:

("-gr.tryflags2") will do it:

[1]
S --> X
X X X X X X X X X /make_b = 1/

[2]
/make_b -1/ X
--> b
/make_c +1/

[3]
/make_c -1/ X
--> c
/make_b +1/

[4]
X --> a

This
grammar produces:

c
a b b b c c a a a, a b a a b a a c c b, b a b c a b b c c a, c a b a a a b a a
c...

We
recommend reading about
**production
procedures**
(reference manual §8.1) that solve equivalent problems.