home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 329_01 / define.doc < prev    next >
Text File  |  1989-10-17  |  4KB  |  83 lines

  1. Define Operating Instructions
  2. (C) October 17  1989  Asaf Arkin
  3. All rights reserved
  4.  
  5.  
  6. Define It
  7.  
  8. A constant feature in Q&A columns are C definitions. Like every thing else about the language, they are laconic and occult, proving difficult to program or comprehend. Some overcome this intricity with aid of a utility -- one that explains C definitions in English you can read. It works fine, if all you intend it is to decipher other's definitions. For creating them, however, an amount of trial and error is involved before you get things right.
  9.  
  10. The treatment I have adopted is of different orientation. The opposite, to be exact: Define constructs definitions from descriptions given to it in plain English. Just tell it what you want, like you write a columnist -- it works the first time.
  11.  
  12.  
  13. How Simple Can It Be
  14.  
  15. Define employs no AI techniques, yet is capable of handling sentences as natural as:
  16.  
  17.      foo is function returning pointer to array of int.
  18.  
  19. which comes out the other way as:
  20.  
  21.      int  (*foo())[];
  22.  
  23. The secret lies in the simplicity of the rules governing the construction of definitions -- they are not nearly as hard as you may think them to be.
  24.  
  25. The first thing Define does with an English description is copy the identifier name (if present) into a malloced memory block. It then proceeds to read the descriptors (pointer, function and array), prefixing and postfixing the identifier with C tokens in accordance; the memory block expands to accommodate the growing definition. Following are the construction rules:
  26.  
  27.      Descriptor            Action
  28.      pointer to            Precede with *
  29.      pointer to function   Precede with *, Enclose definition with (..)
  30.      pointer to array      Precede with *, Enclose definition with (..)
  31.      function returning    Append ()
  32.      array of              Append []
  33.  
  34. Given the previous sentence, Define:
  35.  
  36.      Appends (),              foo()
  37.      Prefixes an *,          *foo()
  38.      Encloses with (..),    (*foo())
  39.      and Appends [].        (*foo())[]
  40.  
  41. The rest of the description is assumed to be the data type: returned, pointed to, or array element. It copies in full to the start of the definition, completing it to:
  42.  
  43.      int (*foo())[];
  44.  
  45. While creating your definitions, Define warns for the following three illegal combinations:
  46.  
  47.     Array of function
  48.     Function returning array
  49. and
  50.     Function returning function
  51.  
  52. As you become familiar with Define, you will develop the habit of taking shortcuts: the is following the identifier name can be dropped; so can to, returning and of; function truncates to just func, returning to return (or nothing), and pointer to poin or simply ptr.
  53.  
  54.  
  55. How Complex Can It Be
  56.  
  57. Define does not suffice in constructing only the complicated parts of your definitions: you can hand it over the most complete definitions, including within the size of arrays and the parameters accepted by functions.
  58.  
  59. To indicate the dimension of an array, follow array with size, eg:
  60.  
  61.      bar is array size 6 of array size 4 of int.
  62.  
  63. to produce:
  64.  
  65.      int bar[6][4];
  66.  
  67. A function's formal parameters you state by proceeding function with accepting (shorthand accept) and a parenthesized list of the parameters. Given:
  68.  
  69.      foo func accept (a ptr int, b array char) return int.
  70.  
  71. Define will return:
  72.  
  73.      int foo(int *a, char b[]);
  74.  
  75. If you program the K&R style, you may consider this capability somewhat redundant. Not so if you follow the ANSI standard -- it demands that you prototype all function definitions and declarations. As well as pointers to functions.
  76.  
  77.  
  78. Conclusion
  79.  
  80. In this article, I have not gone into detail with the internals of Define. It was not intended as a listing to study and hack. Rather, Define is a utility, originated to make life easier for all you programmers struggling with the C programming language. You'll need all the help you can get.
  81.  
  82.  
  83.