Many times, we need to repeatedly generate an element, or in general, have a way to control the number times one or more elements get generated. iLangGen supports all the standard cases:
– Zero or once
– Zero or more times (unbounded)
– Zero or more times (bounded)
– One or more times (unbounded)
– One or more times (bounded)
– A fixed number of times
In today’s post, we shall see how these repetition options are specified in iLangGen rules.
Let us start with Zero or once. The following grammar generates a Noun Phrase followed by a Verb Phrase and optionally followed by an Adverb.
We use a convenient print function to print the generated output.
The ? operator is used on the RHS of the grammar to denote that the next element is to be generated once or not at all. This is what you get when you run the grammar.
The next operator 0-to-n allows us to specify the upper bound for the repetition; the lower bound is zero. This means, the succeeding element can appear zero or more times, but not more than N.
The above use case shows a pattern that is useful while generating test cases for programming language processors. Here, we generate the signature of a C function that takes zero to two arguments.
The following is the output generated by this grammar.
The operator 1-to-n is almost identical except for the lower bound; the succeeding element will be emitted at least one, but no more than N times.
The operator 0+ repeats the next element zero or more times, without uppper bound. Likewise 1+ repeats the element one or more times, no upper bound. In these cases, we usually bind special handlers to the generator to take over termination. Later in this series, I will show how we can do this.
Another useful operator is rpt. This causes the following element to be repeated a fixed number of times, say N.
Here is a grammar that uses this operator:
This is a minor variant of the C function example. In this case, we generate a fixed number of arguments (2 to be precise) to the function.
Here is the generated ouput:
To summarize, iLangGen supports different types of repetition through the following operators:
? => Optional (zero or one)
0-to-n => zero to N times, where N is specified
1-to-n => one to N times, where N is specified
rpt => Repeat N times, where N is specified
0+ => Zero or more times (no upper limit specified)
1+ => One or more times (no upper limit specified)
In the next post, we shall look at some more interesting features of iLangGen.
Good day!
Recent Comments