Button for 1977 Button for 1984 Button for 1990 Button for 1995 Button for MDC Button for notes Button for examples

Example

1984 Version of ANSI Standard

1.2 Example

A simple arithmetic expression, sum, can be defined as follows.

sum P[+ sum]

The primitive symbols are P and +,

This example can be used to illustrate recursion and the technique for scanning text with a transition diagram. The definition above forces a right-to-left order of evaluation. Because this definition is recursive, a "Stack" is needed to save intermediate results whenever sum reinvokes itself. Items are saved onto this Stack by a PUT operation, and are retrieved by a GET operation, in a last-in-first-out order. The transition diagram for the definition of sum above is shown below.

  1. Place value of P into A, that is, P Right Arrow A.
  2. A + Result Right Arrow Result.
  3. A Right Arrow Result.

In order to illustrate the effect of a diagram’s structure on the order of evaulation, this example can be tested on the input string P+Q+R eol , where Q and R are separate occurrences of the symbol P, and eol is the string termination character. The steps below interpret this string using the above diagram, in the same fashion as the algorithm on the previous page uses the MUMPS diagrams in Section 2.

  1. The window is initially positioned at the first character of the string, P.
  2. Path Ea contains the same symbol as in the window (P); consequently, the path to node a is traversed and the window is moved to the next character of the input string.
  3. Action 1 is executed. The value of P is placed in A0. (A0 is the zero-level occurrence of temporary variable A.)
  4. Path ab contains the same symbol as in the window (+), causing transversal of the path to node b, and moving the window to the next character of the input string.
  5. Path bX is a call to sum. PUT Ao on the Stack and start at node E, now at level 1.
  6. Path Ea contains the same symbol as in the window (Q=P), so traverse the path to node a, moving the window to the next input character.
  7. Action 1 is executed, placing the value of Q in A1.
  8. Path ab contains the same symbol as in the window (+), so traverse the path to node b, and move the window to the next character.
  9. Path bX is again a call to sum. PUT Al on the Stack and start at node E, now at level 2.
  10. As before, path Ea is traversed and the value of R is placed in A2.
  11. The default blank path aX is now traversed because there is an end-of-line symbol in the window, which is not the same as a +.
  12. Action 3 is executed, which places the value in A2 (i.e., R) into Result.
  13. The second call to sum is now complete, so effectively path bX can now be traversed at level 1.
  14. Action 2 is executed, by first performing a GET Al from the Stack, then forming Al + Result (which is Q + R), and finally placing the sum into Result.
  15. The first call to sum is now successful, so path bX can now be traversed at level 0.
  16. As above, action 2 is executed, doing a GET Ao from the Stack, forming the sum Ao + Result (which is P + Q + R), and placing this value into Result.
  17. The variable Result now contains the value obtained from scanning the input string P+Q+R eol. This value can in turn be used in another diagram which invoked the sum diagram, much as Result was used in the recursive calls of sum above.
Button for 1977 Button for 1984 Button for 1990 Button for 1995 Button for MDC Button for notes Button for examples

Copyright © Standard Documents; 1977-2024 MUMPS Development Committee;
Copyright © Examples: 1995-2024 Ed de Moel;
Copyright © Annotations: 2003-2008 Jacquard Systems Research
Copyright © Annotations: 2008-2024 Ed de Moel.

This page most recently updated on 15-Nov-2023, 18:58:52.

For comments, contact Ed de Moel (demoel@jacquardsystems.com)