Click here to Skip to main content
Click here to Skip to main content

a Tiny Parser Generator v1.2

By , 21 Sep 2010
 
TinyPG_v1.0_binaries.zip
Release
Examples
BNFGrammar v1.0.tpg
simple expression1.tpg
simple expression2.tpg
Templates
TinyPG.exe
TinyPG_v1.0_sources.zip
TinyPG v1.0
bin
Debug
Release
TinyPG.vshost.exe
TinyPG.vshost.exe.manifest
Compiler
backup
vssver2.scc
vssver2.scc
Controls
DockExtender
vssver2.scc
vssver2.scc
Debug
vssver2.scc
Examples
BNFGrammar v0.2.tpg
BNFGrammar v1.0.tpg
simple expression1.tpg
simple expression2.tpg
test.tpg
vssver2.scc
mssccprj.scc
obj
Debug
TempPE
Release
TempPE
Properties.Resources.Designer.cs.dll
Properties
vssver2.scc
Templates
vssver2.scc
TinyPG.csproj.user
TinyPG.suo
vssver2.scc
TinyPG_v1.1_sources.zip
TinyPG v1.1
bin
Release
Examples
BNFGrammar v1.0.tpg
BNFGrammar v1.1.tpg
simple expression1.tpg
simple expression2.tpg
Templates
TinyPG.exe
Compiler
vssver2.scc
Controls
DockExtender
vssver2.scc
vssver2.scc
Debug
vssver2.scc
Examples
BNFGrammar v0.2.tpg
BNFGrammar v1.0.tpg
BNFGrammar v1.1.tpg
simple expression1.tpg
simple expression2.tpg
vssver2.scc
Highlighter
vssver2.scc
mssccprj.scc
Properties
vssver2.scc
Templates
vssver2.scc
TinyPG.csproj.user
TinyPG.suo
vssver2.scc
TinyPG_v1.2_binaries.zip
BNFGrammar v1.0.tpg
BNFGrammar v1.1.tpg
GrammarHighlighter.tpg
simple expression1.tpg
simple expression2.tpg
C#
VB
TinyPG.exe
TinyPG_v1.2_sources.zip
TinyPG v1.2
bin
Release
Examples
BNFGrammar v1.0.tpg
BNFGrammar v1.1.tpg
GrammarHighlighter.tpg
simple expression1.tpg
simple expression2.tpg
Templates
C#
VB
TinyPG.exe
CodeGenerators
CSharp
vssver2.scc
VBNet
vssver2.scc
vssver2.scc
Compiler
BNFGrammar.tpg
vssver2.scc
Controls
DockExtender
vssver2.scc
vssver2.scc
Debug
vssver2.scc
Examples
BNFGrammar v0.2.tpg
BNFGrammar v1.0.tpg
BNFGrammar v1.1.tpg
BNFGrammar_vb v1.1.tpg
GrammarHighlighter.tpg
GrammarHighlighter_vb.tpg
simple expression1.tpg
simple expression1_vb.tpg
simple expression2.tpg
simple expression2_vb.tpg
vssver2.scc
Highlighter
GrammarHighlighter v1.2.tpg
vssver2.scc
mssccprj.scc
Properties
vssver2.scc
Templates
C#
vssver2.scc
VB
vssver2.scc
vssver2.scc
TinyPG.csproj.user
TinyPG.suo
vssver2.scc
TinyPG_v1_3_binaries.zip
BNFGrammar 1.3.tpg
BNFGrammar_vb 1.3.tpg
GrammarHighlighter v1.3.tpg
GrammarHighlighter_vb.tpg
simple expression1.tpg
simple expression2.tpg
TinyPG.exe
TinyPG.vshost.exe
TinyPG.vshost.exe.manifest
TinyPG_v1_3_sources.zip
TinyPG v1.3
TinyPG.UnitTests
bin
Release
Examples
BNFGrammar 1.3.tpg
BNFGrammar v1.0.tpg
BNFGrammar v1.1.tpg
BNFGrammar_vb 1.3.tpg
GrammarHighlighter v1.3.tpg
GrammarHighlighter_vb.tpg
simple expression1.tpg
simple expression2.tpg
Templates
C#
VB
TinyPG.exe
TinyPG.UnitTests.dll
localtestrun.testrunconfig
mssccprj.scc
Properties
Testfiles
BNFGrammar v1.1.tpg
BNFGrammar_vb v1.1.tpg
GrammarHighlighter.tpg
GrammarHighlighter_vb.tpg
simple expression1.tpg
simple expression1_vb.tpg
simple expression2.tpg
simple expression2_vb.tpg
vssver2.scc
Testfiles
BNFGrammar v1.1.tpg
BNFGrammar_vb v1.1.tpg
GrammarHighlighter.tpg
GrammarHighlighter_vb.tpg
simple expression1.tpg
simple expression1_vb.tpg
simple expression2.tpg
simple expression2_vb.tpg
vssver2.scc
TestResults
TinyPG.testrunconfig
TinyPG.UnitTests.csproj.user
vssver2.scc
TinyPG
bin
Debug
Examples
BNFGrammar 1.3.tpg
BNFGrammar v1.0.tpg
BNFGrammar v1.1.tpg
BNFGrammar_vb 1.3.tpg
GrammarHighlighter v1.3.tpg
GrammarHighlighter.tpg
GrammarHighlighter_vb.tpg
simple expression1.tpg
simple expression2.tpg
Templates
C#
VB
TinyPG.exe
TinyPG.pdb
TinyPG.vshost.exe
Release
Examples
BNFGrammar 1.3.tpg
BNFGrammar_vb 1.3.tpg
GrammarHighlighter v1.3.tpg
GrammarHighlighter_vb.tpg
simple expression1.tpg
simple expression2.tpg
Templates
C#
VB
TinyPG.exe
TinyPG.pdb
TinyPG.vshost.exe
TinyPG.vshost.exe.manifest
CodeGenerators
CSharp
vssver2.scc
VBNet
vssver2.scc
vssver2.scc
Compiler
BNFGrammar 1.3.tpg
vssver2.scc
Controls
DockExtender
vssver2.scc
vssver2.scc
Debug
vssver2.scc
Examples
BNFGrammar 1.3.tpg
BNFGrammar_vb 1.3.tpg
csharpstruct.tpg
GrammarHighlighter v1.3.tpg
GrammarHighlighter_vb.tpg
simple expression1.tpg
simple expression1_vb.tpg
simple expression2.tpg
simple expression2_vb.tpg
vssver2.scc
Highlighter
GrammarHighlighter v1.3.tpg
vssver2.scc
mssccprj.scc
obj
Debug
TempPE
Properties.Resources.Designer.cs.dll
TinyPG.Controls.AutoComplete.resources
TinyPG.Controls.RegExControl.resources
TinyPG.csproj.GenerateResource.Cache
TinyPG.exe
TinyPG.MainForm.resources
TinyPG.pdb
TinyPG.Properties.Resources.resources
Release
TempPE
Properties.Resources.Designer.cs.dll
TinyPG.Controls.AutoComplete.resources
TinyPG.Controls.RegExControl.resources
TinyPG.csproj.GenerateResource.Cache
TinyPG.exe
TinyPG.MainForm.resources
TinyPG.pdb
TinyPG.Properties.Resources.resources
Properties
vssver2.scc
Templates
C#
vssver2.scc
VB
vssver2.scc
vssver2.scc
TinyPG.csproj.user
TinyPG.suo
vssver2.scc
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TinyPG.Compiler;

