Flags may be used in grammars to activate/deactivate rules according to simple numeric and logic evaluations.
Let us look at the ‘-gr.tryFlags’ grammar:
// First create string of ‘a’
gram#1 S --> X /Num_total = 20/
gram#1 /Num_total - 1/ X --> a X
// Create flags counting 'a' and 'b'
gram#2 X --> lambda /Num_a = 20/ /Num_b = 0/
// Now replace 'a' with 'b' until they are in equal numbers
gram#3 /Num_a > Num_b/ a --> b /Num_b + 1/ /Num_a - 1/
This grammar creates a string of 20 terminal symbols (Num_total) containing an equal number of (randomly positioned) ‘a’ and ‘b’, for instance:
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 surrounded with ‘/’. The first occurrence of a flag normally sets its initial value (an integer number), for instance /Num_total = 20/.
Additive/substractive operations (on integers) can then be performed to decrease or decrease the values of flags, e.g. /Num_b + 1/ or /Num_a - 1/.
Flags appearing before the left argument of a rule are evaluated and used to control the rule. For instance,
/myflag/ X --> Y
will only be a candidate rule if ‘myflag’ is strictly positive. This evaluation may also be a control of the values of two flags. For instance, rule:
/flag1 > flag2/ /flag3 = flag2/ /flag4 = 50/ X --> Y
will only stay candidate as long as the three conditions are met.
This technique may be combined with other control procedures, such as (positive/negative, proximate/remote, left/right) contexts, rule weights etc. An example of using flags is found in “-gr.trial.mohanam”, combined with rule weights and pattern contexts. Read page Computing ‘ideas’.
➡ Note that operators ‘≤’, ‘≥’ and ‘≠’ are not yet accepted in the current version of BP3 as it does not handle multibyte Unicode characters.