一元运算符树

时间:2018-12-21 12:17:22

标签: parsing left-recursion superpower

我想解析

之类的表达式
  • a
  • ++ a
  • a ++
  • ++ a ++
  • ++ a ++++

前递增运算符优先于后递增运算符。

我拥有的解析器是这些

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

但是,当我使用Expression解析器解析诸如“ a ++”之类的简单表达式时,测试将挂起。我想这是由于递归问题造成的。

但是有什么问题以及如何解决?

0 个答案:

没有答案