namespace TinyPG.UnitTests
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class ParserTester
    {
        // TODO: set the correct paths to be able to run the unittests succesfully
        private const string TEMPLATEPATH = @"D:\MyProjects\Net\TinyPG v1.3\TinyPG\Templates\C#\";
        private const string TEMPLATEPATH_VB = @"D:\MyProjects\Net\TinyPG v1.3\TinyPG\Templates\VB\";
        private const string OUTPUTPATH = @"D:\MyProjects\Net\TinyPG v1.3\TinyPG.UnitTests\";
        private const string TESTFILESPATH = @"D:\MyProjects\Net\TinyPG v1.3\TinyPG.UnitTests\Testfiles\";

        public ParserTester()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion


        private GrammarTree LoadGrammar(string filename)
        {
            string grammarfile = System.IO.File.ReadAllText(filename);
            Scanner scanner = new Scanner();
            Parser parser = new Parser(scanner);
            GrammarTree tree = (GrammarTree)parser.Parse(grammarfile, new GrammarTree());
            return tree;
        }

        [TestMethod]
        public void SimpleExpression1_Test()
        {
            
            GrammarTree GT = LoadGrammar(TESTFILESPATH + @"simple expression1.tpg");
            Grammar G = (Grammar) GT.Eval();


            G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH;
            G.Directives["TinyPG"]["OutputPath"] = OUTPUTPATH;

            // basic checks
            string temp = G.PrintFirsts();
            Assert.IsTrue(!String.IsNullOrEmpty(temp));
            temp = G.GetOutputPath();
            Assert.IsTrue(!String.IsNullOrEmpty(temp));
            temp = G.PrintGrammar();
            Assert.IsTrue(!String.IsNullOrEmpty(temp));

            Compiler.Compiler compiler = new Compiler.Compiler();
            
            compiler.Compile(G);

            Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors");

            CompilerResult result = compiler.Run("5+7/3*2+(4*2)");

            

            Assert.IsTrue(result.Output.StartsWith("Parse was successful."));
        }

        [TestMethod]
        public void SimpleExpression1_VB_Test()
        {
            GrammarTree GT = LoadGrammar(TESTFILESPATH + @"simple expression1_vb.tpg");
            Grammar G = (Grammar)GT.Eval();
            G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH_VB;

            Compiler.Compiler compiler = new Compiler.Compiler();

            compiler.Compile(G);
            Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors");

            CompilerResult result = compiler.Run("5+7/3*2+(4*2)");

            Assert.IsTrue(result.Output.StartsWith("Parse was successful."));
        }

        [TestMethod]
        public void SimpleExpression2_Test()
        {
            GrammarTree GT = LoadGrammar(TESTFILESPATH + @"simple expression2.tpg");
            Grammar G = (Grammar)GT.Eval();
            G.Directives.Add(new Directive("TinyPG"));
            G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH;

            Compiler.Compiler compiler = new Compiler.Compiler();

            compiler.Compile(G);
            Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors");

            CompilerResult result = compiler.Run("5+8/4*2+(4*2)");

            Assert.IsTrue(Convert.ToInt32(result.Value) == 17);
        }

        [TestMethod]
        public void SimpleExpression2_VB_Test()
        {
            GrammarTree GT = LoadGrammar(TESTFILESPATH + @"simple expression2_vb.tpg");
            Grammar G = (Grammar)GT.Eval();
            G.Directives.Add(new Directive("TinyPG"));
            G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH_VB;

            Compiler.Compiler compiler = new Compiler.Compiler();

            compiler.Compile(G);
            Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors");

            CompilerResult result = compiler.Run("5+8/4*2+(4*2)");

            Assert.IsTrue(Convert.ToInt32(result.Value) == 17);
        }

        [TestMethod]
        public void SimpleExpression3_Test()
        {
            GrammarTree GT = LoadGrammar(TESTFILESPATH + @"BNFGrammar v1.1.tpg");
            Grammar G = (Grammar)GT.Eval();
            G.Directives.Add(new Directive("TinyPG"));
            G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH;

            Compiler.Compiler compiler = new Compiler.Compiler();

            compiler.Compile(G);
            Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors");

            CompilerResult result = compiler.Run("");

            Assert.IsTrue(result.Output.StartsWith("Parse was successful."));
        }

        [TestMethod]
        public void SimpleExpression3_VB_Test()
        {
            GrammarTree GT = LoadGrammar(TESTFILESPATH + @"BNFGrammar_vb v1.1.tpg");
            Grammar G = (Grammar)GT.Eval();
            G.Directives.Add(new Directive("TinyPG"));
            G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH_VB;

            Compiler.Compiler compiler = new Compiler.Compiler();

            compiler.Compile(G);
            Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors");

            CompilerResult result = compiler.Run("");

            Assert.IsTrue(result.Output.StartsWith("Parse was successful."));
        }

        [TestMethod]
        public void SimpleExpression4_Test()
        {
            GrammarTree GT = LoadGrammar(TESTFILESPATH + @"GrammarHighlighter.tpg");
            Grammar G = (Grammar)GT.Eval();
            G.Directives.Add(new Directive("TinyPG"));
            G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH;

            Compiler.Compiler compiler = new Compiler.Compiler();

            compiler.Compile(G);
            Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors");

            CompilerResult result = compiler.Run("using System.IO;\r\n");

            Assert.IsTrue(result.Output.StartsWith("Parse was successful."));
        }

        [TestMethod]
        public void SimpleExpression4_VB_Test()
        {
            GrammarTree GT = LoadGrammar(TESTFILESPATH + @"GrammarHighlighter_vb.tpg");
            Grammar G = (Grammar)GT.Eval();
            G.Directives.Add(new Directive("TinyPG"));
            G.Directives["TinyPG"]["TemplatePath"] = TEMPLATEPATH_VB;

            Compiler.Compiler compiler = new Compiler.Compiler();

            compiler.Compile(G);
            Assert.IsTrue(compiler.Errors.Count == 0, "compilation contains errors");

            CompilerResult result = compiler.Run("using System.IO;\r\n");

            Assert.IsTrue(result.Output.StartsWith("Parse was successful."));
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of use 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)

About the Author

Herre Kuijpers
Architect Capgemini
Netherlands Netherlands
Member
Currently Herre Kuijpers is employed at Capgemini Netherlands for over 10 years, where he developed skills with all kinds of technologies, methodologies and programming languages such as c#, ASP.Net, Silverlight, VC++, Javascript, SQL, UML, RUP, WCF. Currently he fulfills the role of software architect in various projects.
 
Herre Kuijpers is a very experienced software architect with deep knowledge of software design and development on the Microsoft .Net platform. He has a broad knowledge of Microsoft products and knows how these, in combination with custom software, can be optimally implemented in the often complex environment of the customer.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 21 Sep 2010
Article Copyright 2008 by Herre Kuijpers
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid