Using Function Calls in the Grammar

Written by on October 14, 2017 in LISP, Natural Language Processing, Programming with 0 Comments

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.

Direct Function Call in Grammar

Direct Function Call in Grammar

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.

Direct Function Definition

Direct Function Definition

The output generated by this grammar is this:

Grammar Output

Grammar Output

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.

Indirect Function Call Grammar

Indirect Function Call Grammar

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.

Indirect Function

Indirect Function Definition

Here is the output from this grammar:

Grammar Output

Grammar Output

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!

Tags: ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top