Re: Equation definintion

Alan Donovan <adonovan@imerge.co.uk>
2 Nov 1999 00:33:32 -0500

          From comp.compilers

Related articles
Equation definintion sriana@my-deja.com (1999-10-31)
Re: Equation definintion adonovan@imerge.co.uk (Alan Donovan) (1999-11-02)
| List of all articles for this month |
From: Alan Donovan <adonovan@imerge.co.uk>
Newsgroups: comp.compilers
Date: 2 Nov 1999 00:33:32 -0500
Organization: Imerge Ltd.
References: 99-10-193
Keywords: parse

sriana@my-deja.com wrote:
>
> Does anyone have the entire definition of an equation in grammar form.
> I have a short one that allows + * and ( ) but I need to also allow - /
> and exponentiation. I really don't want to sit down and figure the
> entire thing out if its out there somewhere, thanks!


Presuming you mean expression grammar, it's really very simple. Let's
assume for now you only have one type (equivalent to "double" in C). Now
you already have:


E -> ( E )
        | E + E
        | E * E


(and you will need to set the precedences of the operators
appropriately)


Well, just keep on adding:


        ...
      | E - E
      | E * E
      | E ^ E
      ;


You might also want to allow the unary minus operator (e.g. 2 + -1) and
some more arcane operators such as modulo (although you may need to
introduce a type system to distinguish integers from reals).


If you need an algebraic *equation* syntax, you will need to introduce
variable identifiers into the grammar (with E -> <name>) and also make a
new start symbol Q -> E = E. But equation solving is much harder than
equation parsing!


alan
------------------------------------------------------------------------
    Alan Donovan adonovan@imerge.co.uk http://www.imerge.co.uk
    Imerge Ltd. +44 1223 875265


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.