home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / winlisp.zip / OOPL.LZH / SAMPLE.GRM < prev    next >
Text File  |  1989-07-30  |  5KB  |  226 lines

  1. ;===============================================================================
  2. ;
  3. ;             A    S A M P L E    G R A M M A R
  4. ;
  5. ; Ex. given by David S. Touretzky in "How Lisp Has Changed" - Byte Feb. 1988
  6. ; In order to parse some sentences, you must evaluate:
  7. ;
  8. ;            (load "oopl.wl")
  9. ;        (load "stream.wl")
  10. ;        (load "collection.wl")
  11. ;        (load "grammar.wl")
  12. ;        (load "parser.wl")
  13. ;        (setq aParser [{TopDownParser} new "sample.grm"])
  14. ;
  15. ; and then you may evaluate, for example:
  16. ;
  17. ;    (setq aStream 
  18. ;          [{Stream} on '#["the" "boy" "ate" "a" "tasty" "little" "pizza"]])
  19. ;    [aParser parse aStream 's]
  20. ;    [aStream reset]
  21. ;    [aParser parse aStream 'np]
  22. ;    [aStream peek]
  23. ;    [aParser parse aStream 'vp]
  24. ;            . . .
  25. ; or:
  26. ;
  27. ;    (setq anotherStream 
  28. ;          [{Stream} on '(john said that mary kicked the ball)])
  29. ;    [aParser parse anotherStream 's]
  30. ;===============================================================================
  31.  
  32. (defun np-action l    (cons 'np l))
  33.  
  34. (defun nprop-action l    (cons 'nprop l))
  35.  
  36. (defun pro-action l    (cons 'pro l))
  37.  
  38. (defun art-action l    (cons 'art l))
  39.  
  40. (defun adj-action l    (cons 'adj l))
  41.  
  42. (defun n-action l    (cons 'n l))
  43.  
  44. (defun vp-action l    (cons 'vp l))
  45.  
  46. (defun vi-action l    (cons 'vi l))
  47.  
  48. (defun vt-action l    (cons 'vt l))
  49.  
  50. (defun vrel-action l    (cons 'vrel l))
  51.  
  52. %%
  53.  
  54. s        : np vp
  55.             ( (lambda l    (cons 's l)) )
  56.         | np %error
  57.             ( (lambda ( _ tokenStream)
  58.                                   (error "syntax error" 
  59.                      " expected a 'vp', got "
  60.                                 [tokenStream next 3])) )
  61.         ;
  62.  
  63. np        : nprop
  64.             ( np-action )
  65.         | pro
  66.             ( np-action )
  67.                 | art adjlist n
  68.             ( np-action )
  69.                 | art n
  70.             ( np-action )
  71.         | art %error
  72.             ( (lambda ( _ tokenStream)
  73.                                   (error "syntax error" 
  74.                      " expected a 'n' or 'adjlist n', got "
  75.                                 [tokenStream next 3])) )
  76.                 ;
  77.                 
  78. nprop        : %john
  79.             ( nprop-action )
  80.         | %mary
  81.             ( nprop-action )
  82.                 ;
  83.                 
  84. pro        : %he
  85.             ( pro-action )
  86.         | %she 
  87.             ( pro-action )
  88.         | %it
  89.             ( pro-action )
  90.         ;
  91.  
  92. art        : %the
  93.             ( art-action )
  94.         | %a
  95.             ( art-action )
  96.         ;
  97.     
  98. adjlist        : adj adjlist
  99.         | adj
  100.         ;
  101.  
  102. adj        : %big
  103.             ( adj-action )
  104.         | %little
  105.             ( adj-action )
  106.         | %red
  107.             ( adj-action )
  108.         | %tasty
  109.             ( adj-action )
  110.         ;
  111.  
  112. n        : %boy
  113.             ( n-action )
  114.         | %girl
  115.             ( n-action )
  116.         | %ball
  117.             ( n-action )
  118.         | %pizza
  119.             ( n-action )
  120.         | %stick
  121.             ( n-action )
  122.         ;
  123.  
  124. vp        : vi
  125.             ( vp-action )
  126.         | vt np
  127.             ( vp-action )
  128.         | vt %error
  129.             ( (lambda ( _ tokenStream)
  130.                                   (error "syntax error" 
  131.                      " expected a 'np', got "
  132.                                 [tokenStream next 3])) )
  133.         | vrel relc
  134.             ( vp-action )
  135.         | vrel %error
  136.             ( (lambda ( _ tokenStream)
  137.                                   (error "syntax error" 
  138.                      " expected a 'relc', got "
  139.                                 [tokenStream next 3])) )
  140.         ;
  141.  
  142. vi        : %laughed
  143.             ( vi-action )
  144.         | %sang
  145.             ( vi-action )
  146.         ;
  147.  
  148. vt        : %threw
  149.             ( vt-action )
  150.         | %kicked
  151.             ( vt-action )
  152.         | %ate
  153.             ( vt-action )
  154.         ;
  155.  
  156. vrel        : %saw
  157.             ( vrel-action )
  158.         | %said
  159.             ( vrel-action )
  160.         | %thought
  161.             ( vrel-action )
  162.         ;
  163.  
  164. relc        : %that s
  165.             ( (lambda l    (cons 'relc l)) )
  166.         | %that %error
  167.             ( (lambda ( _ tokenStream)
  168.                                   (error "syntax error" 
  169.                      " expected a 's', got "
  170.                                 [tokenStream next 3])) )
  171.         ;
  172.         
  173. %%
  174.  
  175. ;;; GIVE symbol A Token BEHAVIOR
  176.  
  177. [{symbol} addMethod 'name         '(() self)]
  178.  
  179. [{symbol} addMethod 'value         '(() self)]
  180.  
  181.  
  182. ;;; GIVE string A Token BEHAVIOR
  183.  
  184. [{string} addMethod 'name         '(() [{symbol} new () self])]
  185.  
  186. [{string} addMethod 'value         '(() self)]
  187.  
  188.  
  189. ;;; GIVE cons A IndexedCollection BEHAVIOR
  190.  
  191. [{cons} primitiveAllocator
  192.         '(lambda inits
  193.                  (when inits
  194.                                (let ( (inits (car inits)) )
  195.                         (if (consp inits)
  196.                         inits
  197.                         ;;ELSE
  198.                         (error "{cons}:primitiveAllocator"
  199.                                "not a list"
  200.                                                inits)))))]
  201.                                 
  202. [{cons} addMethod 'at         '((index)    (nth index self))]
  203.  
  204. [{cons} addMethod 'size        '(()         (length self))]
  205.  
  206.  
  207. ;;; GIVE vector A IndexedCollection BEHAVIOR
  208.  
  209. [{vector} primitiveAllocator     
  210.         '(lambda inits
  211.                  (if inits
  212.                              (let ( (inits (car inits)) )
  213.                       (if (consp inits)
  214.                       (apply 'vector inits)
  215.                       ;;ELSE
  216.                       (error "{vector}:primitiveAllocator"
  217.                             "not a list"
  218.                                              inits)))
  219.                  ;;ELSE
  220.                  (makevector 0 ())))]
  221.                                 
  222. [{vector} addMethod 'at        '((index)    (vref self index))]
  223.  
  224. [{vector} addMethod 'size    '(()         (vlength self))]
  225.