Tree of unary operators

155 views Asked by At

I want to parse expressions like

  • a
  • ++a
  • a++
  • ++a++
  • ++a++++

The pre-increment operator has precedence over the post-increment operator.

The parsers I have are these:

public static readonly TokenListParser<LangToken, UnaryOperator> Increment = Token.EqualTo(LangToken.DoublePlus).Select(x => UnaryOperators.Increment);
public static readonly TokenListParser<LangToken, UnaryOperator> Decrement = Token.EqualTo(LangToken.DoubleMinus).Select(x => UnaryOperators.Decrement);

public static readonly TokenListParser<LangToken, Expression> Identifier = Token.EqualTo(LangToken.Identifier).Select(x => (Expression)new Id(x.ToStringValue()));

public static readonly TokenListParser<LangToken, Expression> A =
    from c in Parse.Ref(() => Expression)
    from op in Increment.Or(Decrement)
    select (Expression)new OpNode(op, c);

public static readonly TokenListParser<LangToken, Expression> B =
    from op in Increment.Or(Decrement)
    from c in Parse.Ref(() => Expression)
    select (Expression)new OpNode(op, c);

public static readonly TokenListParser<LangToken, Expression> Expression = A.Or(B).Or(Identifier);

However, when I parse a simple expression like "a++" using the Expression parser, the test hangs. I suppose that it's due to a recursion problem.

But what's the problem and how to solve it?

0

There are 0 answers