hi, anyone know of a simple working arithmetic parser that i can see
exactly how it's done? i wrote one in Java, but, i find it hard
translating to lisp.. mmm.. to clearify what i mean by arithmetic
parser, i want to be able to parse math equations such as (3+4)*5, by
doing them in the right order.. it sounds pretty simple, i want to see
how lisp compares to java.. thanks alot :D
··········@gmail.com wrote:
> hi, anyone know of a simple working arithmetic parser that i can see
> exactly how it's done? i wrote one in Java, but, i find it hard
> translating to lisp.. mmm.. to clearify what i mean by arithmetic
> parser, i want to be able to parse math equations such as (3+4)*5, by
> doing them in the right order.. it sounds pretty simple, i want to see
> how lisp compares to java.. thanks alot :D
>
Yikes. You wrote one in Java but cannot write one in Lisp? I do not
believe you. Lisp is a superset of Java, so your report is impossible.
Instead of posting huge vague questions, just write the damn Lisp and
whatever does not work, post it here and ask a sensible question.
This looks like a fancy way to get your homework done. Even if you did
not have an imaginary Java version working, i would still wonder what
the problem was, but the report would be plausible because you might
just be a newbie to programming overall, or recursion or parsing. But
you say "i wrote one in java....". pwuawhahahah.
Tell you what. Forget the arithmetic. You say "I find it hard
translating to lisp". Got an example? (Answer: no.)
kenny
--
Cells? Cello? Cells-Gtk?: http://www.common-lisp.net/project/cells/
Why Lisp? http://lisp.tech.coop/RtL%20Highlight%20Film
"Doctor, I wrestled with reality for forty years, and I am happy to
state that I finally won out over it." -- Elwood P. Dowd
well, no, i admit i don't know enough about lisp to write much stuff
that's useful.. since i did the parser for Java for my AP practice, i
thought i'd do it in lisp and see how they compare.. i do have the java
version working, i can send it via e-mail or post it somewhere if you
wish to see :D mmm.. now i found out my lisp skills is wayyy too bad..
so i'll dive into some more materials and post when i'm done (or have
questions :D)..
and oh yah, this is not an excuse to not do my homework, since i don't
learn lisp in school or anything, we learn java, that's why i wrote it
in Java first, i got a AP exam coming up real soon :|
mmm.. what i did with my java version is that i wrote a
ExpressionTokenizer that takes in a String (expression).. and it has
two methods, peekToken() and nextToken(), it goes one character at a
time, if it meets a number next, it'll continue go on until it reaches
a sign..
second class is Evaluator.. i have getFactorValue(), getTermValue(),
getExpressionValue().. since this is a practice in mutual recursion..
we can divide a expression to several parts, if we meet a "()" in the
expression, it calles getExpressionValue again for the stuff inside
().. then the termvalue method handles the * / side of the expression
and + - is handled by factorvalue.. that's basically the gist of what
i'm doing. i'll post the .java file up (with a simple GUI based
calculator..) on my site later today :D
··········@gmail.com wrote:
> hi, anyone know of a simple working arithmetic parser that i can see
> exactly how it's done? i wrote one in Java, but, i find it hard
> translating to lisp.. mmm.. to clearify what i mean by arithmetic
> parser, i want to be able to parse math equations such as (3+4)*5, by
> doing them in the right order.. it sounds pretty simple, i want to see
> how lisp compares to java.. thanks alot :D
There's a package called CL-INFIX which is just this -- a parser for
infix expressions. It defines a reader-macro #I() that lets you use
infix code within a Lisp program:
#I(2 + 3 * 4) -> 14
'#I(2 + 3 * 4) -> (+ 2 (* 3 4))
It is not limited to arithmetic, giving infix forms for array reference,
logic operations, and conditionals as well.
--
Karl A. Krueger <········@example.edu> { s/example/whoi/ }
On Sat, 16 Apr 2005 19:51:46 -0700, randomtalk wrote:
> hi, anyone know of a simple working arithmetic parser that i can see
> exactly how it's done? i wrote one in Java, but, i find it hard
> translating to lisp.. mmm.. to clearify what i mean by arithmetic
> parser, i want to be able to parse math equations such as (3+4)*5, by
> doing them in the right order.. it sounds pretty simple, i want to see
> how lisp compares to java.. thanks alot :D
http://abstractnonsense.com/convert.lisp.html
I wrote it a few months ago, when I needed an infix->RPN converter. It
works by converting from infix to prefix, and then to RPN (so it does what
you want and then a little more).
It's just under 70 lines of code, so should be pretty easy to understand.
Hope that helps.
Cheers,
Bill.
mmm.. apparently the cl-infix is not free (arguably not open source..)?
mmm.. since i did a google search, it says the infix package is under
non-free tree in debian :|.. i'll try the two pieces of code now :D
··········@gmail.com wrote:
> mmm.. apparently the cl-infix is not free (arguably not open source..)?
> mmm.. since i did a google search, it says the infix package is under
> non-free tree in debian :|.. i'll try the two pieces of code now :D
CL-INFIX is distributed under a license that allows redistribution and
modification, but does not allow you to charge money for it:
;;; Copyright (c) 1993 by Mark Kantrowitz. All rights reserved.
;;;
;;; Use and copying of this software and preparation of derivative works
;;; based upon this software are permitted, so long as the following
;;; conditions are met:
;;; o no fees or compensation are charged for use, copies,
;;; distribution or access to this software
;;; o this copyright notice is included intact.
;;; This software is made available AS IS, and no warranty is made about
;;; the software or its performance.
Probably not a big deal for a casual or experimental project, but it
could be a pain for someone looking to make a collection of the world's
coolest Common Lisp packages ... or looking to use CL-INFIX in a project
with a goal of either proprietary *or* open source distribution.
For instance, these license terms would forbid someone from selling
CD-ROMs that contained CL-INFIX along with other software. Even if the
presence of CL-INFIX were not the selling point of the CD-ROM, charging
money for the CD would still be a copyright infringement.
If Debian included CL-INFIX in their "main" distribution (which goes on
the install CD images) then nobody could sell Debian CDs without
violating the copyright on CL-INFIX. Debian wants people to be able to
sell Debian CDs legally -- so they place packages such as this in
"non-free" which doesn't go on the CDs.
If you're concerned, you might be able to contact the author of CL-INFIX
and ask if it could be relicensed under a Free Software / Open source
license, such as the GPL, BSD, or LLGPL licenses.
--
Karl A. Krueger <········@example.edu> { s/example/whoi/ }
You may want to look at Qi which
generates good Lisp from pattern-directed
definitions. It also has an inbuilt
compiler-compiler Qi YACC. See
www.lambdassociates.org for
details and
www.lambdassociates.org/qilisp.htm
for the 15 minute introduction
for Lisp programmers.
There is also a large YACC for Lisp
- CL YACC available from
http://www.pps.jussieu.fr/~jch/software/cl-yacc/
for parsing work.
A lot depends here on what you
want to do with your arithmetic
reader and whether the input
is fully parenthesised or not.
Here is a simple recognisor in Qi.
(define arith?
[X + Y] -> (and (arith? X) (arith? Y))
[X * Y] -> (and (arith? X) (arith? Y))
[X / Y] -> (and (arith? X) (arith? Y))
[X - Y] -> (and (arith? X) (arith? Y))
X -> (number? X))
or shorter.
(define arith?
[X Op Y] -> (and (arith? X) (arith? Y)) where (element? Op [* / -
+])
X -> (number? X))
Qi generates Lisp code from this code
(DEFUN arith? (V49)
(COND
((AND (CONSP V49) (CONSP (CDR V49)) (CONSP (CDR (CDR V49)))
(NULL (CDR (CDR (CDR V49))))
(qi::wrapper
(element? (CAR (CDR V49)) (CONS '* (CONS '/ (CONS '- (CONS '+
NIL)))))))
(THE SYMBOL (and (arith? (CAR V49)) (arith? (CAR (CDR (CDR
V49)))))))
(T (THE SYMBOL (number? V49)))))
More complex examples would use character
streams and deal with non-fully parenthesised
expressions.
See the online text Functional Programming in Qi
for a type checked version of this kind of program
(chapter 11).
hope this helps
Mark