Parsing strings into LISP syntax 
Author Message
 Parsing strings into LISP syntax

Hi all,

I have an input string (from a file or whatever) of the form

    "fn(x)"

which I would like to assign to a variable as

    '(fn x)

Something like:

  (setf hello (translate "f(x)"))

1> hello
(F X)

I'm confused by creating symbols from strings, parsing parenthesis that are
in non-lisp syntax using the read or read-line functions, etc.

I have recently bought both Graham Lisp books and Norvig's AI book.

Can anyone point me in the right direction?



Tue, 23 Apr 2002 03:00:00 GMT  
 Parsing strings into LISP syntax

Quote:

> Hi all,

> I have an input string (from a file or whatever) of the form

>     "fn(x)"

> which I would like to assign to a variable as

>     '(fn x)

> Something like:

>   (setf hello (translate "f(x)"))

> 1> hello
> (F X)

I can't give you anything nearly sufficient, but I can tell you what's
going on and why you're having trouble.  What you're trying to do is
called "parsing", and there are whole mini-languages devoted to it.

Lisp, for all its virtue, is not very strong in general parsing.
That's because it's so easy to just read Lisp in that one rarely needs
to do general-purpose parsing.  Of course it *can* be done in Lisp,
but generally for parsing, one thinks of PCCTS, or lex and yacc, or
flex and bison, none of which are in Lisp.

Of course, you could hack a quick and dirty solution in Lisp.  It will
be hard to maintain, but if you're just looking for a one-time, short
term thing, that's one way to go.

--
Tom Breton, http://world.std.com/~tob
Not using "gh" since 1997. http://world.std.com/~tob/ugh-free.html



Wed, 24 Apr 2002 03:00:00 GMT  
 Parsing strings into LISP syntax

Quote:

> Lisp, for all its virtue, is not very strong in general parsing.

This is FUD. Lisp has been used in parsing problems for decades.
Half of the natural language systems have been written in Lisp.
On my Lisp machine I'd just use the parser substrate that
is available and has been used with the C, Pascal and fortran
compiler from Symbolics. This stuff available since 15 years.
I guess companies and universities have lot's of parsers
written in CL.


Wed, 24 Apr 2002 03:00:00 GMT  
 Parsing strings into LISP syntax
Pursuant to the original posting:

I already have a function defined in lisp, for example:

(defun fn (x)
    (* x 3))

Now I want to evaluate a string that was entered at run-time (via file, or
whatever). E.g:

1> (evaluate "fn(5)")
15

Presumably I would have called a function as in the original posting called
(translate "fn (5)") which would have returned (fn 5).  It could then use
funcall on the cdr, etc.

So the question is, how do I create dynamically a symbol 'fn which stands
for the function (fn) from the input string "fn"? And, how do I create
dynamically a variable x which evaluates to itself from the input string
"x"?

I've played with intern, gensym, make-symbol, etc. but to no avail, and the
Graham book does not go into great detail about them.

Thanx

Quote:

>Hi all,

>I have an input string (from a file or whatever) of the form

>    "fn(x)"

>which I would like to assign to a variable as

>    '(fn x)

>Something like:

>  (setf hello (translate "f(x)"))

>1> hello
>(F X)

>I'm confused by creating symbols from strings, parsing parenthesis that are
>in non-lisp syntax using the read or read-line functions, etc.

>I have recently bought both Graham Lisp books and Norvig's AI book.

>Can anyone point me in the right direction?



Wed, 24 Apr 2002 03:00:00 GMT  
 Parsing strings into LISP syntax
* Pascal Saremsky
| So the question is, how do I create dynamically a symbol 'fn which stands
| for the function (fn) from the input string "fn"?

  the function FIND-SYMBOL takes a string a returns a symbol with that name
  if it exists and a status keyword that says how the symbols is available
  in that package.  you can use this to control access only to external
  functions in your user-accessible package.

| And, how do I create dynamically a variable x which evaluates to itself
| from the input string "x"?

  you use special variables for this, and the symbol lookup is just like
  FIND-SYMBOL.  if the user shall be able to use new symbols, use INTERN
  instead of FIND-SYMBOL.

