2

我想用目标语言 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.

现在去哪里?

4

1 回答 1

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

    }
于 2012-12-10T15:56:36.327 回答