While programmes are inductively defined as follows:
xi := xj + c,xi += iandprint xare while programm.- If P1 and P2 are while programms, then
P1;P2is a while programm. - If P1 is a while programm, then
while xi != 0 do P1 endis a while programm.
Derived from this inductive definition, a context-free grammar for while-programmes is the following:
G = (N, A, P, programm) with
N = {programm, operator, i},
A = {-, +, :=, ;, while, do, end, x, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, print},
P = {
programm -> xi = xi operator i | xi += i | programm;programm | while xi != 0 do programm end | print xi;
i -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 1i | 2i | 3i | 4i | 5i | 6i | 7i | 8i | 9i;
operator -> + | -
}
