Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

a Tiny Parser Generator v1.2

, 21 Sep 2010 CPOL
@TinyPG is a utility that makes it easier to write and try out your own parser/compiler.
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
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
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
' Generated by TinyPG v1.3 available at www.codeproject.com

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Xml.Serialization


Namespace TinyPG
#Region "ParseTree"
    <Serializable()> _
    Public Class ParseErrors
        Inherits List(Of ParseError)

        Public Sub New()

        End Sub
    End Class

    <Serializable()> _
    Public Class ParseError 
        Private m_message As String
        Private m_code As Integer
        Private m_line As Integer
        Private m_col As Integer
        Private m_pos As Integer
        Private m_length As Integer

        Public ReadOnly Property Code() As Integer
            Get
                Return m_code
            End Get
        End Property

        Public ReadOnly Property Line() As Integer
            Get
                Return m_line
            End Get
        End Property

        Public ReadOnly Property Column() As Integer
            Get
                Return m_col
            End Get
        End Property

        Public ReadOnly Property Position() As Integer
            Get
                Return m_pos
            End Get
        End Property

        Public ReadOnly Property Length() As Integer
            Get
                Return m_length
            End Get
        End Property

        Public ReadOnly Property Message() As String
            Get
                Return m_message
            End Get
        End Property

        Public Sub New(ByVal message As String, ByVal code As Integer, ByVal node As ParseNode)
            Me.New(message, code, 0, node.Token.StartPos, node.Token.StartPos, node.Token.Length)
        End Sub

        Public Sub New(ByVal message As String, ByVal code As Integer, ByVal line As Integer, ByVal col As Integer, ByVal pos As Integer, ByVal length As Integer)
            m_message = message
            m_code = code
            m_line = line
            m_col = col
            m_pos = pos
            m_length = length
        End Sub
    End Class

    ' rootlevel of the node tree
    <Serializable()> _
    Partial Public Class ParseTree
        Inherits ParseNode

        Public Errors As ParseErrors

        Public Skipped As List(Of Token)

        Public Sub New()
            MyBase.New(New Token(), "ParseTree")
            Token.Type = TokenType.Start
            Token.Text = "Root"
            Skipped = New List(Of Token)()
            Errors = New ParseErrors()
        End Sub

        Public Function PrintTree() As String
    Dim sb As New StringBuilder()
    Dim indent As Integer = 0
            PrintNode(sb, Me, indent)
            Return sb.ToString()
        End Function

    Private Sub PrintNode(ByVal sb As StringBuilder, ByVal node As ParseNode, ByVal indent As Integer)

        Dim space As String = "".PadLeft(indent, " "c)

        sb.Append(space)
        sb.AppendLine(node.Text)

        For Each n As ParseNode In node.Nodes
            PrintNode(sb, n, indent + 2)
        Next
    End Sub

    ''' <summary>
    ''' this is the entry point for executing and evaluating the parse tree.
    ''' </summary>
    ''' <param name="paramlist">additional optional input parameters</param>
    ''' <returns>the output of the evaluation function</returns>
    Public Overloads Function Eval(ByVal ParamArray paramlist As Object()) As Object
        Return Nodes(0).Eval(Me, paramlist)
    End Function
    End Class
#End Region

#Region "ParseNode"
    <Serializable()> _
    <XmlInclude(GetType(ParseTree))> _
    Partial Public Class ParseNode 
        Protected m_text As String
        Protected m_nodes As List(Of ParseNode)
        

        Public ReadOnly Property Nodes() As List(Of ParseNode)
            Get
                Return m_nodes
            End Get
        End Property

        
        <XMLIgnore()> _
        Public Parent As ParseNode
        Public Token As Token
        ' the token/rule
        <XmlIgnore()> _
        Public Property Text() As String
            ' text to display in parse tree 
            Get
                Return m_text
            End Get
            Set(ByVal value As String)
                m_text = value
            End Set
        End Property

        Public Overridable Function CreateNode(ByVal token As Token, ByVal text As String) As ParseNode
            Dim node As New ParseNode(token, text)
            node.Parent = Me
            Return node
        End Function

        Protected Sub New(ByVal token As Token, ByVal text As String)
            Me.Token = token
            m_text = text
            m_nodes = New List(Of ParseNode)()
        End Sub

        Protected Function GetValue(ByVal tree As ParseTree, ByVal type As TokenType, ByVal index As Integer) As Object
            Return GetValueByRef(tree, type, index)
        End Function

        Protected Function GetValueByRef(ByVal tree As ParseTree, ByVal type As TokenType, ByRef index As Integer) As Object
            Dim o As Object = Nothing
            If index < 0 Then
                Return o
            End If

            ' left to right
            For Each node As ParseNode In nodes
                If node.Token.Type = type Then
                    System.Math.Max(System.Threading.Interlocked.Decrement(index), index + 1)
                    If index < 0 Then
                        o = node.Eval(tree)
                        Exit For
                    End If
                End If
            Next
            Return o
        End Function

        ''' <summary>
        ''' this implements the evaluation functionality, cannot be used directly
        ''' </summary>
        ''' <param name="tree">the parsetree itself</param>
        ''' <param name="paramlist">optional input parameters</param>
        ''' <returns>a partial result of the evaluation</returns>
        Friend Function Eval(ByVal tree As ParseTree, ByVal ParamArray paramlist As Object()) As Object
            Dim Value As Object = Nothing

            Select Case Token.Type
                Case TokenType.Start
                    Value = EvalStart(tree, paramlist)
                    Exit Select
                Case TokenType.AddExpr
                    Value = EvalAddExpr(tree, paramlist)
                    Exit Select
                Case TokenType.MultExpr
                    Value = EvalMultExpr(tree, paramlist)
                    Exit Select
                Case TokenType.Atom
                    Value = EvalAtom(tree, paramlist)
                    Exit Select

                Case Else
                    Value = Token.Text
                    Exit Select
            End Select
            Return Value
        End Function

        Protected Overridable Function EvalStart(ByVal tree As ParseTree, ByVal ParamArray paramlist As Object()) As Object
            Return Me.GetValue(tree, TokenType.AddExpr, 0)
        End Function

        Protected Overridable Function EvalAddExpr(ByVal tree As ParseTree, ByVal ParamArray paramlist As Object()) As Object
            Dim Value As Integer = Convert.ToInt32(Me.GetValue(tree, TokenType.MultExpr, 0))
        	Dim i As Integer = 1
        	While Me.GetValue(tree, TokenType.MultExpr, i) IsNot Nothing
        		Dim sign As String = Me.GetValue(tree, TokenType.PLUSMINUS, i-1).ToString()
        		If sign = "+" Then
        			Value += Convert.ToInt32(Me.GetValue(tree, TokenType.MultExpr, i))
        		Else 
        			Value -= Convert.ToInt32(Me.GetValue(tree, TokenType.MultExpr, i))
        		End If
        		i=i+1
        	End While
        	Return Value
        End Function

        Protected Overridable Function EvalMultExpr(ByVal tree As ParseTree, ByVal ParamArray paramlist As Object()) As Object
            Dim Value As Integer = Convert.ToInt32(Me.GetValue(tree, TokenType.Atom, 0))
        	Dim i As Integer = 1
        	While Me.GetValue(tree, TokenType.Atom, i) IsNot Nothing
        		Dim sign As String = Me.GetValue(tree, TokenType.MULTDIV, i-1).ToString()
        		If sign = "*" Then
        			Value *= Convert.ToInt32(Me.GetValue(tree, TokenType.Atom, i))
        		Else 
        			Value /= Convert.ToInt32(Me.GetValue(tree, TokenType.Atom, i))
        		End If
        		i=i+1
        	End While
        
        	Return Value
        End Function

        Protected Overridable Function EvalAtom(ByVal tree As ParseTree, ByVal ParamArray paramlist As Object()) As Object
            If Me.GetValue(tree, TokenType.NUMBER, 0) IsNot Nothing Then
        		Return Me.GetValue(tree, TokenType.NUMBER, 0)
        	Else 
        		Return Me.GetValue(tree, TokenType.AddExpr, 0)
        	End If
        End Function



    End Class
#End Region
End Namespace

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

Herre Kuijpers
Architect Capgemini
Netherlands Netherlands
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.

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 21 Sep 2010
Article Copyright 2008 by Herre Kuijpers
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid