comp.lang.ada
 help / color / mirror / Atom feed
* anyone knows how to do this?
@ 1997-10-27  0:00 sho
  1997-10-27  0:00 ` Larry Coon
  0 siblings, 1 reply; 2+ messages in thread
From: sho @ 1997-10-27  0:00 UTC (permalink / raw)



Dear Programmers,

How do you get an expression from the user and calculate it? like for
example.. the user has to input (1+3/4 rem 4) then calculate the result
Is it possible to get user input in expression like the example above?
If you know how to do it could you please tell me how? Thank you for
your time.

Sho




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: anyone knows how to do this?
  1997-10-27  0:00 anyone knows how to do this? sho
@ 1997-10-27  0:00 ` Larry Coon
  0 siblings, 0 replies; 2+ messages in thread
From: Larry Coon @ 1997-10-27  0:00 UTC (permalink / raw)



sho wrote:
 
> How do you get an expression from the user and calculate it? like for
> example.. the user has to input (1+3/4 rem 4) then calculate the result
> Is it possible to get user input in expression like the example above?
> If you know how to do it could you please tell me how? Thank you for
> your time.

Hmmm, there's a right way to do it (write a lexer
and parser) and there's probably a simple way of
doing it (just chew through the thing and use a
stack).  The quick way would involve reading a
character at a time until you have a "token" (an
atomic element such as "+" or "rem"), push the
token onto a stack, and when you've consumed all
the input, go back through the stack pop each
token-op-token, evaluate it, and push the result.
Doing this, you need to watch for parens and
precedence, so it's not as easy as it looks (most
intro programming examples I've seen do this kind
of approach but restrict the input to valid
postfix expressions).

Doing it right is not an easy task.  To do it right
(precedence rules, parenthesised sub-expressions,
etc.), you'd need to write a lexical analyzer
(to break the expression into tokens) and use the
grammar (in a form such as BNF) to write a parser
(a simple operator-precedence parser should do the
trick here).  The parser ensures that the input is
a valid expression and outputs a parse tree, which
your program can then do a postorder traversal on
to evaluate the expression.

If I haven't scared you away yet, look for a book
that covers finite state autotoma, BNF, lexers and
parsers.  There are several such books out there.
Avoid the book by Aho, et. al. -- it's the standard
reference, but it's too deep for what you're doing.
"Introduction to Compiler Construction" by Thomas
Parsons is a little easier to read, and though it
also goes way deeper than you need (like the Aho
book, its subject is writing a compiler, not just
parsing and evaluating expressions), it covers
everything you need to know.

Larry Coon
University of California
larry@assist.org
and lmcoon@home.com




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1997-10-27  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-10-27  0:00 anyone knows how to do this? sho
1997-10-27  0:00 ` Larry Coon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox