I took a little time at lunch--for a few days--and built a "pretty good" (read: *meh*) Autolisp representation of how you'd build a simplistic lexer/parser/thing in a lower level language like C. A lot of the AutoLisp code doesn't make much sense because there isn't really a direct translation for the techniques (especially the AST; because we don't have classes and structures in AutoLisp) and I had to restructure some of the prescribed methods but at least you can see the general idea.
Also, this code/post wasn't necessarily to demonstrate anything "profound", it was merely for my own uses (I used it to sort of dip my toes back into lisp pool). It was frustrating for me because I kept typing things like COND incorrect and I had to force myself to think before I started typing each line.
I hope someone gets a good laugh from this code I worked so hard on these last few days during my lunch breaks.
;;---------
;; MISC SUPPORT FUNCTIONS
;;---------
;;---------
;; LEXER
;;---------
(defun token
( nu
/ isalpha isdigit ispunct
) (and (>= nu
48) (<= nu
57))) (or (and (>= nu
65) (<= nu
90)) (and (>= nu
97) (<= nu
122)))) (or (and (>= nu
33) (<= nu
47)) (and (>= nu
58) (<= nu
64)) (and (>= nu
91) (<= nu
96)))) ;; Tokens
((isalpha nu) -1)
((ispunct nu) -2)
((isdigit nu) -3)
)
)
;;---------
;; AST (Abstract Syntax Tree)
;;
;; This isn't really a good representation of an AST
;; (just uses simple lists) but it will work.
;;---------
(defun StringExpr
-AST
( n
) ;; this function only creates a list
(list
-push
(chr n
) 'StringExprAST
) )
(defun PunctExpr
-AST
( n
) ;; this function only creates a list
(list
-push
(chr n
) 'PunctExprAST
) )
(defun HandleString
( n
) ;; This function only calls the function to create
;; a list but this is where you'd access the SearchTree
(StringExpr-AST n))
;; This function only calls the function to create
;; a list but this is where you'd access the SearchTree
(PunctExpr-AST n))
;;---------
;; PARSER
;;---------
(defun parser
(aString
/ al
-string
-to
-ascii
-list) (defun al
-string
-to
-ascii
-list (str
/ alst cntr
) (if (= cntr '
()) (setq cntr
1)) ((= (token n) -1) (HandleString n))
((= (token n) -2) (HandlePunct n))
;; ...
)
)
)
(al
-string
-to
-ascii
-list aString
)) )
;;---------
;; MAIN DRIVER
;;---------
;; A very dumb driver; only gets one string
PunctExprAST nil)) )