Re: Simple Expression Recursion vs Expression/Term/Factor

Andreas Gieriet <andi@diagonal.ch>
15 Aug 2001 01:15:58 -0400

          From comp.compilers

Related articles
Simple Expression Recursion vs Expression/Term/Factor stoggers@uniquest.demon.co.uk (Mike Stogden) (2001-08-02)
Re: Simple Expression Recursion vs Expression/Term/Factor Mark.van.den.Brand@cwi.nl (M.G.J. van den Brand) (2001-08-06)
Re: Simple Expression Recursion vs Expression/Term/Factor lucadesantis@infinito.it (luca) (2001-08-06)
Re: Simple Expression Recursion vs Expression/Term/Factor gregod@cs.rpi.edu (Douglas Gregor) (2001-08-06)
Re: Simple Expression Recursion vs Expression/Term/Factor joachim_d@gmx.de (Joachim Durchholz) (2001-08-08)
Re: Simple Expression Recursion vs Expression/Term/Factor lucads@xoommail.xoom.it (Luca) (2001-08-08)
Re: Simple Expression Recursion vs Expression/Term/Factor andi@diagonal.ch (Andreas Gieriet) (2001-08-15)
| List of all articles for this month |
From: Andreas Gieriet <andi@diagonal.ch>
Newsgroups: comp.compilers
Date: 15 Aug 2001 01:15:58 -0400
Organization: DS Diagonal Systems AG
Keywords: parse
Posted-Date: 15 Aug 2001 01:15:58 EDT

Mike Stogden wrote:
> Please could someone offer an explanation as to the relative merits of
> describing expressions as productions based on two comparable
> specifications?
>
> I have seen expressions for the same language described as...
>
> expr -> expr + expr
> expr -> expr * expr
> etc...
>
> alternatively...
>
> expr -> expr + term
> term -> factor | term * factor
> factor -> number | identifier
> etc...


Mike,


The second syntax description also defines precedence rules. I prefer
this one since it allows to create a syntax tree which can be
"evaluated" by implementing an evaluate() method on each node.


The class tree might look like:


    Expression
        Term
        Factor
        ...
        Primary
            Literal
                IntegerLiteral
                RealLiteral
                ...
            Name
                SimpleName
                IndexedName
                ...
            NestedExpression
            ...


Parsing of Term would be (C++ like):


--------- 8< ------------ 8< ---------------
// returns Term or Factor or Primary...
static Expression* Term::parse(Scanner& theScanner)
{
    Expression* e = Factor::parse(theScanner);
    while (theScanner.getCurrToken() == '*') {
        theScanner.getNextToken();
        Expression* e2 = Factor::parse(theScanner);
        e = new Term(e, e2);
    }
    return e;
}
--------- 8< ------------ 8< ---------------


Each class has an evaluate() method that returns a value.


E.g.,
--------- 8< ------------ 8< ---------------
Value Term::evaluate()
{
    return itsLeft->evaluate() * itsRight->evaluate();
}
--------- 8< ------------ 8< ---------------




Regards,


Andi


--
Andreas Gieriet, VP R&D mailto:andi@diagonal.ch
DS Diagonal Systems AG http://www.diagonal.ch/
Tumigerstrasse 71 Tel:+41-1-905-6060
CH-8606 Greifensee/Switzerland Fax:+41-1-905-6069


Post a followup to this message

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