Renumbering and remapping conditional jumps

If the grammar has been successfully compiled once, grammar and rule indexes have been inserted in the leftmost part of each rule. This part serves as an identification header. If you need to displace a rule, cut and paste it along with its identification header without changing its arguments . When recompiling the grammar, BP2 will renumber rules and subgrammars, and eventually modify identification headers and arguments in _goto and _failed procedures. Consider for example grammar "-gr.tryGOTO":

-ho.abc
-se.tryGOTO
[This grammar generates a string containing...]
[... not more than six 'c' and an equal...]
[... number of 'a' and 'b'.]

ORD
GRAM#1[1] S --> X S _repeat(K1=12) _goto(1,2)
GRAM#1[2] S --> lambda _goto(2,0)
----------
RND
_print
GRAM#2[1] <5-1> X --> C
GRAM#2[2] <1> X --> C _goto(3,1)
----------
RND
GRAM#3[1] X --> A _failed(4,0) _goto(3,2)
GRAM#3[2] X --> B _failed(3,3) _goto(3,1)
GRAM#3[3] _print ? --> ? B _goto(4,0) _print _stop
----------
SUB
GRAM#4[1] A --> a
GRAM#4[2] B --> b
GRAM#4[3] C --> c

Suppose that rule [3] of subgrammar #3 is displaced as follows:

----------
RND
GRAM#3[3] _print ? --> ? B _goto(4,0) _print _stop
GRAM#3[1] X --> A _failed(4,0) _goto(3,2)
GRAM#3[2] X --> B _failed(3,3) _goto(3,1)
----------

After recompiling subgrammar #3 will be:

----------
RND
GRAM#3[1] _print ? --> ? B _goto(4,0) _print _stop
GRAM#3[2] X --> A _failed(4,0) _goto(3,3)
GRAM#3[3] X --> B _failed(3,1) _goto(3,2)
----------

Let us now study "-gr.tryGOTO". The first subgrammar is an "ORD" grammar therefore rules are tried in order. Rule [1] will be applied 12 times, then rule [2] will be applied so that 'S' will be erased. (Since it is controlled by K1 the number of repetitions may be later modified by a MIDI device.) The workstring will then be displayed because there is a "_print" on top of subgrammar #2. Then rule [1] or [2] of subgrammar #2 will be applied, with a strong preference for [1] which has an initial weight <5>. Whenever rule [2] is applied BP2 will jump to rule [1] of subgrammar #3. If the rule is not candidate, the "_failed" procedure will force it to jump to subgrammar 4. Otherwise, rules [1] and [2] will be applied alternately. If rule [2] of subgrammar #3 is no longer candidate, rule [3] will be applied once (producing an extra 'b'), then "_goto(4,0)" will force a jump to subgrammar #4. The rest needs no explanation. It is easy to check that the numbers of 'a' and 'b' will eventually be equal.

Indeed, this is an extreme case in which there is so much procedural control that the grammar appears almost like a program (in any procedural language). Nevertheless, it demonstrates BP2's ability to combine procedural with declarative knowledge.