Flags can be used in grammars to activate/deactivate rules according to simple numerical and logical evaluations.
Let us look at the ‘-gr.tryFlags’ grammar:
-al.abc
// First create string of ‘a’
gram#1[1] S --> X /Num_total = 20/
gram#1[2] /Num_total - 1/ X --> a X
--------
// Create flags counting 'a' and 'b'
gram#2[1] X --> lambda /Num_a = 20/ /Num_b = 0/
--------
// Now replace 'a' with 'b' until they are in equal numbers
gram#3[1] /Num_a > Num_b/ a --> b /Num_b + 1/ /Num_a - 1/
This grammar produces a string of 20 terminal symbols (Num_total) containing an equal number of (randomly positioned) 'a' and 'b'. For example:
b b a a a b a a b a b a b b b a b b a a
In a grammar rule, flags are enclosed in '/'. The first occurrence of a flag usually sets its initial value (an integer number), for example /Num_total = 20/.
Additive/subtractive operations (on integers) can then be performed to increase or decrease the values of the flags, e.g. /Num_b + 1/ or /Num_a - 1/.
Flags that appear before the left argument of a rule are evaluated and used to control the rule. For example,
/myflag/ X --> Y
will only be a candidate rule if 'myflag' is strictly positive. This evaluation can also be a check of the values of two flags. For example, rule:
/flag1 > flag2/ /flag3 = flag2/ /flag4 = 50/ X --> Y
will only remain a candidate as long as the three conditions are met.
This technique can be combined with other control techniques, such as (positive/negative, proximate/remote, left/right) contexts, rule weights etc. An example of the use of flags can be found in "-gr.trial.mohanam", combined with rule weights and pattern contexts. Read the page Computing ‘ideas’.
➡ Note that the operators '≤', '≥' and '≠' are not yet accepted in the current version of BP3 as it does not handle multi-byte Unicode characters.