home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / lr.zip / LRX.INC < prev    next >
Text File  |  1993-05-15  |  12KB  |  248 lines

  1. /*
  2. ______________________________________________________________________________
  3. LR Parse tree navigation macros
  4. Philip R Brenan,  Transcendental Automation,  1992,  800-FOR-PHIL
  5.  
  6. MACRO    DESCRIPTION
  7. n        Obtain number of a non terminal
  8. loc      Add pointer to current non terminal
  9. one      Return single item below current item, or zero otherwise
  10. zero     Initialize new non terminal pointer set
  11. add      Add pointers to the current non terminal and its singleton dependents
  12. all      Add pointers to the current non terminal and all its dependents up to repetition
  13. zap      Zero and add
  14. up       Move up through parse tree until top, or a non terminal pointer would be overlaid
  15. copy     Initialize new non terminal pointer set by copying existing non terminal pointer set
  16. list_do  Process non terminal sub list
  17. integer  Convert non terminal to integer
  18. string   Convert non terminal to string
  19.  
  20. KEYS     DESCRIPTION
  21. KEYS     Non terminal set generated during grammar preparation
  22. P        Parse results
  23. PT       Current non terminal
  24. UNTIL    Move up until end or this expression returns true
  25. FROM     Non terminal set to be copied
  26. L        Work Xl *
  27. T        Work Xt *
  28. Pt       Dependent non terminal
  29. ______________________________________________________________________________
  30. */
  31. #ifndef   LRX_INC
  32. #define   LRX_INC
  33.  
  34. #ifdef    LRX_MAIN
  35. #include <string.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include "m.inc"
  39. #include "lr.inc"
  40. #endif
  41.  
  42. #ifndef   LR_INC
  43. #include "lr.inc"
  44. #endif
  45. /*
  46. ______________________________________________________________________________
  47. Non terminal pointers
  48. ______________________________________________________________________________
  49. */
  50. typedef struct LRX_PT
  51.  {struct LR_PT *l;
  52.   struct LR_P  *p;          // Parse results
  53.   struct LR_PT *key[32];    // Non terminals
  54.  } LRX_PT;
  55.  
  56. /*
  57. ______________________________________________________________________________
  58. Request for services from LRX_REQ.  Fill out this structure and pass to LRX.
  59. ______________________________________________________________________________
  60. */
  61. typedef struct LRX_REQ
  62.  {long    req;                                   // Actions to be performed as per lrx_req_*
  63.   long    rc;                                    // Return code as defined in lrx_req_rc_*
  64.   char   *name;                                  // Name of grammar - only required if you are compiling a grammar
  65.   LR_P   *P;                                     // Parse tree
  66.   LR     *lr;                                    // Grammar compilation results
  67.   LR2    *grammar;                               // Compiled and loaded grammar
  68.   char   *grammar_file;                          // File   containing compiled grammar
  69.   char   *gdl;                                   // String containing source GDL for grammar
  70.   char   *gdl_file;                              // File   containing source GDL for grammar
  71.   char   *text;                                  // Text to be parsed
  72.   char   *text_file;                             // File containing text to be parsed
  73.   void (**pre)();                                // Pointer to Pre commands  <<GRAMMAR>>_cmd.pre  function pointers
  74.   void (**post)();                               // Pointer to Post commands <<GRAMMAR>>_cmd.post function pointers
  75.   void   *nps;                                   // Pointer to Non terminal Pointer Set <<GRAMMAR>>_NPS
  76.   long    stack;                                 // Default parser stack, will be set to 4K unless you set it higher
  77.   void   *sysp;                                  // User parameter to be passed to each pre/post command function invoked
  78.   char    grammar_file_save[256];                // File   containing compiled grammar, saved from compilation
  79.  } LRX_REQ;
  80.  
  81. #define lrx_req_load_gdl              0x00000001 // Load GDL file
  82. #define lrx_req_compile_gdl           0x00000002 // Compile gdl to create grammar file
  83. #define lrx_req_load_grammar          0x00000004 // Load grammar file
  84. #define lrx_req_load_text             0x00000008 // Load text file
  85. #define lrx_req_parse                 0x00000010 // Perform parse
  86. #define lrx_req_free_parse            0x00000020 // Free parse tree
  87. #define lrx_req_free_grammar          0x00000040 // Free grammar
  88. #define lrx_req_free_gdl              0x00000080 // Free GDL
  89. #define lrx_req_free_text             0x00000100 // Free text
  90. #define lrx_req_free_compile          0x00000200 // Free grammar compilation results
  91. #define lrx_req_strip_comments        0x00000400 // Remove *, -- comment lines from input text
  92. #define lrx_req_print_parse           0x00000800 // Print parse tree to <<GRAMMAR>>.PRS
  93. #define lrx_req_rescan                0x00001000 // Rescan parse tree
  94. #define lrx_req_free_all        \
  95.         lrx_req_free_parse    | \
  96.         lrx_req_free_grammar  | \
  97.         lrx_req_free_gdl      | \
  98.         lrx_req_free_text     | \
  99.         lrx_req_free_compile
  100.  
  101. #define lrx_req_rc_ok                          0 // Request performed successfully
  102. #define lrx_req_rc_no_grammar         0x00000001 // No grammar supplied
  103. #define lrx_req_rc_no_grammar_file    0x00000002 // No grammar file supplied
  104. #define lrx_req_rc_bad_grammar_file   0x00000004 // Bad grammar file supplied - errors occurred during read
  105. #define lrx_req_rc_no_gdl_file        0x00000008 // No GDL file supplied
  106. #define lrx_req_rc_bad_gdl_file       0x00000010 // Bad GDL file, errors occurred during read
  107. #define lrx_req_rc_no_gdl             0x00000020 // No GDL text supplied
  108. #define lrx_req_rc_no_text_file       0x00000040 // No input text file to be loaded, parsed, freed
  109. #define lrx_req_rc_bad_text_file      0x00000080 // Bad input text file, errors occurred during read
  110. #define lrx_req_rc_no_text            0x00000100 // No input text to be parsed
  111. #define lrx_req_rc_compile_errors     0x00000200 // Compile errors occurred, check <<GRAMMAR>>.LR
  112. #define lrx_req_rc_parse_errors       0x00000400 // Parse errors occurred,   check <<GRAMMAR>>.ERR
  113. #define lrx_req_rc_no_nps             0x00000800 // No NPS supplied, you should include <<GRAMMAR>>.STR
  114. #define lrx_req_rc_no_action          0x00001000 // No action specified
  115. #define lrx_req_rc_no_name            0x00002000 // No grammar name supplied
  116. #define lrx_req_rc_no_parse_to_free   0x00004000 // No parse to free per request
  117. #define lrx_req_rc_no_text_to_free    0x00008000 // No text to free per request
  118. #define lrx_req_rc_no_grammar_to_free 0x00010000 // No compiled grammar to free per request
  119. #define lrx_req_rc_no_gdl_to_free     0x00020000 // No gdl to free per request
  120. #define lrx_req_rc_no_compile_to_free 0x00040000 // No compilation results to free per request
  121. #define lrx_req_rc_no_parse_tree      0x00080000 // No parse tree available for printing
  122.  
  123. #define lrx_cmd(NAME) void NAME(LRX_REQ *REQ, LR_PT *PT) // Defines a semantuic action procedure
  124. #define lrx_default_stack                           4096 // Default stack size
  125.  
  126. /*
  127. ______________________________________________________________________________
  128. External macros
  129. ______________________________________________________________________________
  130. */
  131. #define lr_do(_P, _L, _pt) x_vector_do((_P)->V, _L, _pt)
  132.  
  133. /*
  134. ______________________________________________________________________________
  135. N
  136. ______________________________________________________________________________
  137. */
  138. #define lrx_n(_PT) ((_PT)->rc->nt->nts)
  139. /*
  140. ______________________________________________________________________________
  141. LOC
  142. ______________________________________________________________________________
  143. */
  144. #define lrx_loc(_KEYS, _PT)                                       \
  145.  {if (_PT)                                                        \
  146.    {LRX_PT *_q = (void *)&(_KEYS);                                \
  147.                                                                   \
  148.     _q->key[lrx_n(_PT)] = (_PT);                                  \
  149.    }                                                              \
  150.  }
  151. /*
  152. ______________________________________________________________________________
  153. ONE
  154. ______________________________________________________________________________
  155. */
  156. #define lrx_one(_PT) (((_PT) && (_PT)->PL.f && (_PT)->PL.f == (_PT)->PL.l) ? (LR_PT *)(_PT)->PL.f->d : (LR_PT *)0)
  157. /*
  158. ______________________________________________________________________________
  159. ZERO
  160. ______________________________________________________________________________
  161. */
  162. #define lrx_zero(_KEYS, _P)                                          \
  163.   (memset((_KEYS).key, 0, (_P)->lr->NTS * sizeof(LR_PT *)),            \
  164.   (_KEYS).p = (_P))
  165. /*
  166. ______________________________________________________________________________
  167. ADD
  168. ______________________________________________________________________________
  169. */
  170. #define lrx_add(_KEYS, _PT) lrx_Add(&(_KEYS), _PT)
  171. /*
  172. ______________________________________________________________________________
  173. ALL
  174. ______________________________________________________________________________
  175. */
  176. #define lrx_all(_KEYS, _PT)  \
  177.  {lrx_add(_KEYS, _PT);       \
  178.   lrx_All(&(_KEYS), _PT);    \
  179.  }
  180. /*
  181. ______________________________________________________________________________
  182. ZAP
  183. ______________________________________________________________________________
  184. */
  185. #define lrx_zap(_KEYS, _P, _PT)                                      \
  186.   lrx_zero((_KEYS), _P);                                             \
  187.   lrx_add ((_KEYS), _PT);
  188. /*
  189. ______________________________________________________________________________
  190. UP
  191. ______________________________________________________________________________
  192. */
  193. #define lrx_up(_KEYS, _PT, _UNTIL)                                   \
  194. if (_PT)                                                             \
  195.  {XL      *_l;                                                       \
  196.   LR_PT   *_p = _PT;                                                 \
  197.   LR_PT *(*_q)[32];                                                  \
  198.                                                                      \
  199.   for(_q = (void *)&(_KEYS).key; _l = _p->Pl.u;)                     \
  200.    {_p = _l->d;                                                      \
  201.    (*_q)[lrx_n(_p)] = _p;                                            \
  202.     if (_UNTIL) break;                                               \
  203.    }                                                                 \
  204.  }
  205. /*
  206. ______________________________________________________________________________
  207. COPY
  208. ______________________________________________________________________________
  209. */
  210. #define lrx_copy(_KEYS, _FROM)                                             \
  211.   (memcpy((_KEYS).key, (_FROM).key, (_FROM).p->lr->NTS * sizeof(LR_PT *)), \
  212.   (_KEYS).p = (_FROM).p)
  213. /*
  214. ______________________________________________________________________________
  215. LIST_DO
  216. ______________________________________________________________________________
  217. */
  218. #define lrx_list_do(_PT, _L, _pt)                                    \
  219.   x_list_do(&(_PT)->PL, _L, _pt)
  220. /*
  221. ______________________________________________________________________________
  222. Integer
  223. ______________________________________________________________________________
  224. */
  225. #define lrx_integer(_pt) atol(_pt->t)
  226. /*
  227. __________________________________________________________________________________________________
  228. String
  229. __________________________________________________________________________________________________
  230. */
  231. #define lrx_string(M, PT) u_strip_quote1(m_alloc_str(M, PT->t))
  232. /*
  233. ______________________________________________________________________________
  234. Procedures
  235. ______________________________________________________________________________
  236. */
  237. void  lrx_parse        (LR_P **p,       void (**cmd)(), void (**cmd2)(), LRX_PT *PT, LR2 *grammar, char *text, long stack, void *sysp);
  238. void  __lrx_parse_scan (LR_P  *P,       void (**cmd)(), void (**cmd2)(), LRX_PT *PT, void *sysp);
  239. void  lrx_parse_scan   (LRX_REQ *r);
  240. void  lrx_Add          (void *KEYS,     struct LR_PT  *PT);
  241. void  lrx_All          (void *KEYS,     struct LR_PT  *PT);
  242.  
  243. char *lrx_concat       (struct M    *m, struct LR_PT  *p);
  244. void  lrx_concat2      (char **c,       struct LR_PT  *p);
  245. char *lrx_concat_space (struct M    *m, struct LR_PT  *p);
  246. void  lrx_concat_space2(char **c,       struct LR_PT  *p);
  247. long  lrx_req          (LRX_REQ *r);
  248. #endif