| I've played with intern, gensym, make-symbol, etc. but to no avail, and
| the Graham book does not go into great detail about them.

  INTERN should have given you a valuable lead.  perhaps you have been
  using a different package inadvertently?  if you post your code, however,
  helping you becomes significantly easier.

#:Erik
--
  Attention Microsoft Shoppers!  MS Monopoly Money 6.0 are now worthless.



Wed, 24 Apr 2002 03:00:00 GMT  
 Parsing strings into LISP syntax

Quote:


> > Lisp, for all its virtue, is not very strong in general parsing.

> This is FUD.

Knee-jerk defenders like you give Lisp a bad name.  It's a nice
language, not a religion.

I gave him the answer he needed, and if it wasn't as flattering to
Lisp as you would like, too bad.

Quote:
> Half of the natural language systems have been written in Lisp.

Boy, did you pick the wrong guy to try this particular trick on.  I do
NLP, and its focus is almost entirely different than parsing computer
language.  No bearing.  I've caut you trying to pull the wool over my
eyes.

Quote:
> On my Lisp machine

I really don't care.

--
Tom Breton, http://world.std.com/~tob
Not using "gh" since 1997. http://world.std.com/~tob/ugh-free.html



Wed, 24 Apr 2002 03:00:00 GMT  
 Parsing strings into LISP syntax

Quote:




Another one for my killfile.

--
Rainer Joswig, ISION Internet AG, Harburger Schlossstrasse 1,
21079 Hamburg, Germany, Tel: +49 40 77175 226



Wed, 24 Apr 2002 03:00:00 GMT  
 Parsing strings into LISP syntax
Thanks Erik.

This is the example:

Let's say I wanted to 'compile' the following string into lisp:

    (compile-from-c "int times3(x) { return x*3; }")

so that I can now use

    1> (times3 5)
    15

Clearly I need something to take the string "times3" and generate the symbol
TIMES3 from it. Intern or find-symbol alone doesn't seem to help me; the
following example doesn't work:

    (defun (intern "TIMES3") (x)  ;; Wrong!
        (* x 3))

Perhaps I should use macros?

I'm doing everything in the same package.

I get the feeling I'm missing the essence of lisp here!

-Pascal

Quote:

>* Pascal Saremsky
>| So the question is, how do I create dynamically a symbol 'fn which stands
>| for the function (fn) from the input string "fn"?

>  the function FIND-SYMBOL takes a string a returns a symbol with that name
>  if it exists and a status keyword that says how the symbols is available
>  in that package.  you can use this to control access only to external
>  functions in your user-accessible package.

>| And, how do I create dynamically a variable x which evaluates to itself
>| from the input string "x"?

>  you use special variables for this, and the symbol lookup is just like
>  FIND-SYMBOL.  if the user shall be able to use new symbols, use INTERN
>  instead of FIND-SYMBOL.

>| I've played with intern, gensym, make-symbol, etc. but to no avail, and
>| the Graham book does not go into great detail about them.

>  INTERN should have given you a valuable lead.  perhaps you have been
>  using a different package inadvertently?  if you post your code, however,
>  helping you becomes significantly easier.

>#:Erik
>--
>  Attention Microsoft Shoppers!  MS Monopoly Money 6.0 are now worthless.



Wed, 24 Apr 2002 03:00:00 GMT  
 Parsing strings into LISP syntax

Quote:

> Clearly I need something to take the string "times3" and generate the symbol
> TIMES3 from it.

(intern "TIMES3") works.

Quote:
>Intern or find-symbol alone doesn't seem to help me; the
> following example doesn't work:

>     (defun (intern "TIMES3") (x)  ;; Wrong!
>         (* x 3))

DEFUN doesn't evaluate the function name part.

Something like this will work:

(setf (symbol-function (intern "TIMES3"))
      (lambda (x) (* x 3)))

--
Rainer Joswig, ISION Internet AG, Harburger Schlossstrasse 1,
21079 Hamburg, Germany, Tel: +49 40 77175 226



Wed, 24 Apr 2002 03:00:00 GMT  
 Parsing strings into LISP syntax
I see.  And so if I also wanted to create a symbol which evaluates to
itself, then I could use symbol-value instead:

(setf (symbol-value (intern "F")) (intern "F"))

