One of the advanced features of iLangGen is the ability to embed arbitrary function calls on the RHS of grammar rules. Such functions impart interesting dynamic behaviour to the generated data.
For convenience and flexibility, iLangGen supports two types of function calls – Direct and Indirect. In the case of the Direct function call, the function will be called as it is and its return value will be used in the generated data. In the case of Indirect function call, there is a level of indirection. The given function will be called once and it is expected to return another function. It is the returned function that will be used everytime a node in the traversal graph is visited. Let us see both cases.
The following figure shows a simple grammar where the non-terminal Random has a call to function get-random-number on the RHS. You can see that this function is called 5 times to generate 5 data elements. This is the case of Direct call.
The actual function definition appears below. This function is passed 3 arguments when it is called: the node being visited, the output stream and the grammar object. The function can make use of the passed arguments if needed, but in this case, we will keep things simple, where the function merely returns a random number below 1000.
The output generated by this grammar is this:
Here is an example of a grammar where we use Indirect call. The grammar has the same structure as the previous one, but the called function is now passed an argument of 100.
The function definition, shown below, takes the passed argument and returns another function that takes three arguments (as in the Direct call case). The passed argument is bound to the lambda.
Here is the output from this grammar:
As you can see, both types of calls have their uses, the Indirect call providing extra freedom to synthesize the actual behaviour dynamically.
Today’s examples do not use the three arguments passed by the traversal algorithm. In a future post, I will spend some time on what those are and how they can be used to advantage.
That is it for today. Have a great day!
Recent Comments