CFG-RecursiveDescentParser 1.0.1

dotnet add package CFG-RecursiveDescentParser --version 1.0.1
NuGet\Install-Package CFG-RecursiveDescentParser -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CFG-RecursiveDescentParser" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CFG-RecursiveDescentParser --version 1.0.1
#r "nuget: CFG-RecursiveDescentParser, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CFG-RecursiveDescentParser as a Cake Addin
#addin nuget:?package=CFG-RecursiveDescentParser&version=1.0.1

// Install CFG-RecursiveDescentParser as a Cake Tool
#tool nuget:?package=CFG-RecursiveDescentParser&version=1.0.1

Info

C# library that allows grammars to be defined directly in the code on the basis of which the parsing process is performed. It is based on a recursive descent pattern.

Example grammars


        //Defionated symbols
        Symbol minus = new("-");
        Symbol open = new("(");
        Symbol close = new(")");
        Symbol comma = new(",");
        Symbol plus = new("+");
        Symbol mul = new("*");
        Symbol Digits = new(new Regex(@"\d+"), 50);
        Symbol Integer = new();
        Symbol PosInt = new();
        Symbol NegInt = new();
        Symbol Expr = new();
        Symbol Oper = new();
        
        Func<dynamic[], dynamic> fun = s =>
        {
            return new Number(s[0]);
        };
        Expr.AddRule(fun, Integer);
        
        fun = s =>
        {
            string op = s[0];
            Node expr1 = s[2];
            Node expr2 = s[4];
            var root = new Operator(op[0]) { Left = expr1, Right = expr2 };
            return root;
        };
        Expr.AddRule(fun, Oper, open, Expr, comma, Expr, close);

        fun = s => s[0];
        Integer.AddRule(fun, PosInt);

        fun = s => s[0];
        Integer.AddRule(fun, NegInt);

        fun = s =>
        {
            string ds = s[0].Value;
            return Convert.ToInt32(ds);
        };
        PosInt.AddRule(fun, Digits);

        fun = s =>
        {
            int x = s[1];
            return -x;
        };
        NegInt.AddRule(fun, minus, PosInt);

        Oper.AddRule(s => s[0], plus);
        Oper.AddRule(s => s[0], minus);
        Oper.AddRule(s => s[0], mul);

        Grammar g = new(Expr, minus, plus, mul, open, close, comma, 
            Digits, Integer, PosInt, NegInt, Oper);

        g.Parse("1");
        var ob = g.Result;
        Node tree = ob;
        //tree.Value() = 1
        
        g.Parse("+(1,1)");
        ob = g.Result;
        tree = ob;
        //tree.Value() = 2
        
        g.Parse("+(*(5,-(10,5)),*(-6,-4))");
        ob = g.Result;
        tree = ob;
        //tree.Value() = 49

        class Node
        {
            public Node? Left;
            public Node? Right;
        
            public Node(Node? l, Node? r)
            {
                Left = l;
                Right = r;
            }
            
            public virtual int Value()
            {
                return 0;
            }
        }
        
        class Operator : Node
        {
            private char _op;
        
            public Operator(char op) : base(null, null)
            {
                _op = op;
            }
        
            public override int Value()
            {
                switch (_op)
                {
                    case '+': return Left!.Value() + Right!.Value(); 
                    case '-': return Left!.Value() - Right!.Value(); 
                    case '*': return Left!.Value() * Right!.Value();
                    default: throw new NotImplementedException();
                }
            }
        }
        
        class Number : Node
        {
            private int _num;
        
            public Number(int num) : base(null, null)
            {
                _num = num;
            }
        
            public override int Value()
            {
                return _num;
            }
        }

Licence: MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 81 5/9/2024
1.0.0 85 5/7/2024

init