Implementing an "in" operator

"Ed Davis" <>
13 Nov 2002 12:23:05 -0500

          From comp.compilers

Related articles
Implementing an "in" operator (Ed Davis) (2002-11-13)
Re: Implementing an "in" operator (Charles Bryant) (2002-11-20)
Re: Implementing an "in" operator (Steve Siegfried) (2002-11-24)
Re: Implementing an "in" operator (Ed Davis) (2002-11-26)
| List of all articles for this month |

From: "Ed Davis" <>
Newsgroups: comp.compilers
Date: 13 Nov 2002 12:23:05 -0500
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)

translates to:

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

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;
                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.