1> F
F

Because the second intern returns an instance of the same symbol. Please
correct me if I'm wrong, otherwise this works fine for me!

I'll figure out the package complications later on...

Thanks Erik and Rainer.

-Pascal

Quote:



>> Clearly I need something to take the string "times3" and generate the
symbol
>> TIMES3 from it.

>(intern "TIMES3") works.

>>Intern or find-symbol alone doesn't seem to help me; the
>> following example doesn't work:

>>     (defun (intern "TIMES3") (x)  ;; Wrong!
>>         (* x 3))

>DEFUN doesn't evaluate the function name part.

>Something like this will work:

>(setf (symbol-function (intern "TIMES3"))
>      (lambda (x) (* x 3)))

>--
>Rainer Joswig, ISION Internet AG, Harburger Schlossstrasse 1,
>21079 Hamburg, Germany, Tel: +49 40 77175 226




Wed, 24 Apr 2002 03:00:00 GMT  
 Parsing strings into LISP syntax

| I get the feeling I'm missing the essence of lisp here!

  well, just that INTERN is called by the Lisp reader when it sees a string
  of characters that doesn't match anything else.  so when you write

(defun times3 (x) (* x 3))

  the Lisp reader has already interned the symbol for you, and (find-symbol
  "TIMES3") will return the symbol.

  at this point, we're getting too close for comfort to the Lisp reader's
  character case conversion algorithm, and you are well advised not to try
  to out-do it.  for legacy reasons (a.k.a. "hysterical raisins"), the Lisp
  reader upcases; it could have downcased, it could have retained case; it
  upcases, but you don't have to do that.  when you read that symbol as a
  string of characters, just call FIND-SYMBOL (or INTERN) on it directly,
  and the right symbol is returned.

  however, to get a symbol in Lisp with the same name, you must cause the
  Lisp reader to see the exact name, too.  this essentially means that you
  have to cause INTERN not to do case conversion.  using a very powerful
  Lisp reader mechanism, the read-time evaluator, you could do

