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

  1. /*
  2. _______________________________________________________________________________
  3. Debug mode of LR parsing.
  4. Serge Kovalyov, March 1993.
  5. _______________________________________________________________________________
  6. */
  7.  
  8. #ifndef LR_BP_INC
  9. #define LR_BP_INC
  10.  
  11. #include "lr.inc"
  12.  
  13. /*
  14. _______________________________________________________________________________
  15. Types
  16. _______________________________________________________________________________
  17. */
  18.  
  19. // Breakpoint state constants
  20.  
  21. typedef enum
  22.  {bp_default,
  23.   bp_shift_primary,
  24.   bp_reduction_primary,
  25.   bp_reduction_primary_n,
  26.   bp_shift_secondary,
  27.   bp_reduction_secondary,
  28.   bp_reduction_secondary_n
  29.  } LR_BPTS;
  30.  
  31. // Breakpoint set
  32.  
  33. typedef struct LR_BP
  34.  {LR2   *lr;                        // Condensed grammar
  35.   char  *text;                      // Text to be parsed
  36.   char  *file;                      // Name of file containing text
  37.   LR_P  *lp;                        // Parse results
  38.  
  39.   // Array of breakpoints
  40.  
  41.   long   bpts;                      // Number of breakpoints
  42.   long   bptc;                      // Work breakpoint counter
  43.   struct LR_BPT *bpt_start;         // Start breakpoint handler
  44.   struct LR_BPT *bpt_eof;           // End of text breakpoint handler
  45.   struct LR_BPT *bpt_err;           // Error breakpoint handler
  46.   struct LR_BPT *bpt_set;           // Pointer to an array of breakpoints
  47.  
  48.   // User semantics
  49.  
  50.   void (*cmd)();                    // User-defined semantics for all active breakpoints
  51.   void  *user;                      // User-defined pointer (for breakpoint semantics)
  52.  } LR_BP;
  53.  
  54. // Breakpoint handler
  55.  
  56. typedef struct LR_BPT
  57.  {LR_BP *bp;                        // Breakpoint set to which this breakpoint belongs
  58.  
  59.   // User semantics
  60.  
  61.   LR_PT *pt;                        // Parse tree node on which break occurs
  62.   void (*cmd)();                    // Semantic routine to be executed at a break
  63.  
  64.   // Break control flags
  65.  
  66.   unsigned  isactive  : 1;          // Whether the breakpoint is active
  67.   unsigned  stop_here : 1;          // Whether to return control to caller at a break
  68.   unsigned  isstart   : 1;          // Is start breakpoint
  69.   unsigned  iseof     : 1;          // Is end of text break
  70.   unsigned  iserr     : 1;          // Is error break
  71.  
  72.   // Break context - serves to correctly continue parsing after a break
  73.  
  74.   char     *C;                      // Text parsed from previous break
  75.   char     *c;                      // Text to be parsed after break
  76.   char     *Cc;                     // Pointer to currently located string
  77.   LR2_S    *S;                      // LR automaton state at which break occurs
  78.   LR_BPTS   state;                  // Breakpoint state
  79.   long      n;                      // Breakpoint reduction counter
  80.  } LR_BPT;
  81.  
  82. /*
  83. _______________________________________________________________________________
  84. Macros
  85. _______________________________________________________________________________
  86. */
  87.  
  88. // Set a field of a breakpoint handler
  89.  
  90. #define lr_bp_field(_BP, _n, _field, _value)   ((_BP)->bpt_set[_n]._field = (_value))
  91.  
  92. // Obtain number of a breakpoint
  93.  
  94. #define lr_bp_n(_BPT)                          ((_BPT) - (_BPT)->bp->bpt_set)
  95.  
  96. // Scan breakpoint set (work counter is (_BP)->bptc)
  97.  
  98. #define lr_bp_do(_BP)                          for ((_BP)->bptc = 0; (_BP)->bptc < (_BP)->bpts; (_BP)->bptc++)
  99.  
  100. // Obtain pointer to predefined breakpoint
  101.  
  102. #define lr_bp_bpt(_BP, NAME)                   ((_BP)->bpt_##NAME)
  103.  
  104. // Set/clear breakpoint control fields
  105.  
  106. #define lr_bp_active_on(_BP, _n)           lr_bp_field(_BP, _n, isactive,   1)
  107. #define lr_bp_active_off(_BP, _n)          lr_bp_field(_BP, _n, isactive,   0)
  108. #define lr_bp_stop_on(_BP, _n)             lr_bp_field(_BP, _n, stop_here,  1)
  109. #define lr_bp_stop_off(_BP, _n)            lr_bp_field(_BP, _n, stop_here,  0)
  110. #define lr_bp_start_on(_BP, _n)            lr_bp_field(_BP, _n, isstart,    1)
  111. #define lr_bp_start_off(_BP, _n)           lr_bp_field(_BP, _n, isstart,    0)
  112. #define lr_bp_eof_on(_BP, _n)              lr_bp_field(_BP, _n, iseof,      1)
  113. #define lr_bp_eof_off(_BP, _n)             lr_bp_field(_BP, _n, iseof,      0)
  114. #define lr_bp_error_on(_BP, _n)            lr_bp_field(_BP, _n, iserr,      1)
  115. #define lr_bp_error_off(_BP, _n)           lr_bp_field(_BP, _n, iserr,      0)
  116.  
  117. #define lr_bp_switch_on(_BP, _n)           lr_bp_active_on(_BP, _n),  lr_bp_stop_on(_BP, _n)
  118. #define lr_bp_switch_off(_BP, _n)          lr_bp_active_off(_BP, _n), lr_bp_stop_off(_BP, _n)
  119.  
  120. // Set/clear semantic action
  121. // Note: the prototype for a semantic action is 'void _cmd(LR_BPT *)'
  122.  
  123. #define lr_bp_command_on(_BP, _n, _cmd)    lr_bp_field(_BP, _n, cmd, (void (*)())(_cmd))
  124. #define lr_bp_command_off(_BP, _n)         lr_bp_command_on(_BP, _n, 0)
  125. #define lr_bp_command_active(_BP, _n, _cmd)    lr_bp_command_on(_BP, _n, _cmd), lr_bp_active_on(_BP, _n)
  126. #define lr_bp_command_start(_BP, _cmd)     lr_bp_bpt(_BP, start)->cmd = (void (*)())(_cmd)
  127. #define lr_bp_command_eof(_BP, _cmd)       lr_bp_bpt(_BP, eof)  ->cmd = (void (*)())(_cmd)
  128. #define lr_bp_command_err(_BP, _cmd)       lr_bp_bpt(_BP, err)  ->cmd = (void (*)())(_cmd)
  129.  
  130. // Feed the brekpoint set by semantics; _CMDS is an array of semantic actions (its type is 'void (**)()')
  131.  
  132. #define lr_bp_feed(_BP, _CMDS)             lr_bp_do(_BP) lr_bp_command_on(_BP,     (_BP)->bptc, (void (**)())_CMDS+(_BP)->bptc)
  133. #define lr_bp_feed_active(_BP, _CMDS)      lr_bp_do(_BP) lr_bp_command_active(_BP, (_BP)->bptc, (void (**)())_CMDS+(_BP)->bptc)
  134.  
  135. // Register a global breakpoint semantic action
  136.  
  137. #define lr_bp_register_command(_BP, _cmd)  {(_BP)->cmd = (void (*)())(_cmd);                  \
  138.                                             lr_bp_do(_BP) lr_bp_active_on(_BP, (_BP)->bptc);  \
  139.                                            }
  140.  
  141. // Go parsing from a break to specified breakpoint
  142.  
  143. #define lr_bp_go(_BPT_START, _n)           while(((_BPT_START) = lr_bp_next(_BPT_START)) && lr_bp_n(_BPT_START) != (_n) && !(_BPT_START)->iseof && !(_BPT_START)->iserr)
  144.  
  145. /*
  146. _______________________________________________________________________________
  147. Function prototypes
  148. _______________________________________________________________________________
  149. */
  150.  
  151. /*
  152. _______________________________________________________________________________
  153. Initialize new breakpoint set by condensed grammar file
  154. Argument:   grammar2 - name of file containing condensed grammar
  155. Return:     pointer to new breakpoint set
  156.             0 if file not found
  157. _______________________________________________________________________________
  158. */
  159.  
  160. LR_BP *lr_bp_init(char *grammar2);
  161.  
  162. /*
  163. _______________________________________________________________________________
  164. Create new breakpoint set correspondent to a condensed grammar
  165. Argument:   lr - condensed LR grammar (parse table)
  166. Return:     pointer to new breakpoint set
  167. Note:       by default, no breakpoint has a semantics; predefined breakpoints
  168.             bpt_start, bpt_eof and bpt_err are active; bpt_eof and bpt_err
  169.             are stop breakpoints
  170. _______________________________________________________________________________
  171. */
  172.  
  173. LR_BP *lr_bp_create(LR2 *lr);
  174.  
  175. /*
  176. _______________________________________________________________________________
  177. Start parsing for a given text; stop on first active stop breakpoint
  178. Arguments:  bp    - pointer to breakpoint set
  179.             text  - null terminated charcter string of text to be parsed
  180. Return:     pointer to first breakpoint encountered
  181. Note:       if there were the results of previous parsing (field bp->lp is
  182.             nonzero), the function at first releases them
  183. _______________________________________________________________________________
  184. */
  185.  
  186. LR_BPT *lr_bp_start(LR_BP *bp, char *text);
  187.  
  188. /*
  189. _______________________________________________________________________________
  190. Perform a single reduction considering breakpoint(s)
  191. Arguments:  bp    - pointer to breakpoint set
  192.             S     - address of pointer to current LR state
  193.             C     - pointer to text at which reduction starts
  194.             c     - pointer to text at which reduction stops
  195.             sc    - text parsed from the previous break
  196.             s     - breakpoint state
  197.             n     - breakpoint reduction counter
  198. Return:     pointer to encountered breakpoint
  199.             0 if reduction doesn't lead to break (correspondent breakpoint is
  200.             inactive)
  201. _______________________________________________________________________________
  202. */
  203.  
  204. LR_BPT *lr_bp_reduce(LR_BP *bp, LR2_S **S, char **C, char **c, char *sc, LR_BPTS s, long n);
  205.  
  206. /*
  207. _______________________________________________________________________________
  208. Go parsing from a breakpoint until next break
  209. Argument:   bpt   - pointer to start breakpoint
  210. Return:     pointer to next breakpoint encountered
  211. _______________________________________________________________________________
  212. */
  213.  
  214. LR_BPT *lr_bp_next(LR_BPT *bpt);
  215.  
  216. /*
  217. _______________________________________________________________________________
  218. Free a breakpoint set
  219. Argument:   bp    - pointer to breakpoint set
  220. Return:     nothing
  221. _______________________________________________________________________________
  222. */
  223.  
  224. void lr_bp_free(LR_BP *bp);
  225.  
  226. /*
  227. _______________________________________________________________________________
  228. Default error breakpoint semantics - print parse errors to GRAMMAR.ERR file
  229. Argument:   bpt   - pointer to error breakpoint handler
  230. Return:     nothing
  231. Note:       this routine is recommended to be used in run-time semantics,
  232.             e.g.:
  233.                     lr_bp_command_err(bp, lr_bp_error);
  234. _______________________________________________________________________________
  235. */
  236.  
  237. void lr_bp_error(LR_BPT *bpt);
  238.  
  239. /*
  240. ______________________________________________________________________________
  241. Perform LR parse (override combination lr2_open()+lr_parse_open())
  242. Arguments:  grammar2    - .LRS file with condensed grammar
  243.             text        - text to be parsed
  244.             file        - name of file containing text
  245. Return:     pointer to parse results
  246.             0 if condensed grammar was not found
  247. ______________________________________________________________________________
  248. */
  249.  
  250. LR_P *lr_bp_parse_open(char *grammar2, char *text, char *file);
  251.  
  252. /*
  253. ______________________________________________________________________________
  254. Perform LR parse with run-time semantics
  255. Arguments:  p           - address of pointer to parse results
  256.             cmd         - array of run-time semantic fuctions
  257.             lr          - condensed grammar
  258.             text        - text to be parsed
  259.             sysp        - pointer used to pass arguments to run-time semantics
  260. Return:     nothing
  261. ______________________________________________________________________________
  262. */
  263.  
  264. void lr_bp_parse(LR_P **p, void (**cmd)(), LR2 *lr, char *text, void *sysp);
  265.  
  266. #endif
  267.