Since a number is now defined essentially in terms of a number, it can produce an almost endless string of digits. The number can be a number(2) followed by a digit, but number(2) can be a number(3) followed by a digit, and number(3) can be a number(4) followed by a digit etc. The process needn't go on forever - at any point, one of the "sub-numbers" somewhere down in the stack can be defined as a single digit.
This underlines an important point about any sort of recursion - there must be a stopping condition built in. Suppose the rules above had been defined as follows:
Syntax diagrams work similarly to flowcharts. You travel along lines including symbols, which are listed in rectangles strung along these lines. When there is a choice of symbols to include, this is shown by a branch in the line. Here, for instance, is the grammar from the first section represented as a sequence of syntax diagrams. Start from the word on the left that you are trying to define, work along in the direction of the arrows and don't stop until you reach the end of the line on the right.

When items need to be chained together, they appear end to end along the line. When there is a choice of items, the line branches and you can take any branch. You can also use syntax diagrams to define structures recursively.

Syntax diagrams give an easy way to specify optional symbols. In the following syntax diagram, both symbols X and Y are optional:

This grammar rule could generate the strings X, Y, XY or an empty string (where both X and Y had been skipped). Similarly, syntax diagrams can specify "one or more" constructions, where a symbol must be included at least once, but can be included any number of times after that ...

... or "zero or more" where a symbol can be included as many times as you like or not at all:

![]() Go back |
![]() Main Menu |
![]() Worked examples |
![]() Questions |
![]() Go on |