Contents    Page-10    Prev    Next    Page+10    Index   

Grammar Compiler [file gramcom.clj]

It is easy to write a grammar compiler that converts a Yacc-like grammar into the equivalent ATN parsing functions. This is especially easy in Lisp since Lisp code and Lisp data are the same thing.


;            $$       $1   $2
(rulecompr '(loc ->  (in (city))
               (restrict 'city $2))
           'locfn)

(defn locfn []
  (saveptr)
  (let [$1 (and (= atnword (quote in)) atnword)]
    (if $1
        (do (nextword)
            (let [$2 (wordcat (quote city))]
              (if $2
                  (do (nextword)
                      (success)
                      (restrict (quote city) $2))
                  (fail))))
        (fail))))