(defun #.(intern "times3") (x) (* x 3))

  but we have special syntax for this common phenomenon:

(defun |times3| (x) (* x 3))

  you can actually cause the Lisp reader not to do case conversion, and
  this may be preferable in your own code.  see the function READTABLE-CASE
  for the whole story.  however, if you set up a case sensitive readtable
  for your Lisp code, remember that Common Lisp upcases, and you need to
  type in uppercase, too:

(DEFUN times3 (x) (* x 3))

  the aesthetics of this approach is, however, generally disputed.

#:Erik
--
  Attention Microsoft Shoppers!  MS Monopoly Money 6.0 are now worthless.



Wed, 24 Apr 2002 03:00:00 GMT  
 Parsing strings into LISP syntax
* Pascal Saremsky
| Because the second intern returns an instance of the same symbol.  Please
| correct me if I'm wrong, otherwise this works fine for me!

  almost correct: INTERN returns the exact same value every time1.  the
  whole point with INTERN is precisely that: to return _the_ symbol that
  has the name given by the argument, so if the two strings are EQUAL (that
  is, STRING=), then the symbols returned will be EQ.  you may have gotten
  this point, already, but your wording left an ambiguity -- symbols don't
  have instances, they _are_ instances.

#:Erik, persnicketeer
-------
1 barring intervening UNINTERN calls or changes to *PACKAGE*.
--
  Attention Microsoft Shoppers!  MS Monopoly Money 6.0 are now worthless.



Wed, 24 Apr 2002 03:00:00 GMT  
 Parsing strings into LISP syntax

Quote:
> I have an input string (from a file or whatever) of the form

>     "fn(x)"

> which I would like to assign to a variable as

>     '(fn x)

Since the f(g(x)) form of syntax merely puts the function name just
outside a set of parentheses (as opposed to just inside) I think this
simple case can be coped with in the following naive manner, by pushing
such atoms inside any following lists:

----
(defun read-as-list (string)
  (do* ((stream (make-string-input-stream string))
        (list (list #1=(read stream nil)) (push #1# list)))
       ((not (listen stream)) (reverse list))))

(defun functionise (list)
  (let ((copy (copy-tree list)))
    (rfunctionise copy)))

(defun rfunctionise (list)
  (do ((sublist list (cdr sublist)))
      ((endp sublist) list)
    (when (consp (cadr sublist))
      (rfunctionise (cadr sublist))
      (when (atom (car sublist))
        (setf (car sublist) (push (car sublist) (cadr sublist))
              (cdr sublist) (cddr sublist))))))

(defun read-function-form (string)
  (rfunctionise (read-as-list string)))
----

'read-function-form' produces a list of the function forms in the string
given (and will include a final NIL if the string terminates with whitespace).

However, I suspect this is possibly a little more elementary than what
you're looking for. If what you really want is to build your own parser
for algebraic functions expressed as text, then there are, it seems to
me, two main options: 1) write your own parsing functions, or 2) use
common lisp's inbuilt reading framework, writing your own readtable and
shadowing *readtable* while reading.

The former can be less messy for simpler syntaxes, the latter much more
powerful, and undoubtedly the Right Way. The first third or thereabouts
of chapter 22 of Steele give some indication of the mess you are likely
to enounter with option 2...

HTH, HAND.

A

PS: Actually, option 3 is to look for someone else who's already done it
- it's likely to be out there somewhere, since it'd be useful enough.

--====----====----====----====----====----====----====----====--
K(-B-E-A)K8,1:C//cc/DE/F24,'GfGAB^/b'b(EG)b(FA)b(EG)=b(DF)c(E)/^



Thu, 25 Apr 2002 03:00:00 GMT  
 Parsing strings into LISP syntax

Quote:

> I see.  And so if I also wanted to create a symbol which evaluates to
> itself, then I could use symbol-value instead:

> (setf (symbol-value (intern "F")) (intern "F"))

See also the function SET .

This is btw. a property of keywords (symbols in the keyword package):
they are symbols that evaluate to themselves.

Quote:

> 1> F
> F

> Because the second intern returns an instance of the same symbol.

The difference between strings and interned symbols is that you get
the *same* object with symbols. So there is only one
instance of a symbol with a certain name - once they are interned.

(eq 'f 'f)    ->  T
(eq "f" "f")  -> NIL

To puzzle you a bit more:

if you don't intern symbols (put them in a package) they
won't be eq, too.

(eq (make-symbol "F") (make-symbol "F"))   ->  NIL
(eq '#:F '#:F)                             ->  NIL

--
Rainer Joswig, ISION Internet AG, Harburger Schlossstrasse 1,
21079 Hamburg, Germany, Tel: +49 40 77175 226



Thu, 25 Apr 2002 03:00:00 GMT  
 Parsing strings into LISP syntax


Quote:
> I have an input string (from a file or whatever) of the form

>     "fn(x)"

> which I would like to assign to a variable as

[...]

If you do want to use a parser, then email me for details about Zebu (a
yacc kind of thing written in Lisp) - I'm not connected with the package
in any way, but have version 3.5.5 which is the latest version I could
find (and a lot more up-to-date than the CMU archive).  It's quite cute
and easy to use (if you've used this kind of thing before - if not, then
there would be a pretty steep learning curve).

Andrew
http://www.andrewcooke.free-online.co.uk

PS Incidentally, Zebu no onger seems to be supported.  If anyone knows
otherwse, or would like to support it, please contact me - otherwise I
may stick a version with my mods on my web site early next year and act
as some kind of interim source/maintainer (I would really recommend
someone else doing this as I know very little about Lisp! :-).

Sent via Deja.com http://www.deja.com/
Before you buy.



Thu, 25 Apr 2002 03:00:00 GMT  
 
 [ 24 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Lisp syntax : Lisp objects :: Scheme syntax : characters

2. Lisp editor that can parse strings

3. Common Lisp - String parsing

4. parsing string for string

5. New to Lisp question: advantages of Lisp syntax?

6. Syntax problem- Parsing Mail Files

7. Mysterious syntax errors - cannot parse

8. Eiffel++: Syntax & parsing changes

9. Syntax Error Recovery in the Parsing Library.

10. syntax, lexing and parsing, macros

11. PARSE SOURCE syntax question

12. PARSE syntax in CMS?

 

 
Powered by phpBB® Forum Software