home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / sampler / 02 / parser / parslib3.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-09-06  |  1.5 KB  |  62 lines

  1. {PARSLIB3.PAS}
  2. {
  3. Description:  Library of parsing support routines used to handle searches
  4.               and stack operations.
  5. Author:       Karl Gerhard
  6. Date:         8/7/87
  7. Application:  IBM PC and compatibles
  8. }
  9.  
  10. {---------------------------------}
  11. Function search_lhs( lhs:stdstr; var rhs:stdstr):boolean;
  12. { search grammar for lhs, return its rhs }
  13. Var
  14. i,p:integer; statok:boolean;
  15. Begin
  16. i := 0;
  17. lhs := lhs + ' '; { enforce exact word match }
  18. repeat
  19.   i := i + 1;
  20.   p := pos( lhs,grammar[i] );
  21.   statok := (p < 3) and (p > 0);
  22. until (i >= gr_length) or statok;
  23. if statok then begin
  24.   rhs := grammar[i];
  25.   p := 2; repeat p := p + 1; until rhs[p] = ' ';
  26.   rhs := copy(rhs,p,250);
  27.   { logging('search_lhs','['+lhs+']'+rhs);{}
  28. end
  29. else begin
  30.   rhs := 'LHS NOT FOUND';
  31.   logging('  IS TERMINAL: ',lhs);
  32. end;
  33. search_lhs := statok;
  34. End;
  35.  
  36.  
  37. {---------------------------------}
  38. Function pop(var stack:stack_type):stdstr;
  39. { return top of stack, at the left of the stack string }
  40. Var
  41. ps:integer;  s:stdstr;
  42. Begin
  43. if length(stack) < 1 then error('pop','stack underflow');
  44. ps := 1;
  45. s := nextword(stack,ps);
  46. delete(stack,1,ps);
  47. stack := strrtrim(strltrim(stack));
  48. pop := s;
  49. {logging('pop', '['+ s + ']');{P}
  50. End;
  51.  
  52. {---------------------------------}
  53. Procedure push(s:stdstr; var stack:stack_type);
  54. { append s to the stack array }
  55. Begin
  56. if length(s) + 3 + length(stack) > stackmax then error('push',' stack overflow');
  57. stack := s + ' ' + stack;
  58. {logging('push','['+ s + ']');{P}
  59. End;
  60.  
  61. 
  62.