Flags in grammars

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

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

`-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 numbersgram#3[1] /Num_a > Num_b/ a --> b /Num_b + 1/ /Num_a - 1/`

This gram­mar pro­duces 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 example:

`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 enclosed in ‘/’. The first occur­rence of a flag usu­al­ly sets its ini­tial val­ue (an inte­ger num­ber), for exam­ple /Num_total = 20/.

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

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

`/myflag/ X --> Y`

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

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

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

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

Note that the 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.