Author Topic: Alphabet character string only  (Read 13009 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Alphabet character string only
« Reply #60 on: April 13, 2018, 01:38:55 PM »
Sorry if I do not comment but ...

Trovo anche alcuni post su questo thread che confondono. L'unica cosa che possiamo fare a volte è ridere. Sii bene amico mio, è tutto roba piccola alla fine. Saluti.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: Alphabet character string only
« Reply #61 on: April 18, 2018, 01:51:36 PM »
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.

Code - Auto/Visual Lisp: [Select]
  1. ;;---------
  2. ;; MISC SUPPORT FUNCTIONS
  3. ;;---------
  4. (defun list-push (sym lst) (set lst (cons sym (eval lst))) )
  5.  
  6. ;;---------
  7. ;; LEXER
  8. ;;---------
  9. (defun token ( nu / isalpha isdigit ispunct )
  10.     (defun isdigit ( nu )
  11.       (and (>= nu 48) (<= nu 57)))
  12.     (defun isalpha ( nu )
  13.       (or (and (>= nu 65) (<= nu 90))
  14.           (and (>= nu 97) (<= nu 122))))
  15.     (defun ispunct ( nu )
  16.       (or (and (>= nu 33) (<= nu 47))
  17.           (and (>= nu 58) (<= nu 64))
  18.           (and (>= nu 91) (<= nu 96))))
  19.     ;; Tokens
  20.   (cond
  21.     ((isalpha nu) -1)
  22.     ((ispunct nu) -2)
  23.     ((isdigit nu) -3)
  24.     )
  25.  )
  26.  
  27. ;;---------
  28. ;; AST (Abstract Syntax Tree)
  29. ;;
  30. ;; This isn't really a good representation of an AST
  31. ;; (just uses simple lists) but it will work.
  32. ;;---------
  33. (defun StringExpr-AST ( n )
  34.   ;; this function only creates a list
  35.   (list-push (chr n) 'StringExprAST) )
  36.  
  37. (defun PunctExpr-AST ( n )
  38.   ;; this function only creates a list
  39.   (list-push (chr n) 'PunctExprAST) )
  40.  
  41. (defun HandleString ( n )
  42.   ;; This function only calls the function to create
  43.   ;; a list but this is where you'd access the SearchTree
  44.   (StringExpr-AST n))
  45.  
  46. (defun HandlePunct ( n )
  47.   ;; This function only calls the function to create
  48.   ;; a list but this is where you'd access the SearchTree
  49.   (PunctExpr-AST n))
  50.  
  51. ;;---------
  52. ;; PARSER
  53. ;;---------
  54. (defun parser (aString / al-string-to-ascii-list)
  55.     (defun al-string-to-ascii-list (str / alst cntr)
  56.       (if (= cntr '()) (setq cntr 1))
  57.       (while (<= cntr (strlen str))
  58.              (setq alst (cons (ascii (substr str cntr 1)) alst)
  59.                    cntr (1+ cntr)))
  60.       (reverse alst) )
  61.   (mapcar
  62.     (function
  63.       (lambda (n)
  64.         (cond
  65.           ((= (token n) -1) (HandleString n))
  66.           ((= (token n) -2) (HandlePunct n))
  67.           ;; ...
  68.           )
  69.         )
  70.       )
  71.  
  72.     (al-string-to-ascii-list aString)) )
  73.  
  74. ;;---------
  75. ;; MAIN DRIVER
  76. ;;---------
  77. ( (lambda nil
  78.     ;; A very dumb driver; only gets one string
  79.     (parser (getstring "> "))
  80.     (princ "Strings: \t")
  81.     (mapcar 'princ StringExprAST)
  82.     (princ "\nPunct: \t")
  83.     (mapcar 'princ PunctExprAST)
  84.     (setq StringExprAST nil
  85.           PunctExprAST nil)) )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org