Click here to Skip to main content
12,753,809 members (33,013 online)
Click here to Skip to main content

Tagged as

Stats

45K views
1.7K downloads
37 bookmarked
Posted 22 Apr 2012

Visual AST for ANTLR Generated Parser Output

, 17 May 2012 CPOL
Build a visual AST from ANTLR parser
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Antlr.Runtime;
using EntMapping.Utility;

namespace EntMapping
{
    public partial class FormTest : Form
    {
        public FormTest()
        {
            InitializeComponent();
        }

        private void FormTest_Load(object sender, EventArgs e)
        {
            textBoxInput.Text = new Sampler().GetSample();
            pictureBox1.Size = new Size(1207, 388);
        }


        private void buttonParse_Click(object sender, EventArgs e)
        {
            string inputString = textBoxInput.Text;
            var input = new ANTLRStringStream(inputString);
            var lexer = new EntityMappingLexer(input);
            var tokens = new CommonTokenStream(lexer);
            var parser = new EntityMappingParser(tokens);
            var result = parser.prog();
            var tree = result.Tree;
            textBoxStringTree.Text = tree.ToStringTree();

            var treeNodeDrawable = new ASTTreeNode(tree);
            int center;
            if (pictureBox1.Image != null)
            {
                pictureBox1.Image.Dispose();
                pictureBox1.Image = null;
            }
            Image image = new VisualAST(treeNodeDrawable).Draw();
            pictureBox1.Image = image;
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Matthew So (Hong Kong)
Founder Software Force
Hong Kong Hong Kong
I am always interested in finding innovative ways for building better applications and founded a technology company since 2003. Welcome to exchange any idea with you and if I am not too busy before deadline of projects, I will reply your emails. Also, if you willing to pay for consulting works and customized software development, you can leave me message.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 17 May 2012
Article Copyright 2012 by Matthew So (Hong Kong)
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid