대상 언어 C#의 ANTLR을 사용하여 구문 분석 트리 (AST 아님)를 만들고 싶습니다. 이것은 덜 사소한 것 같습니다, 아마도 나는 모든 잘못된 장소를보고 있습니다. ParseTree
와ParseTree (AST 아님) 생성
public partial class TestParser
{
ParseTree pt = new ParseTree("root", null);
partial void EnterRule(string ruleName, int ruleIndex)
{
ParseTree child = new ParseTree(ruleName, pt);
pt.Children.Add(child);
this.pt = child;
}
partial void LeaveRule(string ruleName, int ruleIndex)
{
this.pt = pt.Parent;
}
}
이
public class ParseTree
{
private List<ParseTree> children = new List<ParseTree>();
public ParseTree(string name, ParseTree parent)
{
this.Parent = parent;
this.Rule = name;
}
public ParseTree Parent { get; private set; }
public string Rule { get; private set; }
public List<ParseTree> Children { get { return children; } }
public Boolean IsTerminal
{
get
{
return (children.Count == 0);
}
}
}
이 작동하지만 내 목표를 충족하지 않습니다되는 : 다음과 같이
지금까지 내가 생성 된 파서의 파셜을 구현하기 위해 노력 내가 할 수있는 이 트리에서 일치하는 토큰/텍스트를 가져올 수 없습니다. 그 외에도 추가적인 단점이 있습니다. 여러 문법에 대해이 작업을 수행하려는 경우 부분 클래스를 복사하여 붙여 넣기해야합니다. TestParser의 일부이기 때문에 푸딩 체인을 위로 올려 놓지 않아도됩니다.
나는 http://www.antlr.org/wiki/pages/viewpage.action?pageId=1760을 보았지만 생성 된 파서에는 ParseTreeBuilder
을 사용하는 생성자가 없습니다.
지금까지?
솔루션을 게시 해 주셔서 감사합니다. 'CommonTreeAdaptor'에 대한 클래스 정의도 나타낼 수 있습니까? – Annie
그것은 ANTLR – Martijn