GrammGen in C# (1) First Concepts

Next Post

I worked a lot writing lexers and parsers, see:

and more. Two years ago, I met Ian Piumarta en la Smalltalks 2011, at Universidad de Quilmes (see AjSoda). He suggested I could work  on an implementation of a PEG:

Past year I tried to write something in JavaScript, but this year I put my effort on writing two implementations. I’m not sure they are PEGs, but they are close. Inm JavaScript, I have:

I’m doing “dog fooding” using it at:

with good results. But today, I would like to present my C# implementation:

It’s a solution built using TDD workflow. It has a class library and tests:

The idea is to have a parser with a list of rules. Each rule recognize a series of characters and patterns:

var rule = Rule.Get("0-9").OneOrMore().Generate("Integer");

The above rule recognize a series of digits, producing a non-terminal element with name “Integer” (the non-terminal are named with first letter in upper case). There is a fluent interface, and the .Generate method groups the result  (a character string) under a named element “Integer”.

The .Generate method could associate a custom object to “Integer” element:

    x => int.Parse((string)x));

In this case, it associates the conversion of the collected string to native integer.

You can write a list of rules:

var rules = new Rule[] {
    // ...
    Rule.Get("Term", Rule.Or('*', '/'), "Factor").Generate("Term", MakeBinaryOperatorExpresion),
    // ...

and then, with you can create a Parser object:

var parser = new Parser("1+2", rules);

Then, you can get a named element:

var element = parser.Parse("Expression");
var expression = (IExpression)element.Value;

Our code should generate an element of IExpression. That is our interface: it’s not part of GrammarGen. Each element returned by the library can have an associated custom element, attached in .Generate methods. See the test code, and the calculator sample (ie., it has left recusrion).

Now I have a console sample:

It parse and evaluate an string with an arithmetic expression:

I could use GrammGen for more ambitious projects, ie. building an AST (Abstract Syntax Tree) for a programming language.

Keek tuned!

Angel “Java” Lopez

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s