using regular express to analyze lisp code
thermostat at gmail.com
Thu Oct 4 19:28:59 CEST 2007
On Oct 4, 1:13 pm, Kelie <kf9... at gmail.com> wrote:
> i've spent couple of hours trying to figure out the correct regular
> expression to catch a VisualLisp (it is for AutoCAD and has a syntax
> that's similar to common lisp) function body. VisualLisp is case-
> insensitive. Any line beginning with ";" is for comment (can have
> space(s) before ";").
> here is an example of VisualLisp function:
> (defun get_obj_app_names (obj / rv)
> (foreach app (get_registered_apps (vla-get-document obj))
> (if (get_xdata obj app)
> (setq rv (cons app rv))
> (if rv
> ;;"This line is comment (comment)"
> ;;) This line is also comment
> (acad_strlsort rv)
> for a function named foo, it is easy to find the beginning part of the
> "(defun foo", but it is hard to find the ")" at the end of code block.
> if eventually i can't come up with the solution using regular
> expression only, what i was thinking is after finding the beginning
> part, which is "(defun foo" in this case, i can count the parenthesis,
> ignoring anything inside "" and any line for comment, until i find the
> closing ")".
> not sure if i've made myself understood. thanks for reading.
So, paren matching is a canonical context-sensitive algorithm. Now,
many regex libraries have *some* not-purely-regular features, but I
doubt your going to find anything to match parens in a single regex.
If you want to go all out you can use a parser generator (for python
parser generators, see http://python.fyxm.net/topics/parsing.html).
Otherwise, you can go about it the quick-and-dirty way you describe:
scan for matching open and close parens, and ignore things in quotes
More information about the Python-list