创建ParseTree(不是AST)

时间:2012-12-10 11:25:42

标签: c# antlr antlr3

我想用目标语言C#中的ANTLR创建一个解析树(不是AST)。这似乎不那么微不足道,也许我正在寻找所有错误的地方。

到目前为止,我尝试在生成的解析器中实现partials,如下所示:

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

}

ParseTree

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,但生成的Parser没有一个带ParseTreeBuilder的构造函数。

现在去哪儿?

2 个答案:

答案 0 :(得分:2)

我发现了一个或多或少合理的解决方案。它有一个主要缺点:它只处理仅由令牌组成的生产规则的文本。这对我来说已经足够了,但可能不适合你。正确的实现也应该有令牌节点,因此可以正确地执行它。

适配器:

    public class ParseAdaptor : CommonTreeAdaptor
    {
        private C<ParseTree> container;

        public ParseAdaptor(C<ParseTree> container)
            : base()
        {
            this.container = container;
        }

        public override void AddChild(object t, object child)
        {
            base.AddChild(t, child);
            this.container.Value.Text += base.GetTree(child).Text;
        }


    }

ParseTree实现:

public class ParseTree
{
    private string ownText;
    private List<ParseTree> children = new List<ParseTree>();

    public ParseTree(string name, ParseTree parent)
    {
        this.Parent = parent;
        this.Rule = name;
    }

    public String Text
    {
        get
        {
            if (this.IsTerminal) return this.ownText;
            else
            {
                StringBuilder builder = new StringBuilder();
                foreach (ParseTree child in children)
                {
                    builder.Append(child.Text);
                }
                return builder.ToString();
            }
        }
        set
        {
            this.ownText = value;
        }
    }

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

}
//Isn't this the silliest little thing you've ever seen?
//Where is a pointer when you need one?
public class C<T>
{
    public T Value { get; set; }
}

它与部分粘在一起:

    public partial class TestParser
    {

        C<ParseTree> parseTreeContainer = new C<ParseTree>() { Value = new ParseTree("root", null) };

        public ParseTree Tree
        {
            get
            {
                return parseTreeContainer.Value;
            }
            set
            {
                parseTreeContainer.Value = value;
            }
        }

        partial void CreateTreeAdaptor(ref ITreeAdaptor adaptor)
        {
            adaptor = new ParseAdaptor(this.parseTreeContainer);
        }

        partial void EnterRule(string ruleName, int ruleIndex)
        {
            ParseTree child = new ParseTree(ruleName, Tree);
            ParseTree parent = Tree;
            parent.Children.Add(child);
            Tree = child;
        }

        partial void LeaveRule(string ruleName, int ruleIndex)
        {
            Tree = Tree.Parent;
        }

    }

答案 1 :(得分:-2)

你读过这篇文章了吗?

http://www.antlr.org/wiki/pages/viewpage.action?pageId=1760

示例是在Java中,但我怀疑在C#目标中也实现了相同的类。