GithubHelp home page GithubHelp logo

glrsharp's Introduction

GLRSharp

GLR Combinatorial Parser written in C# 4.0.

Introduction

GLRSharp is a General LR parser implemented as a set of combinatorial functions in C#. It currently only parses sources of type string, however it is architected using generic classes so parsing general binary sources can also be implemented.

GLRSharp implements a simple DSL embedded in C#. The following examples shows most of the features of the DSL. The example is based on the “Groucho Grammar” from Chapter 8 of Natural Language Processing with Python. The full source code can be found in the GLRTest project

// Pre-declare the NonTerminals.  
NonTerminal S = new NonTerminal("S");
NonTerminal PP = new NonTerminal("PP");
NonTerminal NP = new NonTerminal("NP");
NonTerminal VP = new NonTerminal("VP");
NonTerminal Det = new NonTerminal("Det");
NonTerminal N = new NonTerminal("N");
NonTerminal V = new NonTerminal("V");
NonTerminal P = new NonTerminal("P");

// The operator < (or >) implements a sequence of symbols.
// The operator | implements alternative productions
// The extension method T() returns a string terminal matcher.
// To declare an action to be executed when a production is match, use the syntax
//  (P) [a => action(a)]

S.RHS = (NP < VP)[a => string.Format( "S({0},{1})", a[0], a[1])];
PP.RHS = (P < NP)[a => string.Format("PP({0},{1})", a[0], a[1])];
NP.RHS = (Det < N)[a => string.Format("NP({0},{1})", a[0], a[1])] |
    (Det < N < PP)[a => string.Format("NP({0},{1},{2})", a[0], a[1],a[2])] | 
    "I".T();
VP.RHS = (V < NP)[a => string.Format( "VP({0},{1})", a[0], a[1])] |
    (VP < PP)[a => string.Format("VP({0},{1})", a[0], a[1])];
Det.RHS = "an".T() | "my".T();
N.RHS = "elephant".T() | "pajamas".T();
V.RHS = "shot".T();
P.RHS = "in".T();
           

// Create a parser with the grammar, with an option Log function
Parser parser = new Parser(S, Log, LogLevel.Trace);

// Set the skip function to skip white space
parser.Skip = (source, offset) => {
    while (offset < source.Length && char.IsWhiteSpace(source[offset]))
        offset++;
    return offset;
};
parser.Log = Log;
parser.Level = LogLevel.Trace;

// Parse a test string.  This should return two parse trees
var results = parser.Parse("I shot an elephant in my pajamas");
var matches = parser.Matches;

glrsharp's People

Contributors

jcoder58 avatar

Watchers

James Cloos avatar enginekit avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.