Flags in grammars

Flags may be used in gram­mars to activate/deactivate rules accord­ing to sim­ple numer­ic and log­ic evaluations.

Let us look at the ‘-gr.tryFlags’ gram­mar:

-ho.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 gram­mar cre­ates a string of 20 ter­mi­nal sym­bols (Num_total) con­tain­ing an equal num­ber of (ran­dom­ly posi­tioned) ‘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 gram­mar rule, flags are sur­round­ed with ‘/’. The first occur­rence of a flag nor­mal­ly sets its ini­tial val­ue (an inte­ger num­ber), for instance /Num_total = 20/.

Additive/substractive oper­a­tions (on inte­gers) can then be per­formed to decrease or decrease the val­ues of flags, e.g. /Num_b + 1/ or /Num_a - 1/.

Flags appear­ing before the left argu­ment of a rule are eval­u­at­ed and used to con­trol the rule. For instance,

/myflag/ X --> Y

will only be a can­di­date rule if ‘myflag’ is strict­ly pos­i­tive. This eval­u­a­tion may also be a con­trol of the val­ues of two flags. For instance, rule:

/flag1 > flag2/ /flag3 = flag2/ /flag4 = 50/ X --> Y

will only stay can­di­date as long as the three con­di­tions are met.

This tech­nique may be com­bined with oth­er con­trol pro­ce­dures, such as (positive/negative, proximate/remote, left/right) con­texts, rule weights etc. An exam­ple of using flags is found in “-gr.trial.mohanam”, com­bined with rule weights and pat­tern con­texts. Read page Computing ‘ideas’.

Note that oper­a­tors ‘≤’, ‘≥’ and ‘≠’ are not yet accept­ed in the cur­rent ver­sion of BP3 as it does not han­dle multi­byte Unicode characters.