comp.lang.ada
 help / color / mirror / Atom feed
From: "Steven Deller" <deller@smsail.com>
Subject: RE: Calculator and Operator Precedence
Date: Fri, 22 Feb 2002 11:10:15 -0500
Date: 2002-02-22T11:10:15-05:00	[thread overview]
Message-ID: <mailman.1014394382.27785.comp.lang.ada@ada.eu.org> (raw)
In-Reply-To: <3C73C481.6A9235A7@brighton.ac.uk>

Interestingly, I just cobbled together Ada code to parse arbitrary Ada
expressions consisting of just operators, parentheses, and literals (I'm
probably going to make it handle type conversions as well).

It uses two stacks (one for operands, one for operators), and recursion
(on parentheses).  Since each stack only needs a maximum of 5 entries
(because of Ada's left to right evaluation for equal precedence
operators) they could have been done with simple arrays (though I used a
stack package abstraction).

You don't need trees.  

I thought about using a grammar-based parse (Matthew H's solution), but
it has some problems with recognizing where you are in the grammar.

Unary operators are the biggest complication.

About 50 lines of code.  If you can prove you're not doing a student
exercise, I'd be glad to send you the sources.

Regards,
Steve




  reply	other threads:[~2002-02-22 16:10 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-02-19 16:26 Calculator and Operator Precedence jon
2002-02-19 17:24 ` Stephen Leake
2002-02-19 20:00 ` Matthew Heaney
2002-02-20  8:03 ` Phil Thornley
2002-02-20 15:28   ` Jon
2002-02-20 15:45     ` John English
2002-02-22 16:10       ` Steven Deller [this message]
2002-02-22 16:55         ` Jean-Marc Bourguet
2002-02-25  9:17           ` Dmitry A. Kazakov
2002-02-22 20:14         ` Vadim Godunko
replies disabled

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