1

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • From the looks of it, you have an infinite loop that's occurring in the `Expression` parser. `Expression` calls `A` which calls `Expression` which calls `A`, and so on. Can you post the definitions for `LangToken`, `UnaryOperator`, `UnaryOperators`, `OpNode`, and `Id` so I could get a working sample up and running locally? – jtate Jan 07 '19 at 21:28

0 Answers0