Implementing an "in" operator

"Ed Davis" <ed_davis2@yahoo.com>
13 Nov 2002 12:23:05 -0500

          From comp.compilers

Related articles
Implementing an "in" operator ed_davis2@yahoo.com (Ed Davis) (2002-11-13)
Re: Implementing an "in" operator n502640813.ch@chch.demon.co.uk (Charles Bryant) (2002-11-20)
Re: Implementing an "in" operator sos@zjod.net (Steve Siegfried) (2002-11-24)
Re: Implementing an "in" operator ed_davis2@yahoo.com (Ed Davis) (2002-11-26)
| List of all articles for this month |
From: "Ed Davis" <ed_davis2@yahoo.com>
Newsgroups: comp.compilers
Date: 13 Nov 2002 12:23:05 -0500
Organization: http://groups.google.com/
Keywords: code, question, comment
Posted-Date: 13 Nov 2002 12:23:05 EST

Any ideas on how one would implement an "in" operator?


As in (no pun intended):


        if (a in b,c,d..e)
                statements;


translates to:


        if (a == b or a == c or (a >= d and a <=e))
                statements;


I don't have a problem with unary and binary operators, but
this one has got me stumped. Part of my simple
(integer-only) expression parser is shown below:


static Tree *expr(int p) {
        Tree *t = primary(); /* also handles unary operators */
        while (isBinaryOperator(tok.sym) &&
                        precedence(tok.sym) >= p) {
                const Symtype op = tok.sym;
                getsym();
                t = mkBinaryNode(op, t,
                                expr(associativity(op) == Right ?
                                        precedence(op) : 1 + precedence(op)));
        }
        return t;
}


Any ideas on how "in" might be implemented in the above?
[Treat comma and .. as operators long enough to build up a tree of
stuff you can translate when you deal with the "in", perhaps. -John]


Post a followup to this message

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