我想解析表达式
- 一个
- ++一个
- 一个++
- ++一个++
- ++一个++++
前自增运算符优先于后自增运算符。
我拥有的解析器是:
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);
但是,当我使用解析器解析像“a++”这样的简单表达式时Expression
,测试会挂起。我想这是由于递归问题。
但是问题是什么以及如何解决呢?