|Q: sample code for parsing algebraic expressions? email@example.com (Charlie Zender) (1995-12-09)|
|From:||Charlie Zender <firstname.lastname@example.org>|
|Date:||9 Dec 1995 20:01:01 -0500|
|Organization:||National Center for Atmospheric Research|
|Keywords:||parse, question, comment|
I need to parse algebraic expressions for a data processor i'm writing.
that is, I need a function to take forward algebraic expressions like
x^2+3*(x+2) and return a parsed listed of (binary,unary) operator
expressions in the correct order. The algebraic expressions will be
known at run time, so that the code I need does not have to be able
to work like a calculator, and take expressions in real time from the
Here's what I'm hoping the function will do
function results in quotes,
input to function->"x^2+3*x+9"
output from function->
"x,2 binary exponentiate" (goes on stack)
"x,3 binary multiply" (goes on stack)
"binary add" (adds last two number on stack together, places result on stack)
"9 binary add" (add 9 to last number on stack)
Clearly every compiler that knows math has a parser like this in it
somewhere, but I don't know what's most appropriate for my uses.
Does anyone have recommendations on how to do this? Sample code? Books?
Articles? Should I use yacc/bison? Should I write it in C?
thanks in advance (please email as well as post any responses)
Charlie Zender Voice, FAX: (303) 497-1612, 497-1324
NCAR/CGD/CMS E-mail: email@example.com
P.O. Box 3000 URL: http://www.cgd.ucar.edu/cms/zender
Boulder CO 80307-3000 PGP: finger -l firstname.lastname@example.org
[Our book "lex&yacc, 2nd ed" has the usual calculator example, but someone
might have canned infix to RPN code. -John]
Return to the
Search the comp.compilers archives again.