The data received by Mercury is in string format, and the immediate task necessary is to break it into a list of words. The string package from the Mercury library has a set of functions for doing just that.
The initial parsing algorithm used was the simple recursive descent one, as it was fast and simple to implement. See Appendix B for the Mercury implementation of a recursive descent parser.
As the requirements stated that the best possible parsing algorithm should be implemented and the recursive descent parser is at the bottom of the scale, it needed to be re-implemented. The recursive descent parser was not easy to read, as the grammar is stored as the predicates themselves, making it very implementation specific.
Instead a left-corner parser was introduced (see Appendix C for the Mercury code). This combines the advantages of top-down and bottom-up parsing as shown in Chapter 2.
However, in both cases it was not as straight forward as re-implementing standard Prolog code, mainly because of the issue with partially instantiated terms (see Section 4.1.2.3).
The semantics string in each case was to be in the form shown in Figure 4.1. That is to say the first word is the grammar from which the command is taken, the second 'word' being a digit that represents the number of arguments to the function call (the third word). The remained of the string is the arguments to be passed to the specified function.